zoukankan      html  css  js  c++  java
  • [图论][BFS]Fennec VS. Snuke

    题目描述

    Fennec and Snuke are playing a board game.
    On the board, there are N cells numbered 1 through N, and N−1 roads, each connecting two cells. Cell ai is adjacent to Cell bi through the i-th road. Every cell can be reached from every other cell by repeatedly traveling to an adjacent cell. In terms of graph theory, the graph formed by the cells and the roads is a tree.
    Initially, Cell 1 is painted black, and Cell N is painted white. The other cells are not yet colored. Fennec (who goes first) and Snuke (who goes second) alternately paint an uncolored cell. More specifically, each player performs the following action in her/his turn:
    Fennec: selects an uncolored cell that is adjacent to a black cell, and paints it black.
    Snuke: selects an uncolored cell that is adjacent to a white cell, and paints it white.
    A player loses when she/he cannot paint a cell. Determine the winner of the game when Fennec and Snuke play optimally.

    Constraints
    2≤N≤105
    1≤ai,bi≤N
    The given graph is a tree.

    输入

    Input is given from Standard Input in the following format:
    N
    a1 b1
    :
    aN−1 bN−1

    输出

    If Fennec wins, print Fennec; if Snuke wins, print Snuke.

    样例输入

    7
    3 6
    1 2
    3 1
    7 4
    5 7
    1 4
    

    样例输出

    Fennec
    

    提示

    For example, if Fennec first paints Cell 2 black, she will win regardless of Snuke's moves.

    思路:思路很容易想,关键是我忘了怎么用vector来构建邻接表了!!

    AC代码:

    #include <iostream>
    #include<cstdio>
    #include<vector>
    #include<queue>
    #include<cstring>
    using namespace std;
    
    vector<int> edge[100005];//相当于二维数组int edge[100005][可变长];
    queue<int> q;
    int vis[100005];
    int step[100005];
    int pre[100005];
    int flag[100005];
    int n;
    int x=1,y=1;
    
    int bfs1(){//目的是找到从1到n的那条路
       vis[1]=1; step[1]=0; pre[1]=0; q.push(1);
       while(!q.empty()){
         int head=q.front(); q.pop();
         if(head==n) return step[head];
         for(int i=0;i<(int)edge[head].size();i++){
            int nxt=edge[head][i];
            if(vis[nxt]) continue;
            step[nxt]=step[head]+1;
            vis[nxt]=1;
            pre[nxt]=head;
            q.push(nxt);
         }
       }
       return -1;
    }
    
    void bfs2(){//目的是求出在双方最佳策略下,black和white的数量
       while(!q.empty()) q.pop();
       memset(vis,0,sizeof(vis));
       vis[1]=1; q.push(1);
       while(!q.empty()){
         int head=q.front(); q.pop();
         for(int i=0;i<(int)edge[head].size();i++){
            int nxt=edge[head][i];
            if(vis[nxt]||flag[nxt]==2||nxt==n) continue;
            vis[nxt]=1; x++;
            q.push(nxt);
         }
       }
    }
    
    int main()
    {
        scanf("%d",&n);
        for(int i=1;i<=n-1;i++){//邻接表存图
            int a,b;
            scanf("%d%d",&a,&b);
            edge[a].push_back(b);
            edge[b].push_back(a);
        }
        int tmp=bfs1(); tmp--,tmp/=2;//tmp表示在1到n的线路上有几个white
        int now=pre[n]; int cnt=0;
        while(pre[now]!=0){//对线路上的点进行标记——flag[i]=1表示该点black,flag[i]=2表示该点white
            flag[now]=2; cnt++;
            if(cnt>tmp) flag[now]=1;
            now=pre[now];
        }
        bfs2();
        y=n-x;
        if(x<=y) printf("Snuke
    ");
        else printf("Fennec
    ");
        return 0;
    }
    转载请注明出处:https://www.cnblogs.com/lllxq/
  • 相关阅读:
    glib 库 hash table 使用
    git 使用
    centos6.5 下安装 sqlplus
    Oracle tns 协议
    unix环境高级编程附录 B 通用代码
    centos6.5安装gtk开发环境
    linux 下定位程序假死
    Kotlin与Android SDK 集成(KAD 05)
    Kotlin的android扩展:对findViewById说再见(KAD 04)
    Kotlin类:功能更强、而更简洁(KAD 03)
  • 原文地址:https://www.cnblogs.com/lllxq/p/9280959.html
Copyright © 2011-2022 走看看