poj1637 Sightseeing tour

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 should 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 <= 200,1 <= s <= 1000 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

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
3 3
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

Source

 
首先给无向图随意定向,如果某个点初度和入度之差不是2的倍数,就一定没有欧拉回路。
然后舍掉有向边 , 将无向边随意定向相连,容量上限为1 , 对于一个点的入度大于出度我们就把这个点与汇点相连,容量为差/2,
而对于出度小于入度的,就将源点与之相连 ,容量为差/2,(注意容量都用为正)。
然后跑一遍图判断是否满流,满流则有欧拉回路 。
 
  1 #include <iostream>
  2 #include <cstdlib>
  3 #include <cstring>
  4 #include <cstdio>
  5 #include <queue>
  6 const int inf = 1 << 30 , maxm = 20000+ 111 , maxn = 200 + 11 ;
  7 using namespace std ;
  8 queue< int > Q ;
  9 int n , m , s , t , dis[maxn] , cnt , cur[maxn] , head[maxn] , chu[maxn] , ru[maxn] , tot ;
 10 struct id
 11 {
 12     int nxt , to , val ;
 13 } edge[maxm] ;
 14 
 15 inline void Init( )
 16 {
 17     freopen( "poj1637.in" , "r" , stdin ) ;
 18     freopen( "poj1637.out" , "w" , stdout ) ;
 19 }
 20 
 21 int read( )
 22 {
 23     char ch = getchar( ) ; int k = 1 , ret = 0 ;
 24     while( ch < '0' || ch > '9' ) { if( ch == '-' ) k = -1 ; ch = getchar( ) ; }
 25     while( ch >= '0' && ch <= '9' ) { ret = ret * 10 + ch - '0' , ch = getchar( ) ; }
 26     return ret * k ;
 27 }
 28 
 29 void add( int u , int v , int val )
 30 {
 31     edge[++cnt] = id{ head[u] , v , val } ; head[u] = cnt ;
 32 }
 33 
 34 bool judge(  )
 35 {
 36     for( int x = 1 ; x <= n ; ++x )
 37     {
 38         if( (chu[x] - ru[x])&1 ) return false ;
 39     }
 40     return true ;
 41 }
 42 
 43 bool bfs(  )
 44 {
 45     memset( dis , -1 , sizeof(dis) ) ;
 46     dis[s] = 0 ; Q.push( s ) ;
 47     while( !Q.empty( ) )
 48     {
 49         int u = Q.front( ) ; Q.pop( ) ;
 50         for( int i = head[u] ; ~i ; i = edge[i].nxt )
 51         {
 52             int v = edge[i].to ;
 53             if( dis[v] < 0 && edge[i].val > 0 )
 54             {
 55                 dis[v] = dis[u] + 1 ;
 56                 Q.push( v ) ;
 57             }
 58         } 
 59     }
 60     return dis[t] != -1 ;
 61 }
 62 
 63 int dfs( int u , int f )
 64 {
 65     if( u == t ) return f ;
 66     int an , cost = 0 ;
 67     for( int i = cur[u] ; ~i ; i = edge[i].nxt )
 68     {
 69         int v = edge[i].to ;
 70         if( dis[v] != dis[u] + 1 ) continue ;
 71         an = dfs( v , min( f - cost , edge[i].val ) ) ;
 72         edge[i].val -= an , edge[i^1].val += an , cost += an;
 73         if( edge[i].val ) cur[u] = i ;
 74         if( cost == f ) return f ;
 75     }
 76     if( !cost ) dis[u] = -1 ;
 77     return cost ;
 78 }
 79 
 80 int dinic(  )
 81 {
 82     int ans = 0 ;
 83     while( bfs( ) )
 84     {
 85         for( int x = s ; x <= t ; ++x ) cur[x] = head[x] ;
 86         ans += dfs( s , inf ) ;
 87     }
 88     return ans ;
 89 }
 90 
 91 
 92 
 93 void input( )
 94 {
 95     n = read( ) ; m = read( ) ; cnt = -1 ; s = 0 , t = n + 1 ;
 96     int u , v , w ; tot = 0 ;
 97     while( m-- )
 98     {
 99         u = read( ) , v = read( ) , w = read( ) ;
100         chu[u] ++ , ru[v] ++ ;
101         if( !w ) 
102         {
103             add( u , v , 1 ) ;
104             add( v , u , 0 ) ;
105         }
106     }
107     for( int x = 1 ; x <= n ; ++x )
108     {
109         if( ru[x] > chu[x]  ) 
110         {
111             add( x , t , (ru[x] - chu[x]) >> 1 ) ;
112             add( t , x , 0 ) ;
113         } 
114         if( ru[x] < chu[x] )
115         {
116             add( s , x , (chu[x]-ru[x])>>1 ) ;
117             add( x , s , 0 ) ;
118             tot += (chu[x]-ru[x])>>1 ;
119         }
120     }
121     if( !judge( ) ) { puts("impossible") ; return ; }
122     if( tot != dinic( ) ) puts("impossible") ;
123     else puts("possible") ;
124 }
125 
126 int main( )
127 {
128 //    Init( ) ;
129     int t = read( ) ;
130     while( t-- )
131     {
132         memset( head , -1 , sizeof(head) ) ;
133         memset( chu , 0 , sizeof(chu) ) ;
134         memset( ru , 0 , sizeof(ru) ) ;
135         input( ) ;
136     }
137 //    fclose( stdin ) ;
138 //    fclose( stdout ) ;
139     return 0 ;
140 }
原文地址:https://www.cnblogs.com/Ateisti/p/5949068.html