[圖論][BFS]Fennec VS. Snuke
阿新 • • 發佈:2018-07-08
-- const term urn pty for mes 輸入 sel
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.
N
a1 b1
:
aN−1 bN−1
題目描述
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.
Fennec: selects an uncolored cell that is adjacent to a black cell, and paints it black.
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: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\n"); else printf("Fennec\n"); return 0; }
[圖論][BFS]Fennec VS. Snuke