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

Source

给出n个节点m条边 这些边可以是有向可以是无向 现在求这个图中是否存在欧拉回路
首先欧拉回路的定义:(摘抄自kuangbin的blog)一开始当然是判断原图的基图是否连通,若不连通则一定不存在欧拉回路或欧拉路径(不考虑度数为0的点)。

其实,难点在于图中的无向边,需要对所有的无向边定向(指定一个方向,使之变为有向边),使整个图变成一个有向欧拉图(或有向半欧拉图)。若存在一个定向满足此条件,则原图是欧拉图(或半欧拉图)否则不是。关键就是如何定向?

相当于我需要经过图中每条边恰好一次 并且还能够绕回原点 那么经过我们观察可以发现这个满足欧拉回路的话 原图中的所有的度都将是偶数 如果一个图中度存在奇数 那么一定不存在奇数 那么如何定向?
首先可以知道 有向边的带来的度都是确定的,所以我直接累加到相应节点的度里即可
如果我们希望构成一个欧拉回路我需要先针对无向边 任意给他定向 然后并且表示出他们的度来 这时候显然有很多点的度 根本不相同 怎么办我可以调整啊 假如现在我拥有一条s->xx->xxx->t的路径 那么我如果将整条路径完全反向那么是不是只有我s,t的度会改变 并且可以观察到的是s,t改变量一定是偶数 因为进-1出+1 或者反之亦然 中间的改完都是不变的 那么是不是和网络流很像 于是构造网络求解此题 如果一个点入度<出度则源向他连d[i]/2 (表示这个点的入度太少了  我如果有流量经过他的入度就会+1)反之 他向汇连d[i]/2 然后原图中我已经定向过的边权值都为1 表示这条边我只可以反向一回 然后跑一下最大流 看是否满流 就是是否所有点入度都等于出度 然后输出即可

 


elijahqi

退役了 现在在商院 偶尔打CF,有时有ACM regional也去玩一下

发表评论