poj3710 Christmas Game 无向图 tarjan 连通分量

Christmas Game
Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 1669   Accepted: 517

Description

Harry and Sally were playing games at Christmas Eve. They drew some Christmas trees on a paper:

poj3710 Christmas Game 无向图 tarjan 连通分量_第1张图片

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:

poj3710 Christmas Game 无向图 tarjan 连通分量_第2张图片

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:

poj3710 Christmas Game 无向图 tarjan 连通分量_第3张图片

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:

poj3710 Christmas Game 无向图 tarjan 连通分量_第4张图片

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;
}


你可能感兴趣的:(poj3710 Christmas Game 无向图 tarjan 连通分量)