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