[关闭]
@zzzc18 2017-07-16T13:55:32.000000Z 字数 1890 阅读 600

Regular Contest 078

AtCoder


题目链接

D - Fennec VS. Snuke

Time limit : 2sec / Memory limit : 256MB

Score : 400 points

Problem Statement

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


大意:
一棵树 个点,开始的时候 号点是黑色 ,号点是白色,黑色先手,黑白轮流进行,每次可以选一个树上未染色并与其色块相邻的点染成其对应颜色。如果哪一方最后无法再染色,则输掉

Solution

显然,黑白的争端在于与黑白相邻的链上(不在链上的部分可以以后慢慢搞),举个例子
这是开始的情况
2.PNG-21.7kB

抢链上的部分
3.PNG-21.5kB

最终情况
4.PNG-21.8kB

所以让他们在链上打一架就好了,DFS一边就好了,处理size,链

  1. #include<cstdio>
  2. #include<algorithm>
  3. #define MAXN 300000
  4. using namespace std;
  5. int n,loc;
  6. struct E{
  7. int next,to;
  8. }edge[MAXN<<1];
  9. int head[MAXN],edge_num;
  10. int vis[MAXN],path[MAXN];
  11. int size[MAXN];
  12. void addedge(int x,int y){
  13. edge[++edge_num].next=head[x];
  14. edge[edge_num].to=y;
  15. head[x]=edge_num;
  16. }
  17. void INIT(){
  18. scanf("%d",&n);
  19. int i;
  20. for(i=1;i<n;i++){
  21. int a,b;
  22. scanf("%d%d",&a,&b);
  23. addedge(a,b);
  24. addedge(b,a);
  25. }
  26. }
  27. void DFS(int x,int depth){
  28. path[depth]=x;vis[x]=1;
  29. size[x]=1;
  30. if(x==n){
  31. loc=path[depth/2+1];
  32. }
  33. int i;
  34. for(i=head[x];i;i=edge[i].next){
  35. if(vis[edge[i].to])
  36. continue;
  37. DFS(edge[i].to,depth+1);
  38. size[x]+=size[edge[i].to];
  39. }
  40. }
  41. void solve(){
  42. DFS(1,0);
  43. int sz_f=size[1]-size[loc],sz_s=size[loc];
  44. if(sz_f>sz_s)
  45. printf("Fennec\n");
  46. else
  47. printf("Snuke\n");
  48. }
  49. void DEBUG(){
  50. int i;
  51. printf("Size:");
  52. for(i=1;i<=n;i++)
  53. printf("%d ",size[i]);
  54. }
  55. int main(){
  56. INIT();
  57. solve();
  58. //DEBUG();
  59. return 0;
  60. }
添加新批注
在作者公开此批注前,只有你和作者可见。
回复批注