zoukankan      html  css  js  c++  java
  • A

    There is a tree with NN vertices numbered 11 through NN. The ii-th of the N1N−1 edges connects vertices aiai and bibi.

    Initially, each vertex is uncolored.

    Takahashi and Aoki is playing a game by painting the vertices. In this game, they alternately perform the following operation, starting from Takahashi:

    • Select a vertex that is not painted yet.
    • If it is Takahashi who is performing this operation, paint the vertex white; paint it black if it is Aoki.

    Then, after all the vertices are colored, the following procedure takes place:

    • Repaint every white vertex that is adjacent to a black vertex, in black.

    Note that all such white vertices are repainted simultaneously, not one at a time.

    If there are still one or more white vertices remaining, Takahashi wins; if all the vertices are now black, Aoki wins. Determine the winner of the game, assuming that both persons play optimally.

    Constraints

     

    • 2N1052≤N≤105
    • 1ai,biN1≤ai,bi≤N
    • aibiai≠bi
    • The input graph is a tree.

    Input

     

    Input is given from Standard Input in the following format:

    NN
    a1a1 b1b1
    :
    aN1aN−1 bN1bN−1
    

    Output

     

    Print First if Takahashi wins; print Second if Aoki wins.

    Sample Input 1

     

    3
    1 2
    2 3
    

    Sample Output 1

     

    First
    

    Below is a possible progress of the game:

    • First, Takahashi paint vertex 22 white.
    • Then, Aoki paint vertex 11 black.
    • Lastly, Takahashi paint vertex 33 white.

    In this case, the colors of vertices 1122 and 33 after the final procedure are black, black and white, resulting in Takahashi's victory.

    Sample Input 2

     

    4
    1 2
    2 3
    2 4
    

    Sample Output 2

     

    First
    

    Sample Input 3

     

    6
    1 2
    2 3
    3 4
    2 5
    5 6
    

    Sample Output 3

     

    Second
    #include <iostream>
    #include <vector>
    #include <algorithm>
    #include <string>
    #include <set>
    #include <queue>
    #include <map>
    #include <sstream>
    #include <cstdio>
    #include <cstring>
    #include <numeric>
    #include <cmath>
    #include <iomanip>
    #include <deque>
    #include <bitset>
    #include <cassert>
    //#include <unordered_set>
    //#include <unordered_map>
    #define ll              long long
    #define pii             pair<int, int>
    #define rep(i,a,b)      for(int  i=a;i<=b;i++)
    #define dec(i,a,b)      for(int  i=a;i>=b;i--)
    #define forn(i, n)      for(int i = 0; i < int(n); i++)
    using namespace std;
    int dir[4][2] = { { 1,0 },{ 0,1 } ,{ 0,-1 },{ -1,0 } };
    const long long INF = 0x7f7f7f7f7f7f7f7f;
    const int inf = 0x3f3f3f3f;
    const double pi = acos(-1.0);
    const double eps = 1e-6;
    const ll mod = 998244353;
    
    inline ll read()
    {
        ll x = 0; bool f = true; char c = getchar();
        while (c < '0' || c > '9') { if (c == '-') f = false; c = getchar(); }
        while (c >= '0' && c <= '9') x = (x << 1) + (x << 3) + (c ^ 48), c = getchar();
        return f ? x : -x;
    }
    
    ll gcd(ll m, ll n)
    {
        return n == 0 ? m : gcd(n, m % n);
    }
    
    /**********************************************************/
    const int N = 1e5 + 5;
    
    vector<int> g[N];
    bool leaf[N];
    
    
    bool dfs(int x, int fa)
    {
        if (g[x].size() == 1 && g[x][0] == fa)
        {
            leaf[x] = 1;
            return 0;
        }
        int cnt0 = 0;
        for (auto to : g[x])
        {
            if (to != fa)
            {
                if (dfs(to, x))
                    return 1;
                if(leaf[to])
                    cnt0++;
            }
        }
        if (cnt0 > 1)
            return 1;
        if (fa == -1 && cnt0 == 0)
            return 1;
        if (cnt0)
            leaf[x] = 0;
        else
            leaf[x] = 1;
        return 0;
    }
    
    int main()
    {
    #ifdef _DEBUG
        freopen("input.txt", "r", stdin);
        freopen("output.txt", "w", stdout);
    #endif
        int n;
        cin >> n;
        rep(i, 1, n-1)
        {
            int u, v;
            scanf("%d%d",&u,&v);
            g[u].push_back(v);
            g[v].push_back(u);
        }
        int fg = 0;
        rep(i, 1, n)
        {
            if (g[i].size() > 1)
            {
                fg = dfs(i, -1);
                break;
            }
        }
        if (fg)
            puts("First");
        else
            puts("Second");
        return 0;
    }
  • 相关阅读:
    Redis应用场景
    GDAL读取Shp问题解决:Unable to open EPSG support file gcs.csv
    IDEA整合Mybatis+Struts2+Spring (二)--整合框架
    IDEA整合Mybatis+Struts2+Spring(一)--新建项目
    Spring中的单例模式和多例模式的应用
    java设计模式1--单例模式
    HashMap原理以及自己实现HashMap
    JQ之$.ajax()方法以及ajax跨域请求
    数据库简单操作以及多表查询操作的一些总结
    JS正则表达式学习总结
  • 原文地址:https://www.cnblogs.com/dealer/p/13780892.html
Copyright © 2011-2022 走看看