首页 > 其他 > 详细

POJ 1637 混合图欧拉回路判定

时间:2014-03-26 16:30:07      阅读:584      评论:0      收藏:0      [点我收藏+]
Sightseeing tour
Time Limit: 1000MS   Memory Limit: 10000K
Total Submissions: 7070   Accepted: 2941

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


只需要判断出度入度,然后就是建图跑最大流判断是否满流。

代码:

/* ***********************************************
Author :rabbit
Created Time :2014/3/26 10:45:50
File Name :12.cpp
************************************************ */
#pragma comment(linker, "/STACK:102400000,102400000")
#include <stdio.h>
#include <iostream>
#include <algorithm>
#include <sstream>
#include <stdlib.h>
#include <string.h>
#include <limits.h>
#include <string>
#include <time.h>
#include <math.h>
#include <queue>
#include <stack>
#include <set>
#include <map>
using namespace std;
#define INF 0x3f3f3f3f
#define eps 1e-8
#define pi acos(-1.0)
typedef long long ll;
const int maxn=20010;
const int maxm=1000000;
struct Edge{
	int next,to,cap;
	Edge(){};
	Edge(int _next,int _to,int _cap){
		next=_next;to=_to;cap=_cap;
	}
}edge[maxm];
int head[maxn],tol,dep[maxn],gap[maxn];
void addedge(int u,int v,int flow){
    edge[tol]=Edge(head[u],v,flow);head[u]=tol++;
    edge[tol]=Edge(head[v],u,0);head[v]=tol++;
}
void bfs(int start,int end){
    memset(dep,-1,sizeof(dep));
    memset(gap,0,sizeof(gap));
    gap[0]++;int front=0,rear=0,Q[maxn];
    dep[end]=0;Q[rear++]=end;
    while(front!=rear){
        int u=Q[front++];
        for(int i=head[u];i!=-1;i=edge[i].next){
            int v=edge[i].to;if(dep[v]==-1)
                Q[rear++]=v,dep[v]=dep[u]+1,gap[dep[v]]++;
        }
    }
}
int sap(int s,int t,int N){
	int res=0;bfs(s,t);
	int cur[maxn],S[maxn],top=0,u=s,i;
	memcpy(cur,head,sizeof(head));
	while(dep[s]<N){
		if(u==t){
			int temp=INF,id;
		    for( i=0;i<top;i++)
			   if(temp>edge[S[i]].cap)
				   temp=edge[S[i]].cap,id=i;
		    for( i=0;i<top;i++)
			      edge[S[i]].cap-=temp,edge[S[i]^1].cap+=temp;
		    res+=temp;top=id;u=edge[S[top]^1].to;
		}
		if(u!=t&&gap[dep[u]-1]==0)break;
		for( i=cur[u];i!=-1;i=edge[i].next)
			if(edge[i].cap&&dep[u]==dep[edge[i].to]+1)break;
		if(i!=-1)cur[u]=i,S[top++]=i,u=edge[i].to;
		else{
			int MIN=N;
			for( i=head[u];i!=-1;i=edge[i].next)
				if(edge[i].cap&&MIN>dep[edge[i].to])
					MIN=dep[edge[i].to],cur[u]=i;
			--gap[dep[u]];++gap[dep[u]=MIN+1];
			if(u!=s)u=edge[S[--top]^1].to;
		}
	}
	return res;
}
int in[maxn],out[maxn];

int main()
{
     //freopen("data.in","r",stdin);
     //freopen("data.out","w",stdout);
     int T,t;
	 scanf("%d",&T);
     for(t=1;t<=T;t++){
		 int n,m;
		 scanf("%d%d",&n,&m);
		 memset(in,0,sizeof(in));
		 memset(out,0,sizeof(out));
		 memset(head,-1,sizeof(head));tol=0;
		 int i,j,k;
		 while(m--){
			scanf("%d%d%d",&i,&j,&k);
			 out[i]++;in[j]++;
			 if(k==0)addedge(i,j,1);
		 }
		 int flag=1;
				int sum=0;
				for(int i=1;i<=n;i++){
					int ret=out[i]-in[i];
					if(ret>0)addedge(0,i,ret/2),sum+=ret/2;
					else if(ret<0)addedge(i,n+1,-ret/2);
					if((in[i]+out[i])%2){
						flag=0;break;
					}
				}
				if(!flag){
					puts("impossible");continue;
				}
				int ans=sap(0,n+1,n+10);
				if(ans==sum)puts("possible");
				else puts("impossible");
	 }
     return 0;
}


POJ 1637 混合图欧拉回路判定,布布扣,bubuko.com

POJ 1637 混合图欧拉回路判定

原文:http://blog.csdn.net/xianxingwuguan1/article/details/22161355

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!