[POJ]1673 混合欧拉图判定

本文介绍了一种利用网络流解决混合图中是否存在欧拉回路的问题,并提供了详细的算法实现过程。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Sightseeing tour
Time Limit: 1000MS Memory Limit: 10000K
Total Submissions: 9713 Accepted: 4093

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

题意

          就是给定你一个既有有向边又有无向边的图,要问是否有欧拉回路(即欧拉环),这就是经典的混合欧拉图判定.

题解

          刚开始拿到这道题什么算法要想不出来,唯一可能就爆搜,因为连存在欧拉回路的充要条件都不知道.
        然而这道题使用网络流求解.
        orz orz orz orz....万能的网络流.
        kuangbin神犇的讲解太清楚经典了,我这里就不班门弄斧了,上链接:点这里
        Tips:  他还讲了欧拉路径的判定
        The End.

#include<stdio.h>
#include<queue>
#include<cstring>
#include<algorithm>
#define clear(a) memset(a,0,sizeof(a))
#define deeper(a) memset(a,-1,sizeof(a))
using namespace std;
const int maxn=250;
bool vis[maxn],flag;
int n,m,q,S,T,num,x,y,z;
int h[maxn],dis[maxn],d[maxn],cur[maxn];
struct edge{
    int nxt,v,c;
}e[20005];
void add(int u,int v,int c){
    e[++num].v=v,e[num].nxt=h[u],e[num].c=c,h[u]=num;
    e[++num].v=u,e[num].nxt=h[v],e[num].c=0,h[v]=num;
}
inline const int read(){
    register int f=1,x=0;
    register char ch=getchar();
    while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}
    while(ch>='0'&&ch<='9'){x=(x<<3)+(x<<1)+ch-'0';ch=getchar();}
    return x*f;
}
bool bfs(){
    queue<int> q;
    deeper(dis);
	q.push(S);
	dis[S]=1;
	while(!q.empty()){
	    int u=q.front();q.pop();
		for(int i=h[u];i;i=e[i].nxt){
	        int v=e[i].v;
	        if(dis[v]==-1&&e[i].c){
	           dis[v]=dis[u]+1;
	           q.push(v);
			}
		}
	}
	return dis[T]!=-1;
}
int dfs(int u,int a){
    if(u==T) return a;
    int flow=0,f;
	for(int i=cur[u];i;i=cur[u]=e[i].nxt){
	    int v=e[i].v;
	    if(dis[v]==dis[u]+1&&e[i].c){
	       int x=min(e[i].c,a);
	       f=dfs(v,x);
	       e[i].c-=f,e[i^1].c+=f;
	       a-=f,flow+=f;
	       if(a==0) return flow;
		}
	}
	if(!flow) dis[u]=-1;
	return flow;
}
int main(){
    q=read();
	while(q--){
	  int flow=0,sum=0;
	  clear(d),clear(h),flag=true,num=1;
	  n=read(),m=read();
	  S=0,T=n+1;
	  for(register int i=1;i<=m;i++){
	      x=read(),y=read(),z=read();
	      d[x]++;d[y]--;
	      if(!z) add(x,y,1);
	  } 
	  for(int i=1;i<=n;i++){
	   	  if(d[i]&1) {flag=false;break;}
		  if(d[i]>0) add(S,i,d[i]/2),sum+=d[i]/2;
		  if(d[i]<0) add(i,T,(-1*d[i])/2); 
	   }
	  if(!flag) {puts("impossible");continue;}
	  while(bfs()) {for(int i=S;i<=T;i++) cur[i]=h[i];flow+=dfs(S,10000000);}
	  if(flow!=sum) flag=false;
	  if(flag) puts("possible");
	  else puts("impossible");
	}   
}


           
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值