描述
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.
输入
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.
输出
For each scenario, output one line containing the text "possible" or "impossible", whether or not it‘s possible to construct a sightseeing tour.
样例输入
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
样例输出
possible
impossible
impossible
possible
题意
m个点,s条边,问是否存在欧拉回路
题解
网络流判混合路欧拉回路
关键是把图变成有向图再判断
容易知道如果是欧拉回路那么所有点的入度in,等于出度out
可以发现无向边(u,v),有向边(u,v)或是有向边(v,u),in[i]-out[i]的奇偶性不变
那我们就可以先假定无向边(u,v)变成有向边(u,v)
统计所有点的入出度
如果存在i,使得abs(in[i]-out[i])%2==1那么图不存在欧拉回路
对于in[i]>out[i]的点,说明i点需要多流出流量,建边(S,i)流量(in[i]-out[i])/2
对于in[i]<out[i]的点,说明i点需要多流入流量,建边(i,T)流量(out[i]-in[i])/2
对于所有无向边(u,v),建边(u,v)流量1
跑S->T的最大流,若满流即存在一种方法通过改变无向边的方向使得每个点的入度=出度(是不是类似于上下界可行流是否有解问题)
代码
1 #include<stdio.h> 2 #include<string.h> 3 #include<algorithm> 4 using namespace std; 5 6 const int maxn=1e5+5; 7 const int maxm=2e5+5; 8 const int INF=0x3f3f3f3f; 9 10 int TO[maxm],CAP[maxm],NEXT[maxm],tote; 11 int FIR[maxn],gap[maxn],cur[maxn],d[maxn],q[400000]; 12 int n,m,S,T; 13 14 void add(int u,int v,int cap) 15 { 16 //printf("i=%d %d %d %d\n",tote,u,v,cap); 17 TO[tote]=v; 18 CAP[tote]=cap; 19 NEXT[tote]=FIR[u]; 20 FIR[u]=tote++; 21 22 TO[tote]=u; 23 CAP[tote]=0; 24 NEXT[tote]=FIR[v]; 25 FIR[v]=tote++; 26 } 27 void bfs() 28 { 29 memset(gap,0,sizeof gap); 30 memset(d,0,sizeof d); 31 ++gap[d[T]=1]; 32 for(int i=1;i<=n;++i)cur[i]=FIR[i]; 33 int head=1,tail=1; 34 q[1]=T; 35 while(head<=tail) 36 { 37 int u=q[head++]; 38 for(int v=FIR[u];v!=-1;v=NEXT[v]) 39 if(!d[TO[v]]) 40 ++gap[d[TO[v]]=d[u]+1],q[++tail]=TO[v]; 41 } 42 } 43 int dfs(int u,int fl) 44 { 45 if(u==T)return fl; 46 int flow=0; 47 for(int &v=cur[u];v!=-1;v=NEXT[v]) 48 if(CAP[v]&&d[u]==d[TO[v]]+1) 49 { 50 int Min=dfs(TO[v],min(fl,CAP[v])); 51 flow+=Min,fl-=Min,CAP[v]-=Min,CAP[v^1]+=Min; 52 if(!fl)return flow; 53 } 54 if(!(--gap[d[u]]))d[S]=n+1; 55 ++gap[++d[u]],cur[u]=FIR[u]; 56 return flow; 57 } 58 int ISAP() 59 { 60 bfs(); 61 int ret=0; 62 while(d[S]<=n)ret+=dfs(S,INF); 63 return ret; 64 } 65 void init() 66 { 67 tote=0; 68 memset(FIR,-1,sizeof FIR); 69 } 70 int main() 71 { 72 int N,u,v,op,s,_; 73 scanf("%d",&_); 74 while(_--) 75 { 76 int in[205]={0},out[205]={0}; 77 init(); 78 scanf("%d%d",&N,&m); 79 S=N+1,T=S+1,n=T; 80 for(int i=1;i<=m;i++) 81 { 82 scanf("%d%d%d",&u,&v,&op); 83 in[v]++,out[u]++; 84 if(op==0) 85 add(u,v,1); 86 } 87 int flag=1; 88 for(int i=1;i<=N;i++) 89 if((out[i]-in[i])%2==1) 90 { 91 flag=0; 92 break; 93 } 94 if(!flag) 95 { 96 printf("impossible\n"); 97 continue; 98 } 99 int sum=0; 100 for(int i=1;i<=N;i++) 101 { 102 if(in[i]>out[i]) 103 { 104 sum+=(in[i]-out[i])/2; 105 add(i,T,(in[i]-out[i])/2); 106 } 107 else if(out[i]>in[i]) 108 add(S,i,(out[i]-in[i])/2); 109 } 110 printf("%s\n",ISAP()==sum?"possible":"impossible"); 111 } 112 return 0; 113 }
原文地址:https://www.cnblogs.com/taozi1115402474/p/9743778.html