「POJ1637」Sightseeing tour

2014年12月29日5,3051

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

Sample Output

题解

混合图欧拉回路

设点x的出度-入度为d[x]

对于无向图先任意定向,若任意x,d[x]非偶数则impossible

d[x]/2即为连接每个点的边需要反向的数量

若d[x]/2>0 连S->T 容量d[x]/2

否则d[x]/2<0 连 x->T 容量-d[x]/2

舍弃有向边,将定向后的无向边放进网络流图中

判最大流是否等于∑ d[x]/2

没有理解建图可参照网络流建模总结

http://wenku.baidu.com/link?url=Eu-odIezr8ArWPR0MZ55vSL9dENzUhesNZpdrt5mc0ouQpBe_sFiy3lBu9dJV-DzsUHbs_eX2LX8zMLhUZ7F8L3gV0xPfYMTN_MWIT_K3gK

 

avatar
1 Comment threads
0 Thread replies
0 Followers
 
Most reacted comment
Hottest comment thread
1 Comment authors
rrr Recent comment authors
  Subscribe  
提醒
rrr
rrr

jud()放在建图的前面,更快一些