混合图中欧拉回路

给出一张混合图(有有向边,也有无向边),判断是否存在欧拉回路。

首先是对图中的无向边随意定一个方向,然后统计每个点的入度(indeg)和出度(outdeg),

如果(indeg - outdeg)是奇数的话,一定不存在欧拉回路;

如果所有点的入度和出度之差都是偶数,那么就开始网络流构图:

1,对于有向边,舍弃;

   对于无向边,就按照最开始指定的方向建权值为 1 的边;

2,对于入度小于出度的点,从源点连一条到它的边,权值为(outdeg - indeg)/2;

   出度小于入度的点,连一条它到汇点的权值为(indeg - outdeg)/2 的边;

构图完成,如果满流(求出的最大流值 == 和汇点所有连边的权值之和),那么存在欧拉回路,否则不存在。

^_^路径输出:

就是把所有用到的边都存下来~(原图中的有向边必然要存下来),

但是对于原图中的无向边,我们要根据刚才最大流时每条边中的流量关系来确定其方向:

如果规定方向后,跑出最大流时这条无向边中有流量,说明要这条边正向

否则,将这条边反向

例题: POJ 1637 Sightseeing tour

Sightseeing tour
Time Limit:1000MS Memory Limit:10000K
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

 解: 

解题思路如上所述

  1 #include<iostream>
  2 #include<cstdio>
  3 #include<cmath>
  4 #include<algorithm>
  5 #include<cstring>
  6 #include<string>
  7 #include<queue>
  8 #define ll long long
  9 #define inf 2147483600
 10 using namespace std;
 11 inline int read()
 12 {
 13     int x=0,w=1;char ch=getchar();
 14     while(!isdigit(ch)){if(ch=='-') w=-1;ch=getchar();}
 15     while(isdigit(ch)) x=(x<<3)+(x<<1)+ch-'0',ch=getchar();
 16     return x*w;
 17 }
 18 const int N=1e5+10;
 19 struct node{
 20     int u,v,fl,ne;
 21 }e[N*2];
 22 int h[N],tot,n,m,aim;
 23 queue<int>q;
 24 int d[N],S,T;
 25 int fg,ans;
 26 int Q,ol[N][2],js,in[N],out[N];
 27 void add1(int u,int v,int fl)
 28 {
 29     if(v==T) aim+=fl;
 30     tot++;e[tot]=(node){u,v,fl,h[u]};h[u]=tot;
 31 }
 32 void add(int u,int v,int fl)
 33 {
 34     add1(u,v,fl);add1(v,u,0);
 35 }
 36 bool bfs()
 37 {
 38     for(int i=S;i<=T;++i) d[i]=0;
 39     d[S]=1;q.push(S);
 40     while(!q.empty())
 41     {
 42         int ff=q.front();q.pop();
 43         for(int i=h[ff];i;i=e[i].ne)
 44         {
 45             int rr=e[i].v;
 46             if(!d[rr] && e[i].fl)
 47              d[rr]=d[ff]+1,q.push(rr);
 48         }
 49     }
 50     return d[T];
 51 }
 52 int dfs(int u,int fl)
 53 {
 54     if(u==T) return fl;
 55     int get=0,f;
 56     for(int i=h[u];i;i=e[i].ne)
 57     {
 58         int rr=e[i].v;
 59         if(e[i].fl && d[rr]==d[u]+1)
 60         {
 61             f=dfs(rr,min(fl,e[i].fl));
 62             if(f==0) continue;
 63             e[i].fl-=f;e[i^1].fl+=f;
 64             get+=f;fl-=f;
 65             if(fl==0) break;
 66         }
 67     }
 68     if(get==0) d[u]=0;
 69     return get;
 70 }
 71 void clear()
 72 {
 73     S=0;T=n+1;tot=1;js=0;
 74     aim=0;fg=0;
 75     for(int i=S;i<=T;++i)
 76     {
 77         h[i]=0;
 78         in[i]=0;out[i]=0;
 79         ol[i][0]=0;ol[i][1]=0;
 80     }
 81 }
 82 void Flow()
 83 {
 84     for(int i=1,u,v;i<=js;++i)
 85     {
 86         u=ol[i][0];v=ol[i][1];
 87         add(u,v,1);
 88     }
 89     for(int i=1;i<=n;++i)
 90      if(in[i]<out[i]) add(S,i,(out[i]-in[i])/2);
 91      else if(in[i]>out[i]) add(i,T,(in[i]-out[i])/2);
 92     ans=0;
 93     while(bfs()) ans+=dfs(S,inf);
 94     if(ans<aim) fg=1;
 95 }
 96 int main()
 97 {
 98     Q=read();
 99     while(Q--)
100     {  
101         n=read();m=read();
102         clear();
103         for(int i=1,x,y,z;i<=m;++i)
104         {
105             x=read();y=read();z=read();
106             in[y]++;out[x]++; 
107             if(z==0)ol[++js][0]=x,ol[js][1]=y;
108         }
109         for(int i=1;i<=n;++i)
110          if((in[i]-out[i])&1){fg=1;break;}
111         if(fg) puts("impossible");
112         else{
113             fg=0;Flow();
114             if(fg) puts("impossible");
115             else puts("possible");
116         }
117     }
118     return 0;
119 }
拍个代码

 

原文地址:https://www.cnblogs.com/adelalove/p/8650934.html