poj--1637--Sightseeing tour(网络流,最大流判断混合图是否存在欧拉图)

Time Limit: 1000MS   Memory Limit: 10000KB   64bit IO Format: %I64d & %I64u

Status

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

根据欧拉图的性质,如果一个图是欧拉图,那么每一个点的入度出度一定相等,如果之和就是偶数,入度出度只差也应该是偶数,如果差不是偶数的话,那么一定不是欧拉图
差k是偶数的话就可以通过最大流对无向边确定方向,只需要将多出来的度数k/2变换方向,就可使将图变成欧拉图,通过最大流实现多余的度数重新分配,最后判断是否满流,
如果满流欧拉图就可以建成

【建模方法】
把该图的无向边随便定向,计算每个点的入度和出度。如果有某个点出入度
之差为奇数,那么肯定不存在欧拉回路。因为欧拉回路要求每点入度 = 出度,
也就是总度数为偶数,存在奇数度点必不能有欧拉回路。
好了,现在每个点入度和出度之差均为偶数。那么将这个偶数除以 2,得 x。
也就是说,对于每一个点,只要将 x 条边改变方向(入>出就是变入,出>入就是
9
变出),就能保证出=入。如果每个点都是出=入,那么很明显,该图就存在欧拉
回路。
现在的问题就变成了:我该改变哪些边,可以让每个点出=入?构造网络流
模型。首先,有向边是不能改变方向的,要之无用,删。一开始不是把无向边定
向了吗?定的是什么向,就把网络构建成什么样,边长容量上限 1。另新建 s 和
t。对于入>出的点 u,连接边(u, t)、容量为 x,对于出>入的点 v,连接边(s, v),
容量为 x(注意对不同的点 x 不同)。之后,察看是否有满流的分配。有就是能
有欧拉回路,没有就是没有。欧拉回路是哪个?察看流值分配,将所有流量非 0
(上限是 1,流值不是 0 就是 1)的边反向,就能得到每点入度=出度的欧拉图。
由于是满流,所以每个入>出的点,都有 x 条边进来,将这些进来的边反向,
OK,入=出了。对于出>入的点亦然。那么,没和 s、t 连接的点怎么办?和 s 连
接的条件是出>入,和 t 连接的条件是入>出,那么这个既没和 s 也没和 t 连接的
点,自然早在开始就已经满足入=出了。那么在网络流过程中,这些点属于“中
间点”。我们知道中间点流量不允许有累积的,这样,进去多少就出来多少,反
向之后,自然仍保持平衡。
所以,就这样,混合图欧拉回路问题,解了。

#include<stdio.h>
#include<string.h>
#include<queue>
#include<stack>
#include<vector>
#include<algorithm>
using namespace std;
#define MAXN 400
#define MAXM 50000
#define INF 10000000+10
int dis[MAXN],vis[MAXN],head[MAXN],in[MAXN],out[MAXN];
int cur[MAXN],m,n,top,sum;
bool flog;
struct node
{
	int u,v,cap,flow,next;
}edge[MAXM];
void init()
{
	top=0;
	memset(head,-1,sizeof(head));
}
void add(int a,int b,int c)
{
	node E1={a,b,c,0,head[a]};
	edge[top]=E1;
	head[a]=top++;
	node E2={b,a,0,0,head[b]};
	edge[top]=E2;
	head[b]=top++;
}
void getmap()
{
	init();
	scanf("%d%d",&n,&m);
	memset(in,0,sizeof(in));
	memset(out,0,sizeof(out));
	for(int i=1;i<=m;i++)
	{
		int a,b,c;
		scanf("%d%d%d",&a,&b,&c);
		if(a==b) continue;
		if(c==0) add(a,b,1);
		in[b]++,out[a]++;
	}
	flog=true;
	sum=0;
	for(int i=1;i<=n;i++)
	{
		int k=abs(in[i]-out[i]);
		if(k%2)
		{
			flog=false;
			break;
		}
		k/=2;
		if(in[i]<out[i])
		add(0,i,k),sum+=k;
		else
		add(i,n+1,k);
	}
}
bool bfs(int s,int e)
{
	queue<int>q;
	memset(vis,0,sizeof(vis));
	memset(dis,-1,sizeof(dis));
	q.push(s);
	dis[s]=0;
	vis[s]=1;
	while(!q.empty())
	{
		int u=q.front();
		q.pop();
		for(int i=head[u];i!=-1;i=edge[i].next)
		{
			node E=edge[i];
			if(E.cap>E.flow&&!vis[E.v])
			{
				vis[E.v]=1;
				dis[E.v]=dis[E.u]+1;
				if(E.v==e)
				return true;
				q.push(E.v);
			}
		}
	}
	return false;
}
int dfs(int x,int a,int e)
{
	if(x==e||a==0)
	return a;
	int flow=0,f;
	for(int& i=cur[x];i!=-1;i=edge[i].next)
	{
		node& E=edge[i];
		if(dis[E.v]==dis[x]+1&&(f=dfs(E.v,min(a,E.cap-E.flow),e))>0)
		{
			E.flow+=f;
			edge[i^1].flow-=f;
			a-=f;
			flow+=f;
			if(a==0) break;
		}
	}
	return flow;
}
int MAXflow(int s,int e)
{
	int flow=0;
	while(bfs(s,e))
	{
		memcpy(cur,head,sizeof(head));
		flow+=dfs(s,INF,e);
	}
	return flow;
}
void slove()
{
	if(!flog)
	{
		printf("impossible
");
		return ;
	}
	if(MAXflow(0,n+1)==sum)
	printf("possible
");
	else
	printf("impossible
");
}
int main()
{
	int t;
	scanf("%d",&t);
	while(t--)
	{
		init();
		getmap();
		slove();
	}
	return 0;
}


原文地址:https://www.cnblogs.com/playboy307/p/5273649.html