Title Link: http://acm.hdu.edu.cn/showproblem.php?pid=5348
The main idea: to give a picture of the non-directed, ask whether there is a scheme, so that each side given direction, each point of the difference in degrees and out of less than equal to 1. If not present, output-1; exists, the direction is output by the input order of the edges.
For example: If the first side of the u,v is given, then the corresponding m line output 0 represents u->v, the output 1 represents u<-v.
Problem Solving Ideas:
First, it is proved that the diagram can be constructed to meet the requirements.
For a ring, it is obvious that each point in the inside is equal to the out degree. You can then think of the ring as a point.
For a tree, you can divide its sub-nodes so that the difference between the degrees and the degrees is less than or equal to 1.
It can therefore be thought of as dividing a graph into a forest of rings and points. Then there must be a solution.
DFS traversal, first traversing points with an odd number of degrees, because these points must be the starting point or end point. After traversing, delete the traversed edges.
Then traverse the remaining edges.
The assignment to ans[(id>>1) +1] in the code is due to the addition of two forward edges to the adjacency table for each input edge, so that the relationship of the side's ordinal ID to the ordinal ID in the adjacency table is
Id ' >>1=id. Here ID ' is (0,1), (2,3) .... Therefore, the corresponding ordinal number is divided directly by 2. +1 is simply to store it in the ANS array starting from 1. Look at your personal habits.
Was forced to learn the adjacency table usage of the next array. Previously, only the adjacency table of vectors was used.
To node I, Head[i] represents the last added edge with node I as the starting point, initialized to-1.
The Edge J, Pre[j] denotes the number of the previous edge of the same starting point as the Edge J, or the ordinal in the edge array; nxt[j] represents the number of the next edge of the same starting point.
You can use To[j] to represent the end point of the Edge J. You can also use the structure to maintain the relationship between the start and end points.
Cost[j] represents the edge of Edge J.
The code is as follows:
#include <iostream>#include<cstdio>#include<algorithm>#include<cstring>using namespacestd;#defineN 200005#defineM 600005structedge{intu,v; Edge () {} Edge (intUintv): U (U), V (v) {}}edge[m];intHead[n],pre[m],nxt[m],du[n],ans[m>>1],e;intn,m,t,a,b;voidinit () {memset (head,-1,sizeof(head)); memset (NXT,-1,sizeof(NXT)); Memset (Du,0,sizeof(du)); E=0;}voidAddedge (intUintv) {Edge[e]=Edge (U,V); Pre[e]=Head[u]; Head[u]=D; E++; Edge[e]=Edge (V,u); Pre[e]=Head[v]; HEAD[V]=D; E++;}voidDfsintu) { while(head[u]!=-1) {Du[u]--; intid=head[u],v=edge[id].v; if(id&1) ans[(id>>1)+1]=0; Elseans[(id>>1)+1]=1; intpre,nxt; Head[u]=Pre[id]; Pre=Pre[id]; Nxt=Nxt[id]; if(pre!=-1) nxt[pre]=Nxt; if(nxt!=-1) pre[nxt]=Pre; ID^=1; if(head[v]==ID) head[v]=Pre[id]; Pre=Pre[id]; Nxt=Nxt[id]; if(pre!=-1) nxt[pre]=Nxt; if(nxt!=-1) pre[nxt]=Pre; U=v; if(Du[v]) du[v]--; }}intMain () {scanf ("%d",&t); while(t--) {init (); scanf ("%d%d",&n,&m); for(intI=0; i<m;i++) {scanf ("%d%d",&a,&b); Addedge (A, b); Du[a]++,du[b]++; } for(intI=0; i<e;i++)if(pre[i]!=-1) Nxt[pre[i]]=i; for(intI=1; i<=n;i++) Nxt[head[i]]=-1; for(intI=1; i<=n;i++)if(du[i]&1) Dfs (i); for(intI=1; i<=n;i++)if(Du[i]) DFS (i); for(intI=1; i<=m;i++) printf ("%d\n", Ans[i]); } return 0;}
View Code
2015 Multi-school game fifth 1009 (Hdu 5348)