「POJ1637」Sightseeing tour
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
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 |
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
1 2 3 4 |
possible impossible impossible possible |
题解
混合图欧拉回路
设点x的出度-入度为d[x]
对于无向图先任意定向,若任意x,d[x]非偶数则impossible
d[x]/2即为连接每个点的边需要反向的数量
若d[x]/2>0 连S->T 容量d[x]/2
否则d[x]/2<0 连 x->T 容量-d[x]/2
舍弃有向边,将定向后的无向边放进网络流图中
判最大流是否等于∑ d[x]/2
没有理解建图可参照网络流建模总结
http://wenku.baidu.com/link?url=Eu-odIezr8ArWPR0MZ55vSL9dENzUhesNZpdrt5mc0ouQpBe_sFiy3lBu9dJV-DzsUHbs_eX2LX8zMLhUZ7F8L3gV0xPfYMTN_MWIT_K3gK
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 |
#include<cstdio> #include<cmath> #include<ctime> #include<cstring> #include<iostream> #include<algorithm> #define inf 1000000000 #define ll long long using namespace std; int read() { int x=0;char ch=getchar(); while(ch<'0'||ch>'9')ch=getchar(); while(ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();} return x; } int S,T,n,m,cnt,tot; int cur[205],last[205],h[205],q[205],d[205]; struct edge{ int to,next,v; }e[20005]; void insert(int u,int v,int w) { e[++cnt].to=v;e[cnt].next=last[u];last[u]=cnt;e[cnt].v=w; e[++cnt].to=u;e[cnt].next=last[v];last[v]=cnt;e[cnt].v=0; } bool bfs() { int head=0,tail=1; memset(h,-1,sizeof(h)); q[0]=S;h[S]=0; while(head!=tail) { int now=q[head];head++; for(int i=last[now];i;i=e[i].next) if(e[i].v&&h[e[i].to]==-1) { h[e[i].to]=h[now]+1; q[tail++]=e[i].to; } } return h[T]!=-1; } int dfs(int x,int f) { if(x==T)return f; int w,used=0; for(int i=cur[x];i;i=e[i].next) if(h[e[i].to]==h[x]+1) { w=dfs(e[i].to,min(f-used,e[i].v)); e[i].v-=w;e[i^1].v+=w; if(e[i].v)cur[x]=i; used+=w;if(used==f)return f; } if(!used)h[x]=-1; return used; } int dinic() { int tmp=0; while(bfs()) { for(int i=S;i<=T;i++)cur[i]=last[i]; tmp+=dfs(S,inf); } return tmp; } bool jud() { for(int i=1;i<=n;i++) if(d[i]%2!=0)return 0; return 1; } int main() { int test=read(); while(test--) { memset(last,0,sizeof(last));cnt=1; memset(d,0,sizeof(d));tot=0; n=read();m=read();T=n+1; for(int i=1;i<=m;i++) { int u=read(),v=read(),f=read(); d[u]++;d[v]--; if(f==0)insert(u,v,1); } for(int i=1;i<=n;i++) { if(d[i]<0)insert(i,T,-d[i]/2); if(d[i]>0)insert(0,i,d[i]/2),tot+=d[i]/2; } if(!jud()){puts("impossible");continue;} if(tot!=dinic())puts("impossible"); else puts("possible"); } return 0; } |
jud()放在建图的前面,更快一些