poj 1637 最大流 && 欧拉混合回路

Sightseeing tour
Time Limit: 1000MS   Memory Limit: 10000K
Total Submissions: 6821   Accepted: 2816

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

抄:  http://yzmduncan.iteye.com/blog/1149049

基础知识

    欧拉回路是图G中的一个回路,经过每条边有且仅一次,称该回路为欧拉回路。具有欧拉回路的图称为欧拉图,简称E图。

    无向图中存在欧拉回路的条件:每个点的度数均为偶数。

    有向图中存在欧拉回路的条件:每个点的入度=出度。

    欧拉路径比欧拉回路要求少一点:

    无向图中存在欧拉路径的条件:每个点的度数均为偶数或者有且仅有2个度数为奇数的点。

    有向图中存在欧拉路径的条件:除了2个点外,其余的点入度=出度,且在这2个点中,一个点的入度比出度大1,另一个出度比入度大1。

     欧拉路径的输出:经典的套圈算法。

 

    下面来重点讲讲混合图的欧拉回路问题。

    混合图就是边集中有有向边和无向边同时存在。这时候需要用网络流建模求解。

    建模:

    把该图的无向边随便定向,计算每个点的入度和出度。如果有某个点出入度之差为奇数,那么肯定不存在欧拉回路。 因为欧拉回路要求每点入度 = 出度,也就是总度数为偶数,存在奇数度点必不能有欧拉回路。

    好了,现在每个点入度和出度之差均为偶数。那么将这个偶数除以2,得x。也就是说,对于每一个点,只要将x条边改变方向(入>出就是变入,出>入就是变出),就能保证出 = 入。如果每个点都是出 = 入,那么很明显,该图就存在欧拉回路。

    现在的问题就变成了:我该改变哪些边,可以让每个点出 = 入?构造网络流模型。

    首先,有向边是不能改变方向的,要之无用,删。一开始不是把无向边定向了吗?定的是什么向,就把网络构建成什么样,边长容量上限1。另新建s和t。对于入 > 出的点u,连接边(u, t)、容量为x,对于出 > 入的点v,连接边(s, v),容量为x(注意对不同的点x不同)。
    之后,察看从S发出的所有边是否满流。有就是能有欧拉回路,没有就是没有。欧拉回路是哪个?察看流值分配,将所有流量非 0(上限是1,流值不是0就是1)的边反向,就能得到每点入度 = 出度的欧拉图。 
    由 于是满流,所以每个入 > 出的点,都有x条边进来,将这些进来的边反向,OK,入 = 出了。对于出 > 入的点亦然。那么,没和s、t连接的点怎么办?和s连接的条件是出 > 入,和t连接的条件是入 > 出,那么这个既没和s也没和t连接的点,自然早在开始就已经满足入 = 出了。那么在网络流过程中,这些点属于“中间点”。我们知道中间点流量不允许有累积的,这样,进去多少就出来多少,反向之后,自然仍保持平衡。
    所以,就这样,混合图欧拉回路问题,解了。

AC代码:

#include<iostream>
#include<cstdio>
#include<cstring>
#include<string>
#include<cmath>
#include<vector>
#include<cstdlib>
#include<algorithm>
#include<queue>
#include<map>

using namespace std;

#define LL long long
#define ULL unsigned long long
#define UINT unsigned int
#define MAX_INT 0x7fffffff
#define cint const int
#define MAX(X,Y) ((X) > (Y) ? (X) : (Y))
#define MIN(X,Y) ((X) < (Y) ? (X) : (Y))
#define INF 100000000
#define MAXN 222
#define MAXM 2522

struct edge{
    int u, v, cap, flow, nxt;
}e[MAXM];
int h[MAXN], cc;

void add(int u, int v, int cap){
    e[cc]=(edge){u, v, cap, 0, h[u]};
    h[u]=cc++;
    e[cc]=(edge){v, u, 0, 0, h[v]};
    h[v]=cc++;
}

int num[MAXN], p[MAXN], d[MAXN], vis[MAXN];
int cur[MAXN];

void rbfs(cint t){
    queue<int> q;       q.push(t);
    memset(vis, 0, sizeof(vis));
    vis[t]=1;       d[t]=0;
    while(!q.empty()){
        int u=q.front();   q.pop();
        for(int i=h[u]; i!=-1; i=e[i].nxt){
            i^=1;
            int v=e[i].u, cap=e[i].cap, ef=e[i].flow;
            if(!vis[v] && cap>ef){
                vis[v]=1;
                d[v]=d[u]+1;
                q.push(v);
            }
            i^=1;
        }
    }
}

int Augment(cint s, cint t){
    int u, a=INF;
    for(u=t; u!=s; u=e[p[u]].u)
        a = MIN(a, e[p[u]].cap - e[p[u]].flow);
    for(u=t; u!=s; u=e[p[u]].u){
        e[p[u]].flow+=a;
        e[p[u]^1].flow-=a;
    }
    return a;
}

int isap(cint s, cint t, cint n){
    int i, flow=0;
    rbfs(t);
    memset(num, 0, sizeof(num));
    for(i=0; i<n; i++) num[d[i]]++;
    for(i=0; i<n; i++) cur[i]=h[i];
    for(int u=s; d[s]<n;){
        if(u==t){
            flow+=Augment(s, t);
            u=s;
        }
        bool ok=false;
        for(i=cur[u]; i!=-1; i=e[i].nxt){
            int v=e[i].v, cap=e[i].cap, ef=e[i].flow;
            if(d[v]+1==d[u] && cap>ef){
                ok=true;
                p[v]=i;
                cur[u]=i;
                u=v;
                break;
            }
        }
        if(!ok){
            int tmp = n-1;
            for(i=h[u]; i!=-1; i=e[i].nxt){
                int v=e[i].v, cap=e[i].cap, ef=e[i].flow;
                if(cap>ef) tmp=MIN(tmp, d[v]);
            }
            if(--num[d[u]]==0) break;
            num[d[u]=tmp+1]++;
            cur[u]=h[u];
            if(u!=s) u=e[p[u]].u;
        }
    }
    return flow;
}

int in[MAXN], out[MAXN];

bool solve(){
    int n, u, v, f, m, i;
    scanf(" %d %d", &n, &m);
    memset(in, 0, sizeof(in));
    memset(out, 0, sizeof(out));
    memset(h, -1, sizeof(h));       cc=0;
    while(m--){
        scanf(" %d %d %d", &u, &v, &f);
        out[u]++;   in[v]++;      
        if(!f) add(u, v, 1);        //忽略有向边,无向边随便取方向
    }
    int sum=0;
    for(i=1; i<=n; i++){
        int cap = abs(in[i] - out[i]);  
        if(cap&1) break;          //若无向边数量非偶
        else if(in[i]<out[i]){
            add(0, i, cap>>1);       //改变x/2条边方向,源点0
            sum = sum + (cap>>1);
        }
        else if(in[i]>out[i]) add(i, n+1, cap>>1);    //汇点n+1
    }
    if(i<=n) return false;
    return sum==isap(0, n+1, n+2);        //满流?
}

int main(){
//    freopen("C:\Users\Administrator\Desktop\in.txt","r",stdin);
    int T;
    scanf(" %d", &T);
    while(T--){
        if(solve()) printf("possible
");
        else printf("impossible
");
    }
    return 0;
}