AtCoder AGC033C Removing Coins (Game Theory)

Topic Link

https://atcoder.jp/contests/agc033/tasks/agc033_c

answer

Finally will do the most simple game theory point of ......
First, the meaning of the title of the operation is to select a point, all the leaves are not at this point deleted (if this point is not a leaf cut from all the leaves).
For any one point is not less than \ (3 \) of the tree, there must be a point (such as non-leaf nodes), such that after the operation of reducing the diameter of the point \ (2 \) ; at the same time there must be a point (such as a diameter endpoint), so that the operating point of the reduced diameter \ (1 \) ; while any one of operation such that the diameter of the absence of other changes. This model is therefore a Bash game, the diameter size of the modulus answer \ (3 \) values related.
Set diameter length (number of dots) of \ (L \) . If \ (l = 1 \) is the upper hand win, \ (L = 2 \) is flip win. Later it becomes a situation just discussed, so if and only if the length of the diameter \ (\ mod 3 = 2 \ ) when the flip win.
Time complexity \ (O (n-) \) .

Code

#include<bits/stdc++.h>
#define llong long long
using namespace std;

inline int read()
{
    int x = 0,f = 1; char ch = getchar();
    for(;!isdigit(ch);ch=getchar()) {if(ch=='-') f = -1;}
    for(; isdigit(ch);ch=getchar()) {x = x*10+ch-48;}
    return x*f;
}

const int N = 2e5;
struct Edge
{
    int v,nxt;
} e[(N<<1)+3];
int fe[N+3];
int fa[N+3];
int len[N+3];
int n,en,mx;

void addedge(int u,int v)
{
    en++; e[en].v = v;
    e[en].nxt = fe[u]; fe[u] = en;
}

void dfs(int u)
{
    for(int i=fe[u]; i; i=e[i].nxt)
    {
        int v = e[i].v; if(v==fa[u]) continue;
        fa[v] = u; dfs(v);
        mx = max(mx,len[u]+len[v]+1);
        len[u] = max(len[u],len[v]+1);
    }
}

int main()
{
    scanf("%d",&n);
    for(int i=1; i<n; i++) {int u,v; scanf("%d%d",&u,&v); addedge(u,v); addedge(v,u);}
    dfs(1);
    if(mx%3==1) {puts("Second");}
    else {puts("First");}
    return 0;
}

Guess you like

Origin www.cnblogs.com/suncongbo/p/12227637.html