POJ1637 Sightseeing tour 混合图的欧拉回路+最大流

混合图的欧拉回路判断方法在黑书上面有具体提到。
 
这里采用的方法:
先给无向边定向,让后统计每一个点的出入度,如果有一个点的出入度只差为奇数,则该图不存在欧拉回路(有向图的欧拉回路每个点的度数都是偶数,至于出度=入度,在求最大流时我们会进行调整)。
全部判断完后,开始建图。
1:把每一条无向边建成一条容量为1的弧。
2:出入度之差不为0的点:
如果出>入 则将改点和源点相连 容量为出入度之差/2。
如果入>出 则将点和汇点相连,容量为出入度之差/2。
完成后求一个最大流。如果是满流则存在偶来回路
Sightseeing tour
Time Limit: 1000MS   Memory Limit: 10000K
Total Submissions: 5792   Accepted: 2357

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
 
  
#include
#include
#include
#include
#include
#include

using namespace std;

#define MAXN 255
#define INF 0xFFFFFF

struct edge
{
	int to,c,next;
};

struct edge2
{
	int u,v,ty;
};

edge e[99999];
edge2 ee[99999];
int deg[MAXN]; 
int que[MAXN*100],dis[MAXN],pre[MAXN];
int head[MAXN],head2[MAXN];
int st,ed,maxflow;
int en,n,m;

void add(int a,int b,int c)
{
	e[en].to=b;
	e[en].c=c;
	e[en].next=head[a];
	head[a]=en++;
	e[en].to=a;
	e[en].c=0;
	e[en].next=head[b];
	head[b]=en++;	
} 

bool bfs() 
{
	memset(dis,-1,sizeof(dis));
	que[0]=st,dis[st]=1;
	int t=1,f=0;
	while(f0)
				add(i,ed,deg[i]);
			else
			{
				add(st,i,abs(deg[i]));
				temp+=deg[i];
			}
		}
	}
	for(int i=1;i<=m;i++)
		if(!ee[i].ty) add(ee[i].u,ee[i].v,1);
	return temp;
}

void solve()
{
	int a,b,c;
	scanf("%d%d",&n,&m);
	memset(deg,0,sizeof(deg));
	st=0,ed=1+n;
	for(int i=1;i<=m;i++)
	{
		scanf("%d%d%d",&a,&b,&c);
		deg[a]--;
		deg[b]++;
		ee[i].u=a;
		ee[i].v=b;
		ee[i].ty=c;
		
	}
	for(int i=1;i<=n;i++)
	{
		if(deg[i]%2!=0)
		{
			printf("impossible\n");
			return;
		}
		deg[i]/=2;
	}
	int fuflow=abs(build());
	maxflow=0;
	while(bfs()) dfs();
	if(maxflow==fuflow)
		printf("possible\n");
	else
		printf("impossible\n");
	
}

int main()
{
	int t;
	scanf("%d",&t);
	while(t--) solve();
	return 0;
} 

你可能感兴趣的:(图论,网络流,欧拉回路,each,integer,build,input,struct)