【最大流+混合图欧拉回路】POJ-1637 Sightseeing tour

来源:互联网 发布:浏览器数据分析案例 编辑:程序博客网 时间:2024/05/01 08:23
Sightseeing tour
Time Limit: 1000MS Memory Limit: 10000K   

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

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
————————————————————灰心的分割线————————————————————
前言:关于混合图的欧拉回路问题,夏天的风解释得很详细。
思路:有向边不必理会,因为是固定的。对于无向边,暂时将其当做单向边,统计出每个点的度数。
一旦某个点是奇度点,一定不存在欧拉回路。因为该点不可能通过调整边的方向使得入度 = 出度。
如果不存在奇度点,那么用网络流来判断能否通过修改边得到欧拉回路。
对于之前人为定向的单向边,容量为1,表示可以修改一个出度。
对于出度 > 入度的点,将源点与它相连,容量为可以减少的出度。((出度 - 入度) / 2)
对于入度 > 出度的点,将它与汇点相连,容量为可以增加的出度。((入度 - 出度) / 2)
这样一来,一旦可以满流(流入 = 流出),说明所有需要修改的度都修改了。入度 = 出度。也就是说存在欧拉回路。
代码如下:
/*ID: j.sure.1PROG:LANG: C++*//****************************************/#include <cstdio>#include <cstdlib>#include <cstring>#include <algorithm>#include <cmath>#include <stack>#include <queue>#include <vector>#include <map>#include <string>#include <climits>#include <iostream>#define LL long longusing namespace std;const int INF = 0x3f3f3f3f;/****************************************/const int N = 222, M = 1e4;struct Node {int u, v, cap;int next;}edge[M];int tot, head[N], lev[N], cur[N], q[N], s[N], ind[N], oud[N], dif[N], dir[N], n, m, S, T;bool vis[N];void init(){tot = 0; memset(head, -1, sizeof(head));}void add(int u, int v, int c){edge[tot].u = u; edge[tot].v = v; edge[tot].cap = c;edge[tot].next = head[u]; head[u] = tot++;}bool judge(){for(int i = 1; i <= n; i++) if(vis[i]) {if((oud[i] + ind[i]) & 1) return false;dif[i] = (oud[i] - ind[i]) >> 1;}return true;}bool bfs(){memset(lev, -1, sizeof(lev));int fron = 0, rear = 0;q[rear++] = S; lev[S] = 0;while(fron < rear) {int u = q[fron%N]; fron++;for(int i = head[u]; i != -1; i = edge[i].next) {int v = edge[i].v;if(edge[i].cap && lev[v] == -1) {lev[v] = lev[u] + 1;q[rear%N] = v; rear++;if(v == T) return true;}}}return false;}int Dinic(){int ret = 0;while(bfs()) {memcpy(cur, head, sizeof(head));int top = 0, u = S;while(1) {if(u == T) {int mini = INF, loc;for(int i = 0; i < top; i++) {if(mini > edge[s[i]].cap) {mini = edge[s[i]].cap; loc = i;}}for(int i = 0; i < top; i++) {edge[s[i]].cap -= mini;edge[s[i]^1].cap += mini;}ret += mini;top = loc;u = edge[s[top]].u;}int &i = cur[u];for(; i != -1; i = edge[i].next) {int v = edge[i].v;if(edge[i].cap && lev[v] == lev[u] + 1) break;}if(i != -1) {s[top++] = i;//新的允许弧入栈u = edge[i].v;}else {if(!top) break;lev[u] = -1;//阻塞流u = edge[s[--top]].u;}}}return ret;}int main(){#ifdef J_Sure//freopen("000.in", "r", stdin);//freopen(".out", "w", stdout);#endifint kase;scanf("%d", &kase);while(kase--) {memset(ind, 0, sizeof(ind));memset(oud, 0, sizeof(oud));memset(vis, 0, sizeof(vis));scanf("%d%d", &n, &m);S = 0; T = n+1;int u, v, dir;init();for(int i = 1; i <= m; i++) {scanf("%d%d%d", &u, &v, &dir);vis[u] = vis[v] = 1;oud[u]++; ind[v]++;if(!dir) add(u, v, 1), add(v, u, 0);}bool flag = judge();if(flag) {int max_flow = 0;for(int i = 1; i <= n; i++) if(vis[i]) {if(oud[i] > ind[i]) {//对于出大于入的点,可以提供一个修改边add(S, i, dif[i]); add(i, S, 0);max_flow += dif[i];}else if(ind[i] > oud[i]) {add(i, T, -dif[i]); add(T, i, 0);}}int ret = Dinic();if(ret == max_flow) puts("possible");else puts("impossible"); }else {puts("impossible");}}return 0;}


0 0