Describe
The City executive board in Lund wants-construct a sightseeing tour by bus in Lund, so-tourists can see every corn Er of the beautiful city. They want to construct, the tour so, every street in the city is visited exactly once. The bus should also start and end at the same junction. As in any city, the streets is either one-way or two-way, and traffic rules that must is obeyed by the tour bus. Help the Executive Board and determine if it's possible to construct a sightseeing tour under these constraints.
Input
On the first line of the input was a single positive integer n, telling the number of the test scenarios to follow. Each scenario begins with a line containing the positive integers m and s, 1 <= m <= 200,1 <= s <= The number of junctions and streets, respectively. The following S lines contain the streets. Each street was described with three integers, Xi, Yi, and Di, 1 <= xi,yi <= m, 0 <= di <= 1, where Xi and Yi a Re the junctions connected by a street. If Di=1, then the street was a one-way street (going from Xi to Yi), otherwise it ' s a two-way street. Assume that there exists a junction from the where all other junctions can be reached.
Output
For each scenario, output one line containing the text "possible" or "impossible", whether or not it's possible to Constru CT a sightseeing tour.
Sample input
4
5 8
2 1 0
1 3 0
4 1 1
1 5 0
5 4 1
3 4 0
4 2 1
2 2 0
4 4
1 2 1
2 3 0
3 4 0
1 4 1
7 ·
1 2 0
2 3 0
3 2 0
3 4
1 2 0
2 3 1
1 2 0
3 2 0
Sample output
Possible
Impossible
Impossible
Possible
Test instructions
M-point, S-bar, ask if there is a Euler loop
Exercises
Euler Circuit of network flow judgment mixed Road
The key is to turn the graph into a map and then judge
It is easy to know if it is the Euler loop, then all points of the in, equal to out of the degree out
No forward Edge (U,V), a forward edge (u,v), or a forward edge (V,u), in[i]-out[i] can be found unchanged
Then we can assume that the non-u,v (u,v) becomes the forward edge
Statistics of all points in the degree
If present I, make ABS (In[i]-out[i])%2==1 so the figure does not exist Euler circuit
For In[i]>out[i] points, indicating that I point requires more outgoing traffic, build edge (s,i) flow (In[i]-out[i])/2
For In[i]<out[i] points, indicating that I point requires more inflow of traffic, build edge (i,t) flow (Out[i]-in[i])/2
For all non-forward edges (u,v), Build Edge (u,v) Flow 1
The maximum flow of a running s->t, if full flow is a method by changing the direction of the non-directional edge of each point into the degree = out (is not similar to the upper and lower bounds feasible flow has a solution problem)
Code
1#include <stdio.h>2#include <string.h>3#include <algorithm>4 using namespacestd;5 6 Const intmaxn=1e5+5;7 Const intmaxm=2e5+5;8 Const intinf=0x3f3f3f3f;9 Ten intTo[maxm],cap[maxm],next[maxm],tote; One intfir[maxn],gap[maxn],cur[maxn],d[maxn],q[400000]; A intn,m,s,t; - - voidAddintUintVintcap) the { - //printf ("i=%d%d%d%d\n", tote,u,v,cap); -to[tote]=v; -cap[tote]=cap; +next[tote]=Fir[u]; -fir[u]=tote++; + Ato[tote]=u; atcap[tote]=0; -next[tote]=Fir[v]; -fir[v]=tote++; - } - voidBFS () - { inMemset (Gap,0,sizeofgap); -memset (D,0,sizeofd); to++gap[d[t]=1]; + for(intI=1; i<=n;++i) cur[i]=Fir[i]; - intHead=1, tail=1; theq[1]=T; * while(head<=tail) $ {Panax Notoginseng intu=q[head++]; - for(intv=fir[u];v!=-1; v=Next[v]) the if(!D[to[v]]) +++gap[d[to[v]]=d[u]+1],q[++tail]=To[v]; A } the } + intDfsintUintFL) - { $ if(u==t)returnFL; $ intflow=0; - for(int&v=cur[u];v!=-1; v=Next[v]) - if(cap[v]&&d[u]==d[to[v]]+1) the { - intmin=Dfs (To[v],min (fl,cap[v));Wuyiflow+=min,fl-=min,cap[v]-=min,cap[v^1]+=Min; the if(!FL)returnflow; - } Wu if(! (--gap[d[u])) d[s]=n+1; -++gap[++d[u]],cur[u]=Fir[u]; About returnflow; $ } - intISAP () - { - BFS (); A intret=0; + while(d[s]<=n) ret+=DFS (s,inf); the returnret; - } $ voidInit () the { theTote=0; thememset (fir,-1,sizeofFIR); the } - intMain () in { the intn,u,v,op,s,_; thescanf"%d",&_); About while(_--) the { the int inch[205]={0}, out[205]={0}; the init (); +scanf"%d%d",&n,&m); -s=n+1, t=s+1, n=T; the for(intI=1; i<=m;i++)Bayi { thescanf"%d%d%d",&u,&v,&op); the inch[V]++, out[u]++; - if(op==0) -Add (U,v,1); the } the intflag=1; the for(intI=1; i<=n;i++) the if(( out[i]-inch[i]) %2==1) - { theflag=0; the Break; the }94 if(!flag) the { theprintf"impossible\n"); the Continue;98 } About intsum=0; - for(intI=1; i<=n;i++)101 {102 if(inch[i]> out[i])103 {104sum+= (inch[i]- out[i]) /2; theAdd (I,t, (inch[i]- out[i]) /2);106 }107 Else if( out[i]>inch[i])108Add (S,i, ( out[i]-inch[i]) /2);109 } theprintf"%s\n", ISAP () ==sum?"possible":"Impossible");111 } the return 0;113}
TOJ 2099 Sightseeing Tour (network flow judgment mixed graph)