[poj1637]Sightseeing tour 最大流,欧拉回路判断

Sightseeing tour
Time Limit: 1000MS Memory Limit: 10000K
Total Submissions: 9 Accepted: 4

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

题意:

一张由有向边和无向边组成的图,求是否有一种将无向图改为有向图的方法,使原图存在欧拉回路

先把无向图钦定为有向图

入度减出度为d[i]

如果是奇数,则不可能构成欧拉回路

如何用网络流实现自我调节?

囿于边反向,所以反过来入度减出度的值改变量为2

同时除二,保证符合实际情况(不可能减半条边)

S->i d[i]/2

j->T -d[j]/2

i->j 1

#include<iostream>
#include<cstring>
#include<cstdio>
#define INF 100000000
using namespace std;
const int N = 205;
int last[N],h[N],q[N],d[N],cur[N],TEST,tot,ans,cnt,T,S=0,n,m;
struct Edge{
	int to,next,v;
}e[210005];
void insert( int u, int v, int w ){
	e[++cnt].to = v; e[cnt].next = last[u]; e[cnt].v = w; last[u] = cnt;
	e[++cnt].to = u; e[cnt].next = last[v]; e[cnt].v = 0; last[v] = cnt;
}
bool bfs(){
	memset(h,-1,sizeof(h));
	int tail = 1, head = 0;
	q[0] = S; h[0] = S;
	while( tail != head ){
		int now = q[head++];
		for( int i = last[now]; i; i = e[i].next )
			if( h[e[i].to] == -1 && e[i].v ){
				h[e[i].to] = h[now] + 1;
				q[tail++] = e[i].to;
			}
	}
	return h[T] != -1;
}
int dfs( int x, int f ){
	int w,used=0;
	if( x == T ) return f;
	for( int i = cur[x]; i; i = e[i].next )
		if( h[e[i].to] == h[x] + 1 ){
			w = dfs(e[i].to,min(e[i].v,f-used));
			e[i].v -= w; e[i^1].v += w; used += w;
			if( e[i].v ) cur[x] = i; if( f == used ) return f;
		}
	if( !used ) h[x] = -1;
	return used;
}
void dinic(){
	while( bfs() ){
		for( int i = S; i <= T; i++ ) cur[i] = last[i];
		ans += dfs(S,INF);
	}
}
bool check(){
	for( int i = 1; i <= n; i++ ) if( d[i]&1 ) return false;
	return true;
}
int main(){
	scanf("%d", &TEST);
	while( TEST-- ){
		memset(last,0,sizeof(last)); cnt = 1;
		memset(d,0,sizeof(d)); tot=0; ans=0;
		scanf("%d%d", &n, &m); T = n+1;
		for( int i = 1,u,v,type; i <= m; i++ ){
			scanf("%d%d%d", &u, &v, &type);
			d[u]++; d[v]--; if( !type ) insert( u, v, 1 );
		}
		if(!check()){puts("impossible");continue;}
		for( int i = 1; i <= n; i++ ){
			if( d[i] > 0 ) insert(S,i,d[i]/2),tot+=d[i]/2;
			if( d[i] < 0 ) insert(i,T,-d[i]/2);
		}
		dinic();
		if( tot != ans ) puts("impossible");
		else puts("possible");
	}
	return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值