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

37 篇文章 0 订阅
9 篇文章 0 订阅
混合图的欧拉回路判断方法在黑书上面有具体提到。
 
这里采用的方法:
先给无向边定向,让后统计每一个点的出入度,如果有一个点的出入度只差为奇数,则该图不存在欧拉回路(有向图的欧拉回路每个点的度数都是偶数,至于出度=入度,在求最大流时我们会进行调整)。
全部判断完后,开始建图。
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<iostream>
#include<algorithm>
#include<cstdio>
#include<cmath>
#include<cstring>
#include<string>

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(f<t)
	{
		int j=que[f++];
		for(int k=head[j];k!=-1;k=e[k].next)
		{	
			int i=e[k].to;
			if(dis[i]==-1 && e[k].c)
			{
				que[t++]=i;
				dis[i]=dis[j]+1;
				if(i==ed) return true;
			}
		}
	}
	return false; 
} 

int update()  
{  
	int p,flow=INF;  
    for (int i=pre[ed];i!=-1;i=pre[i])
		if(e[head2[i]].c<flow) p=i,flow=e[head2[i]].c;    
    for (int i=pre[ed];i!=-1;i=pre[i]) 
		e[head2[i]].c-=flow,e[head2[i]^1].c+=flow;   
    maxflow+=flow; 
    return p;
}  

void dfs() 
{  
	memset(pre,-1,sizeof(pre));
	memcpy(head2,head,sizeof(head2));  
    for(int i=st,j;i!=-1;)  
    {  
        int flag=false;  
        for(int k=head[i];k!=-1;k=e[k].next)    
          if(e[k].c && (dis[j=e[k].to]==dis[i]+1) )  
          {  
                pre[j]=i; 
				head2[i]=k;  
				i=j; 
				flag=true;  
                if(i==ed) 
					i=update();  
                if(flag) 
					break;  
          }  
        if (!flag) dis[i]=-1,i=pre[i];   
    }  
} 

int build()
{
	int temp=0;
	memset(head,-1,sizeof(head));
	for(int i=1;i<=n;i++)
	{
		if(deg[i])
		{
			if(deg[i]>0)
				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;
} 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值