8.18 2020 Multi-University Training Contest 9题解及补题

8.18 2020 Multi-University Training Contest 9题解及补题

比赛过程

据说最后两场比较难,改了很多次出的A,就相当于费劲签了到。

题解

A Tree

题意

给了你一棵树,然后加一条有向边,找到最大数量的\((x,y)\)

解法

假如说没加边,那么总共的数量就是所有节点的孩子数量(包括自己)全部加起来,加入的这条有向边一定是从某一叶子节点到根节点,接着开个数组存一下信息就可以开始乱搞了。
比赛的时候一直TLE和WA

代码

#pragma region
#include <algorithm>
#include <cmath>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <unordered_map>
#include <vector>
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define rep(i, a, n) for (int i = a; i <= n; ++i)
#define per(i, a, n) for (int i = n; i >= a; --i)
namespace fastIO
{
#define BUF_SIZE 100000
#define OUT_SIZE 100000
    //fread->R
    bool IOerror = 0;
    //inline char nc(){char ch=getchar();if(ch==-1)IOerror=1;return ch;}
    inline char nc()
    {
        static char buf[BUF_SIZE], *p1 = buf + BUF_SIZE, *pend = buf + BUF_SIZE;
        if (p1 == pend)
        {
            p1 = buf;
            pend = buf + fread(buf, 1, BUF_SIZE, stdin);
            if (pend == p1)
            {
                IOerror = 1;
                return -1;
            }
        }
        return *p1++;
    }
    inline bool blank(char ch) { return ch == ' ' || ch == '\n' || ch == '\r' || ch == '\t'; }
    template <class T>
    inline bool R(T &x)
    {
        bool sign = 0;
        char ch = nc();
        x = 0;
        for (; blank(ch); ch = nc())
            ;
        if (IOerror)
            return false;
        if (ch == '-')
            sign = 1, ch = nc();
        for (; ch >= '0' && ch <= '9'; ch = nc())
            x = x * 10 + ch - '0';
        if (sign)
            x = -x;
        return true;
    }
    inline bool R(double &x)
    {
        bool sign = 0;
        char ch = nc();
        x = 0;
        for (; blank(ch); ch = nc())
            ;
        if (IOerror)
            return false;
        if (ch == '-')
            sign = 1, ch = nc();
        for (; ch >= '0' && ch <= '9'; ch = nc())
            x = x * 10 + ch - '0';
        if (ch == '.')
        {
            double tmp = 1;
            ch = nc();
            for (; ch >= '0' && ch <= '9'; ch = nc())
                tmp /= 10.0, x += tmp * (ch - '0');
        }
        if (sign)
            x = -x;
        return true;
    }
    inline bool R(char *s)
    {
        char ch = nc();
        for (; blank(ch); ch = nc())
            ;
        if (IOerror)
            return false;
        for (; !blank(ch) && !IOerror; ch = nc())
            *s++ = ch;
        *s = 0;
        return true;
    }
    inline bool R(char &c)
    {
        c = nc();
        if (IOerror)
        {
            c = -1;
            return false;
        }
        return true;
    }
    template <class T, class... U>
    bool R(T &h, U &... t) { return R(h) && R(t...); }
#undef OUT_SIZE
#undef BUF_SIZE
}; // namespace fastIO
using namespace fastIO;
template <class T>
void _W(const T &x) { cout << x; }
void _W(const int &x) { printf("%d", x); }
void _W(const int64_t &x) { printf("%lld", x); }
void _W(const double &x) { printf("%.16f", x); }
void _W(const char &x) { putchar(x); }
void _W(const char *x) { printf("%s", x); }
template <class T, class U>
void _W(const pair<T, U> &x) { _W(x.fa), putchar(' '), _W(x.S); }
template <class T>
void _W(const vector<T> &x)
{
    for (auto i = x.begin(); i != x.end(); _W(*i++))
        if (i != x.cbegin())
            putchar(' ');
}
void W() {}
template <class T, class... U>
void W(const T &head, const U &... tail) { _W(head), putchar(sizeof...(tail) ? ' ' : '\n'), W(tail...); }
#pragma endregion
const int maxn = 5e5 + 500;
const int mod = 1e9 + 7;
ll gcd(ll T, ll b) { return b == 0 ? T : gcd(b, T % b); }
ll lcm(ll T, ll b) { return T / gcd(T, b) * b; }
ll mul(ll a, ll b, ll c)
{
    ll ans = 0;
    while (b)
    {
        if (b & 1)
        {
            ans = (ans + a) % c;
            b--;
        }
        b >>= 1;
        a = (a + a) % c;
    }
    return ans;
}
ll powmod(ll a, ll b, ll c)
{
    ll ans = 1;
    while (b)
    {
        if (b & 1)
        {
            ans = mul(ans, a, c);
            b--;
        }
        b >>= 1;
        a = mul(a, a, c);
    }
    return ans;
}

