hdu 1956

来源:互联网 发布:大数据搜索 编辑:程序博客网 时间:2024/05/21 07:59

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

这个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 4005using 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 tourTime Limit: 5000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 496    Accepted Submission(s): 230Problem DescriptionThe 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. InputOn 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. OutputFor each scenario, output one line containing the text “possible” or “impossible”, whether or not it’s possible to construct a sightseeing tour. Sample Input45 82 1 01 3 04 1 11 5 05 4 13 4 04 2 12 2 04 41 2 12 3 03 4 01 4 13 31 2 02 3 03 2 03 41 2 02 3 11 2 03 2 0 Sample Outputpossibleimpossibleimpossiblepossible SourceNWERC2003 Recommendwangye 


原创粉丝点击