[POJ3710] Christmas Game (Game tree Delete edge game) __ the puzzle

Source: Internet
Author: User
Topic Description

Transmission Door
There are n local unicom map. Harry and Sally take turns deleting an edge from the picture, and the part that does not connect to the root node is removed. Sally is a tempo. The graph is obtained by adding some edges from the underlying tree. All formed loops are guaranteed to not share edges, and only have a common point with the underlying tree. Who can't move who loses. Solving

In game theory, a typical tree pruning game and the method of dealing with the ring.
Because there are 2 rings of the size, pay special attention when dealing with them. I just because of this problem pit for half a day, wrote several versions of the code ...
DFS and Tarjan can do code .

#include <iostream> #include <cstring> #include <cstdio> using namespace std;
#define N-T,n,m,x,y,top,ans int;
int tot,point[n],nxt[n*10],v[n*10];

int sg[n],stack[n],vis[n];
    void Clear () {n=m=x=y=top=0;
    Tot=0;memset (point) (point,0,sizeof);
memset (sg,0,sizeof (SG)); memset (vis,-1,sizeof (VIS)); } void Add (int x,int y) {++tot; nxt[tot]=point[x]; point[x]=tot; v[tot]=y;} void dfs (int x,int fa) {stack[++top
    ]=x;vis[x]=1;
    BOOL Flag=false;
            for (int i=point[x];i;i=nxt[i]) {if (V[i]==fa&&!flag) {flag=true;
        Continue
            } if (vis[v[i]]==1) {int now=stack[top],cnt=1;
                while (Now!=v[i]) {cnt++;
                vis[now]=0;
            Now=stack[--top];
        } if (cnt%2) sg[v[i]]^=1;
            else if (vis[v[i]]==-1) {DFS (v[i],x); if (Vis[v[i]]) sg[x]^= (Sg[v[i])+1);
} if (vis[x])--top;
        int main () {while (~SCANF ("%d", &t)) {ans=0;
            while (t--) {clear ();
            scanf ("%d%d", &n,&m);
                for (int i=1;i<=m;++i) {scanf ("%d%d", &x,&y);
            Add (x,y); add (y,x);
            DFS (1,0);
        ANS^=SG[1];
        if (ans) puts ("Sally");
    Else puts ("Harry"); }
}
Summary

① don't forget to leave the stack

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.