inline int read()
{
    int x = 0, f = 1;
    char ch = getchar();
    while (ch < '0' || ch > '9')
    {
        if (ch == '-')
            f = -1;
        ch = getchar();
    }
    while (ch >= '0' && ch <= '9')
    {
        x = x * 10 + ch - '0';
        ch = getchar();
    }
    return x * f;
}
ll fa[maxn];
vector<ll> G[maxn];
ll num[maxn], sum[maxn];
ll n, ans = 0;
void dfs(ll u, ll f)
{
    ++num[u];
    for (auto v : G[u])
    {
        dfs(v, u);
        num[u] += num[v];
    }
    ans += num[u];
}
void dfs2(ll u, ll f)
{
    sum[u] = sum[f] + n - num[u];
    for (auto v : G[u])
    {
        dfs2(v, u);
    }
}
void init()
{
    fa[1] = 0;
    for (int i = 1; i <= n; ++i)
    {
        sum[i] = 0, num[i] = 0, G[i].clear();
    }
}
int main()
{
    ll T;
    scanf("%lld", &T);
    while (T--)
    {
        ans = 0;
        scanf("%lld", &n);
        init();
        for (int i = 2; i <= n; ++i)
        {
            scanf("%lld", fa + i);
            G[fa[i]].push_back(i);
        }
        dfs(1, 0);
        dfs2(1, 0);
        sort(sum + 1, sum + 1 + n);
        ans += sum[n];
        printf("%lld\n", ans);
    }
    return 0;
}

B

题意

解法

代码

//将内容替换成代码

C Slime and Stones

题意

有两堆石子,第一堆有 $a \(个石头,第二堆有\) b$个石头。
你有两种操作:
操作一:从某堆拿任意个石子。
操作二:从第一堆拿 $x $个,从第二堆拿 \(y\)个,要求 \(∣x−y∣≤k\),其中\(k\)是给定常数。

每个人都是最优策略,让你求是否先手必胜(1)或先手必败(2)

解法

”扩展威佐夫博弈“

代码

#include <algorithm>
#include <bitset>
#include <cctype>
#include <cerrno>
#include <clocale>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <deque>
#include <exception>
#include <fstream>
#include <functional>
#include <limits>
#include <list>
#include <map>
#include <iomanip>
#include <ios>
#include <iosfwd>
#include <iostream>
#include <istream>
#include <ostream>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <stdexcept>
#include <streambuf>
#include <string>
#include <utility>
#include <vector>
#include <cwchar>
#include <cwctype>
#define IO ios::sync_with_stdio(0), cin.tie(0)
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
const int inf = ~0u >> 1;
typedef pair<int, int> P;
#define REP(i, a, n) for (int i = a; i < (n); ++i)
#define PER(i, a, n) for (int i = (n)-1; i >= a; --i)
const ll mod = 1e9 + 7;
ll gcd(ll T, ll b) { return b == 0 ? T : gcd(b, T % b); }
ll lcm(ll T, ll b) { return T / gcd(T, b) * b; }
const int maxn = 1000002;
ll a[maxn];
void init() {
	a[0]=1;
	for(int i=1;i<=1000000;i++) {
		a[i]=(a[i-1]*i)%mod;
	}
}
template <typename T>
void read(T &x)
{
	x = 0;
	char ch = getchar();
	ll f = 1;
	while (!isdigit(ch))
	{
		if (ch == '-')
			f *= -1;
		ch = getchar();
	}
	while (isdigit(ch))
	{
		x = x * 10 + ch - 48;
		ch = getchar();
	}
	x *= f;
}
int main()
{
    int T;
    cin>>T;
    while(T--){
        int a,b,t;
        cin>>a>>b>>t;
        double k = (double)t;
        if(a > b)swap(a,b);
        double XA = (1.0-k+sqrt(5.0+2*k+k*k))/2;
        double XB = (3.0+k+sqrt(5.0+2*k+k*k))/2;
        int l = 0;
        int r = 100000000;
        while(l < r){
            int mid = l + r >> 1;
            if(floor((double)mid * XA) > a)r = mid - 1;
            else if(floor((double)mid * XA) < a)l = mid + 1;
            else {
                l = mid;
                break;
            }
        }
        if(floor((double)l * XA) == a && floor((double)l * XB) == b)printf("0\n");
        else printf("1\n");
    }
    return 0;
}

D

题意

解法

代码

//将内容替换成代码

E

题意

解法

代码

//将内容替换成代码

F

题意

解法

代码

//将内容替换成代码

G Game

题意

解法

代码

//将内容替换成代码

H

题意

解法

代码

//将内容替换成代码

I

题意

解法

代码

//将内容替换成代码

J

题意

解法

代码

//将内容替换成代码
posted @ 2020-09-01 22:52  cugbacm03  阅读(143)  评论(0)    收藏  举报