【POJ1637】Sightseeing tour

                                            Sightseeing tour

Time Limit: 1000MS Memory Limit: 10000K
Total Submissions: 10504 Accepted: 4428

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 <cstdlib>
#include <cstring>
#include <queue>
using namespace std;

const int inf=1e9;
const int Maxn=205;
const int Maxm=2010;
int t,n,m,size,S,T,sum,ans;
int first[Maxn],dep[Maxn],du[Maxn],tmp[Maxn];
struct shu{int to,next,len;};
shu edge[Maxm<<1];

inline int get_int()
{
	int x=0,f=1;
	char c;
	for(c=getchar();(!isdigit(c))&&(c!='-');c=getchar());
	if(c=='-') f=-1,c=getchar();
	for(;isdigit(c);c=getchar()) x=(x<<3)+(x<<1)+c-'0';
	return x*f;
}

inline void build(int x,int y,int z)
{
	edge[++size].next=first[x],first[x]=size,edge[size].to=y,edge[size].len=z;
	edge[++size].next=first[y],first[y]=size,edge[size].to=x,edge[size].len=0;
}

inline void init()
{
	size=1,ans=sum=0;
	for(int i=0;i<=n+1;i++) first[i]=du[i]=0;
	n=get_int(),m=get_int(),T=n+1;
	for(int i=1;i<=m;i++)
	{
	  int x=get_int(),y=get_int(),tag=get_int();
	  du[x]--,du[y]++;
	  if(!tag) build(y,x,1);
	}
	for(int i=1;i<=n;i++)
	  if(du[i]>0) build(S,i,du[i]/2),sum+=du[i]/2;
	  else build(i,T,-du[i]/2);
}

inline bool bfs()
{
	for(int i=0;i<=n+1;i++) dep[i]=0;
	queue<int>q;q.push(S),dep[S]=1;
	while(q.size())
	{
	  int p=q.front();q.pop();
	  for(int u=first[p];u;u=edge[u].next)
	  {
	  	int to=edge[u].to;
	  	if(!dep[to]&&edge[u].len)
	  	{
	  	  dep[to]=dep[p]+1,q.push(to);
	  	  if(to==T) return 1;
	  	}
	  }
	}
	return 0;
}

inline int dinic(int p,int flow)
{
	if(p==T) return flow;
	int sum=0;
	for(int &u=tmp[p];u&&sum<flow;u=edge[u].next)
	{
	  int to=edge[u].to;
	  if(dep[to]==dep[p]+1&&edge[u].len)
	  {
	    int minn=dinic(to,min(flow-sum,edge[u].len));
	    if(!(flow-sum)){dep[to]=0;break;}
	    edge[u].len-=minn,edge[u^1].len+=minn,sum+=minn;
	  }
	}
	return sum;
}


inline void solve()
{
	for(int i=1;i<=n;i++) if(du[i]&1) {puts("impossible");return;}
	while(bfs())
	{
	  memcpy(tmp,first,sizeof(first));
	  ans+=dinic(S,inf);
	}
	if(ans==sum) puts("possible");
	else puts("impossible");
}

int main()
{
	t=get_int();
	while(t--)
	{
	  init();
	  solve();
	}
	return 0;
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值