"POJ1637" Sightseeing Tour

Source: Internet
Author: User
Tags in degrees

Description

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

45 82 1 01 3 04 1 11 5 05 4 13 4 04 2 12 2 04 41 2 12 3 03 4 01 4 13 31 2 02 3 03 2 03 41 2 02 3 11 2 03 2 0

Sample Output

Possibleimpossibleimpossiblepossible

Test instructions

This paper gives a diagram of two-way edge and two-way edge, which makes the graph form the Euler loop and asks whether there is a feasible scheme.

Analysis:

For the non-directional side, we do not know the direction, just arbitrarily directed, and then think about how to modify.

After the completion of the direction, the degree and the degree of all points, if any one point out and the difference in degrees is odd, then the graph will not constitute the Euler circuit.

(First, assume that a point of the degree and the degrees are out and in, if the difference between the degree and the degree of |out-in| is odd, then a is connected to the edge of the point to change the direction, b from this point to the outer edge of the change direction, then its penetration into the in-a+b, the degree into the Out-b+a, The difference between the degree and the |out-b+a-in+a-b|=|out-in+2a-2b| is still odd, so in any case the side redirection, the diagram can not constitute a Euler circuit. )

For a point in the diagram, if the in>out, it is necessary to have (In-out)/2 out side need to change direction, if in<out, it is necessary to have (out-in)/2 in the edge need to change direction. For a non-directional edge (U,V), which is defined as U->V, it is likely to be changed to V->u.

The remaining problems can be solved with the maximum flow, for each in>out node, from the source to the point where a flow is (in-out)/2 of the edge, for each in<out point, from this point to the sink to connect a flow of (out-in)/2 edge. For each non-directional edge that is directed to U->v, a side with a flow of 1 is connected from V to U, and the edge is flowing, indicating that (U,V) is changed direction.

Run the maximum flow, if the maximum flow is equal to the sum of the traffic to the edge connected to the sink, then it is indicated that after some of the edges are redirected, all the points and degrees are equal, that is, the Euler circuit can be formed.

Code:

1#include <cstdio>2#include <cstring>3 4 Const intMAXN = -;5 Const intMAXM = the;6 7 intET[MAXM], EP[MAXM], EF[MAXM];8 intLAST[MAXN], en;9 intp1, p2, dir;Ten intt, N, M, STA, end, ans, ind[maxn]; One  AInlinevoidInsertint  from,intTo,intflow) - { -en++; theEt[en] =to ; -Ef[en] =flow; -Ep[en] = last[ from]; -last[ from] =en; +en++; -Et[en] = from; +Ef[en] =0; AEp[en] =Last[to]; atLast[to] =en; - } -  -InlineintMinintAintb) - { -     returnA < b?a:b; in } -  to intGAP[MAXN], DIS[MAXN], CUR[MAXN]; +  - intSapintNowintflow) the { *     if(now = =end) $     {Panax Notoginseng         returnflow; -     } the     intRET =0, TMP; +      for(inte = Cur[now]; E Cur[now] = e =Ep[e]) A     { the         if(Et[e] >0&& Dis[now] = = Dis[et[e]] +1) +         { -TMP = SAP (Et[e], min (Flow-ret, ef[e])); $RET + =tmp; $Ef[e]-=tmp; -Ef[e ^1] +=tmp; -             if(ret = =flow) the             { -                 returnret;Wuyi             } the         } -     } WuCur[now] =Last[now]; -     if(--gap[dis[now]] <=0) About     { $Dis[sta] = end +1; -     } -++gap[++Dis[now]]; -     returnret; A } +  the intMaxflow () - { $Memset (Gap,0,sizeof(GAP)); thememset (DIS,0,sizeof(DIS)); the     intFlow =0; thegap[0] =end; the      for(inti =1; I <= end; i++) -     { inCur[i] =Last[i]; the     } the      while(Dis[sta] <=end) About     { theFlow + = SAP (STA,2147483647); the     } the     returnflow; + } -  the intMain ()Bayi { thescanf"%d", &t); the      while(t--) -     { -Memset (Last,0,sizeof(last)); thememset (Ind,0,sizeof(Ind)); theEn =1; theAns =0; thescanf"%d%d", &n, &m); -STA = n +1; theEnd = STA +1; the          for(inti =0; I < m; i++) the         {94scanf"%d%d%d", &p1, &AMP;P2, &dir); theind[p1]--; theind[p2]++; the             if(!dir)98             { AboutInsert (P2, p1,1); -             }101         }102         BOOLFlag =1;103          for(inti =1; I <= N; i++)104         { the             if(Ind[i]%2>0)106             {107Flag =0;108                  Break;109             } the             if(Ind[i] >0)111             { theInsert (STA, I, ind[i]/2);113             } the             if(Ind[i] <0) the             { theInsert (I, end,-ind[i]/2);117Ans + =-ind[i]/2;118             }119         } -         if(Flag = =0)121         {122Puts"Impossible");123         }124         Else the         {126Puts (maxflow () = = ans?"possible":"Impossible");127         } -     }129}

"POJ1637" Sightseeing Tour

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.