poj3710 Christmas Game 无向图 tarjan 连通分量
2013-10-04 10:56
316 查看
Christmas Game
Description
Harry and Sally were playing games at Christmas Eve. They drew some Christmas trees on a paper:
Then they took turns to cut a branch of a tree, and removed the part of the tree which had already not connected with the root. A step shows as follows:
Sally always moved first. Who removed the last part of the trees would win the game.
After a while, they all figured out the best strategy and thought the game was too simple for them. Harry said, “The Christmas trees should have some gifts in them!” So Sally drew some gifts (simple polygons) in the initial trees:
You may assume the initial picture is a tree with some simple polygons, in which each edge is involved in at most one polygon. We also know that every polygon has only one node involved in the main tree (the hanging point of the giftJ) .In every sub-tree
(connected subgraph), there was one and only one node representing the “root”. According to these assumptions, following graphs will never appear:
Sally and Harry took turns (Sally was always the first person to move), to cut an edge in the graph, and removed the part of the tree that no longer connected to the root. The person who cannot make a move lost the game.
Your job is to decide who will finally win the game if both of them use the best strategy.
Input
The input file contains multiply test cases.
The first line of each test case is an integer N (N<100), which represents the number of sub-trees. The following lines show the structure of the trees. The first line of the description of a tree is the number of the nodesm (m<100)
and the number of the edges k (k<500). The nodes of a tree are numbered from 1 tom. Each of following lines contains 2 integers
a and b representing an edge <a,b>. Node 1 is always the root.
Output
For each test case, output the name of the winner.
Sample Input
Sample Output
Time Limit: 1000MS | Memory Limit: 65536K | |
Total Submissions: 1669 | Accepted: 517 |
Harry and Sally were playing games at Christmas Eve. They drew some Christmas trees on a paper:
Then they took turns to cut a branch of a tree, and removed the part of the tree which had already not connected with the root. A step shows as follows:
Sally always moved first. Who removed the last part of the trees would win the game.
After a while, they all figured out the best strategy and thought the game was too simple for them. Harry said, “The Christmas trees should have some gifts in them!” So Sally drew some gifts (simple polygons) in the initial trees:
You may assume the initial picture is a tree with some simple polygons, in which each edge is involved in at most one polygon. We also know that every polygon has only one node involved in the main tree (the hanging point of the giftJ) .In every sub-tree
(connected subgraph), there was one and only one node representing the “root”. According to these assumptions, following graphs will never appear:
Sally and Harry took turns (Sally was always the first person to move), to cut an edge in the graph, and removed the part of the tree that no longer connected to the root. The person who cannot make a move lost the game.
Your job is to decide who will finally win the game if both of them use the best strategy.
Input
The input file contains multiply test cases.
The first line of each test case is an integer N (N<100), which represents the number of sub-trees. The following lines show the structure of the trees. The first line of the description of a tree is the number of the nodesm (m<100)
and the number of the edges k (k<500). The nodes of a tree are numbered from 1 tom. Each of following lines contains 2 integers
a and b representing an edge <a,b>. Node 1 is always the root.
Output
For each test case, output the name of the winner.
Sample Input
2 2 1 1 2 4 4 1 2 2 3 2 4 3 4
Sample Output
Sally
删边游戏的升级版,我们会发现,拥有奇数条边的环可简化为一条边,偶数条边的环可简化为一个节点!这样,我们用tarjan缩点,去掉环,再就是形成了几个树,用异或和就可以了!
#include <iostream> #include <stdio.h> #include <string.h> #include <vector> using namespace std; #define M 105 #define mem(a,b) memset(a,b,sizeof(a)) int stack[100000],stop,insta[M],dfn[M],vis[M],low[M],clock_m,map[M][M],n; vector<int >vec[M]; int init(){ mem(dfn,0);mem(insta,0);mem(vis,0);mem(low,0);mem(map,0);clock_m=0;stop=0; for(int i=0;i<=n;i++)vec[i].clear(); } int tarjan(int x,int pre){ dfn[x]=low[x]=++clock_m; insta[x]=1;stack[stop++]=x; for(int i=0;i<vec[x].size();i++){ int cnt=vec[x][i]; if(pre==cnt){ if(map[x][cnt]>1&&map[x][cnt]%2==0){ vis[cnt]=0; } continue; } if(!dfn[cnt]){ tarjan(cnt,x); low[x]=min(low[x],low[cnt]); } else { low[x]=min(low[x],dfn[cnt]); } } if(dfn[x]==low[x]){ int k=0; do{ vis[stack[--stop]]=1; k++; }while(stack[stop]!=x); vis[x]=0; if(k&1){ vis[stack[stop+1]]=0; } } } int getsg(int x,int pre){ int ans=0; for(int i=0;i<vec[x].size();i++){ int cnt=vec[x][i]; if(!vis[cnt]&&pre!=cnt){ ans^=1+getsg(cnt,x); } } return ans; } int main() { int tcase,k,ans,s,e; while(scanf("%d",&tcase)!=EOF){ ans=0; while(tcase--){ init(); scanf("%d%d",&n,&k); for(int i=0;i<k;i++){ scanf("%d%d",&s,&e); vec[s].push_back(e); vec[e].push_back(s); map[s][e]=++map[e][s]; } tarjan(1,-1); ans^=getsg(1,-1); } if(ans)printf("Sally\n"); else printf("Harry\n"); } return 0; }
相关文章推荐
- Tarjan(无向图)有向图缩环/求(边双)强连通分量
- Tarjan求强连通分量
- 有向图强连通分量Tarjan模板
- HDU 4612 Warm up(无向图强连通分量缩点建树)
- 由 USACO2003 Popular Cows[受欢迎的奶牛] 认识 Tarjan 求强连通分量并缩点
- 【tarjan双连通求割点&连通分量】POJ 1523
- PAT 甲级 1021. Deepest Root(dfs:无向图的最远路径、连通分量个数)
- [poj 2186]Popular Cows[Tarjan强连通分量]
- bzoj 1051 tarjan强连通分量
- 51nod 1456 小K的技术(Tarjan强连通分量缩点,并查集)
- POJ1523(tarjan割点后,连通分量有多少)
- 【图论】【强连通分量】【Tarjan】pascal+邻接表
- Tarjan求解有向图强连通分量的线性时间的算法
- 强连通分量(Tarjan、Kosaraju)
- BZOJ2140: 稳定婚姻 Tarjan求无向图强联通分量
- poj 2186 Popular Cows(Tarjan,强连通分量缩点)
- tarjan板子(割点割边连通分量)
- 无向图边连通分量(加边使其成为边连通分量)poj3352
- POJ 1523 SPF(无向图连通分量求割点)
- 图论算法(6) --- Tarjan算法求强连通分量