Poj1637 sightseeing tour hybrid graph's Euler Loop + maximum flow

Source: Internet
Author: User
The Euler's loop Judgment Method of the hybrid graph is described in the blacklist. The method used here: orientation the undirected edge first, and then calculate the inbound and outbound degrees of each vertex. If there is a single vertex, the only difference is an odd number, this graph does not have an Euler Loop (the degree of each point in the Euler loop of the directed graph is an even number. As for outbound = inbound, we will adjust the maximum flow ). After all the results are determined, create a graph. 1: Build an arc with a capacity of 1 for each undirected edge. 2: The difference between the inbound and outbound degrees is not 0: If the inbound mode is exceeded, the connected capacity of the Change Point and the source point is 2. If inbound> outbound, the vertex is connected to the sink point, and the capacity is the difference between the inbound and outbound degrees/2. Find the maximum stream. If the stream is full, the occasional loop sightseeing tour exists.
Time limit:1000 ms   Memory limit:10000 K
Total submissions:5792   Accepted:2357

Description

The City Executive Board in Lund wants to construct a sightseeing tour by bus in Lund, so that tourists can see every corner of the beautiful city. they want to construct the tour so that every street in the city is visited exactly
Once. the bus shoshould also start and end at the same junction. as in any city, the streets are either one-way or two-way, traffic rules that must be 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 is a single positive integer N, telling the number of test scenarios to follow. each scenario begins with a line containing two positive integers m and S, 1 <= m <= 1000 ,1 <= S <= being the number
Of junctions and streets, respectively. the following s lines contain the streets. each street is described with three integers, XI, Yi, and Di, 1 <= xi, Yi <= m, 0 <= di <= 1, where XI and Yi are the junctions connected by a street. if DI = 1, then the street
Is a one-way street (going from Xi to Yi), otherwise it's a two-way street. you may assume that there exists a junction from 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 construct 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
#include<iostream>#include<algorithm>#include<cstdio>#include<cmath>#include<cstring>#include<string>using namespace std;#define MAXN 255#define INF 0xFFFFFFstruct edge{int to,c,next;};struct edge2{int u,v,ty;};edge e[99999];edge2 ee[99999];int deg[MAXN]; int que[MAXN*100],dis[MAXN],pre[MAXN];int head[MAXN],head2[MAXN];int st,ed,maxflow;int en,n,m;void add(int a,int b,int c){e[en].to=b;e[en].c=c;e[en].next=head[a];head[a]=en++;e[en].to=a;e[en].c=0;e[en].next=head[b];head[b]=en++;} bool bfs() {memset(dis,-1,sizeof(dis));que[0]=st,dis[st]=1;int t=1,f=0;while(f<t){int j=que[f++];for(int k=head[j];k!=-1;k=e[k].next){int i=e[k].to;if(dis[i]==-1 && e[k].c){que[t++]=i;dis[i]=dis[j]+1;if(i==ed) return true;}}}return false; } int update()  {  int p,flow=INF;      for (int i=pre[ed];i!=-1;i=pre[i])if(e[head2[i]].c<flow) p=i,flow=e[head2[i]].c;        for (int i=pre[ed];i!=-1;i=pre[i]) e[head2[i]].c-=flow,e[head2[i]^1].c+=flow;       maxflow+=flow;     return p;}  void dfs() {  memset(pre,-1,sizeof(pre));memcpy(head2,head,sizeof(head2));      for(int i=st,j;i!=-1;)      {          int flag=false;          for(int k=head[i];k!=-1;k=e[k].next)              if(e[k].c && (dis[j=e[k].to]==dis[i]+1) )            {                  pre[j]=i; head2[i]=k;  i=j; flag=true;                  if(i==ed) i=update();                  if(flag) break;            }          if (!flag) dis[i]=-1,i=pre[i];       }  } int build(){int temp=0;memset(head,-1,sizeof(head));for(int i=1;i<=n;i++){if(deg[i]){if(deg[i]>0)add(i,ed,deg[i]);else{add(st,i,abs(deg[i]));temp+=deg[i];}}}for(int i=1;i<=m;i++)if(!ee[i].ty) add(ee[i].u,ee[i].v,1);return temp;}void solve(){int a,b,c;scanf("%d%d",&n,&m);memset(deg,0,sizeof(deg));st=0,ed=1+n;for(int i=1;i<=m;i++){scanf("%d%d%d",&a,&b,&c);deg[a]--;deg[b]++;ee[i].u=a;ee[i].v=b;ee[i].ty=c;}for(int i=1;i<=n;i++){if(deg[i]%2!=0){printf("impossible\n");return;}deg[i]/=2;}int fuflow=abs(build());maxflow=0;while(bfs()) dfs();if(maxflow==fuflow)printf("possible\n");elseprintf("impossible\n");}int main(){int t;scanf("%d",&t);while(t--) solve();return 0;} 

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.