hdu 1956

学欧拉图过程中就遇到了混合欧拉的判定。。然后难度一下子上去了。。

这个sigthseeing tour是道很经典的题。。我初学网络流的时候,还有hdu、poj、白书上好像都有。。

然后大致思路是酱紫的:先判断是否连通。。然后在判断入度和出度差为奇数的点,有的话肯定是没有欧拉回路的,然后有向边可以不管,无向边暂时随机定向,剩下的问题就是如何改变无向边的方向来形成欧拉回路了。。

先记录每个点的di=入度-出度,事实上,让一条有向边(s,t)反向,效果就是ds-2,dt+2。。然后咋们就可以形成这么一种思路,建立源点S和终点T,S连向入度大于出度的点,容量为d/2,同理出度大于入度的点连向T,容量为-d/2。。然后有向边按我们定好的方向连,容量为1,跑一遍看是否满流即可。。

由于太久没敲代码了所以之前的ISAP模板全忘光了。。。本来这个模板就挺反人类的了。。所以写题的时候一直都在弄模板qaq


#include<cstdio>
#include<cstring>
#include<algorithm>
#include<iostream>
#include<queue>
#define inc(i,l,r) for(int i=l;i<=r;i++)
#define dec(i,l,r) for(int i=l;i>=r;i--)
#define link(x) for(edge *j=h[x];j;j=j->next)
#define inf 1e9
#define mem(a) memset(a,0,sizeof(a))
#define ll long long
#define succ(x) (1<<x)
#define lowbit(x) (x&&(-x))
#define NM 205
#define nm 4005
using namespace std;

int read(){
	int x=0,f=1;char ch=getchar();
	while(!isdigit(ch)){if(ch=='-')f=-1;ch=getchar();}
	while(isdigit(ch))x=x*10+ch-'0',ch=getchar();
	return f*x;
}


struct edge{
	int t,v;
	edge *next,*rev;
}e[nm],*h[NM],*o=e;
void add(int x,int y,int v){
	o->t=y;o->v=v;o->next=h[x];h[x]=o++;
}
void _add(int x,int y,int v){
	add(x,y,v);add(y,x,0);
	h[x]->rev=h[y];h[y]->rev=h[x];
}

int f[NM],n,m,_s,a[NM];

int maxflow(){
	int flow=0,tot=n+1,d[NM],cnt[NM];edge *j,*p[NM],*tmp[NM];
	mem(d);mem(tmp);mem(cnt);mem(p);mem(tmp);
	inc(i,0,n)tmp[i]=h[i];
	cnt[0]=tot;
	for(int x=0,s=inf;d[n]<tot;){
		for(j=tmp[x];j;j=j->next)
			if(j->v&&d[j->t]+1==d[x])break;
		if(j){
			s=min(j->v,s);tmp[x]=p[j->t]=j;
			if((x=j->t)==n){
				for(;x;x=p[x]->rev->t)
				p[x]->v-=s,p[x]->rev->v+=s;
				flow+=s;s=inf;
			}
		}else{
			if(!--cnt[d[x]])break;
			d[x]=tot;
			link(x)if(j->v&&d[x]>d[j->t]+1)
			d[x]=d[j->t]+1,tmp[x]=j;
			cnt[d[x]]++;
			if(x)x=p[x]->rev->t;
		}
	}
//	printf("%d\n",flow);
	return flow;
}

int find(int x){
	return f[x]==x?x:f[x]=find(f[x]);
}
int main(){
	freopen("data.in","r",stdin);
	int T=read();
	while(T--){
		mem(f);mem(e);mem(h);o=e;mem(a);_s=0;
		n=read();m=read();
		inc(i,1,n)f[i]=i;
		inc(i,1,m){
			int _x=read(),_y=read();
			a[_x]++;a[_y]--;
			if(!read())_add(_x,_y,1);
			if(_x==_y)continue;
			_x=find(_x);_y=find(_y);
			if(_x!=_y)f[_x]=_y;
		}
		inc(i,1,n)if(find(i)!=find(1)){
			printf("impossible\n");
			goto la;
		}
		inc(i,1,n)if(a[i]%2){
			printf("impossible\n");
			goto la;
		}else if(a[i]>0)_add(0,i,a[i]/2),_s+=a[i]/2;
		else if(a[i]<0)_add(i,n+1,-a[i]/2);
		//inc(i,1,n)printf("%d ",a[i]);putchar('\n');
		n++;
		if(_s==maxflow())printf("possible\n");else printf("impossible\n");
la:;
	}
	return 0;
}

Sightseeing tour
Time Limit: 5000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 496    Accepted Submission(s): 230


Problem 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
NWERC2003
 

Recommend
wangye
 


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值