混合欧拉回路的判断(Dinic)

来源:互联网 发布:php 抽奖系统 设计 编辑:程序博客网 时间:2024/04/29 11:03

POJ1637

Sightseeing tour
Time Limit: 1000MS Memory Limit: 10000KTotal Submissions: 7483 Accepted: 3115

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

45 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 Output

possibleimpossibleimpossiblepossible

题意:在很多城市之间有单向路和双向路,旅游局想构造出一条路线从一个城市出发经过每一条路且仅经过一次然后再回到该城市;

INPUT

第一行首先输入一个整数n代表测试实例的个数,每个测试实例有nm分别代表城市数和道路数,然后m行,每行有abc三个整数,若c==0则代表ab之间是双向的,若c==1,则代表是ab的单向路;

OUTPUT

若可以构建输出"possible",否则"impossible";

分析:

这道题是第一道欧拉回路(混合图求欧拉回路)的题。

 

定义

欧拉通路 (Euler tour)——通过图中每条边一次且仅一次,并且过每一顶点的通路。

欧拉回路 (Euler circuit)——通过图中每条边一次且仅一次,并且过每一顶点的回路。

欧拉图——存在欧拉回路的图。

无向图是否具有欧拉通路或回路的判定

G有欧拉通路的充分必要条件为:连通,G中只有两个奇度顶点(它们分别是欧拉通路的两个端点)

G有欧拉回路(G为欧拉图)G连通,G中均为偶度顶点。

有向图是否具有欧拉通路或回路的判定

D有欧拉通路:D连通,除两个顶点外,其余顶点的入度均等于出度,这两个特殊的顶点中,一个顶点的入度比出度大1,另一个顶点的入度比出度小1

D有欧拉回路(D为欧拉图)D连通,D中所有顶点的入度等于出度。

混合图。混合图也就是无向图与有向图的混合,即图中的边既有有向边也有无向边。

混合图欧拉回路

混合图欧拉回路用的是网络流。

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

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

现在的问题就变成了:该改变哪些边,可以让每个点出 入?构造网络流模型。有向边不能改变方向,直接删掉。开始已定向的无向边,定的是什么向,就把网络构建成什么样,边长容量上限1。另新建st。对于入 出的点u,连接边(u, t)、容量为x,对于出 入的点v,连接边(s, v),容量为x(注意对不同的点x不同。当初由于不小心,在这里错了好几次)。之后,察看是否有满流的分配。有就是能有欧拉回路,没有就是没有。查看流值分配,将所有流量非 0(上限是1,流值不是0就是1)的边反向,就能得到每点入度 出度的欧拉图。

由于是满流,所以每个入 出的点,都有x条边进来,将这些进来的边反向,OK,入 出了。对于出 入的点亦然。那么,没和st连接的点怎么办?和s连接的条件是出 入,和t连接的条件是入 出,那么这个既没和s也没和t连接的点,自然早在开始就已经满足入 出了。那么在网络流过程中,这些点属于“中间点”。我们知道中间点流量不允许有累积的,这样,进去多少就出来多少,反向之后,自然仍保持平衡。

所以,就这样,混合图欧拉回路问题,解了。

 

总之:

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

首先是对图中的无向边随意定一个方向,然后统计每个点的入度(indeg)和出度(outdeg),如果(indeg - outdeg)是奇数的话,一定不存在欧拉回路;

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

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

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

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

程序:

#include"stdio.h"#include"string.h"#include"queue"#include"stack"#include"iostream"#include"string"#include"map"#include"stdlib.h"#define inf 99999999#define M 1009using namespace std;struct st{    int u,v,w,next,vis;}edge[M*5];int t,head[M],out[M],in[M],dis[M],q[M];void init(){    t=0;    memset(head,-1,sizeof(head));    memset(out,0,sizeof(out));    memset(in,0,sizeof(in));}void add(int u,int v,int w){    edge[t].u=u;    edge[t].v=v;    edge[t].w=w;    edge[t].next=head[u];    head[u]=t++;    edge[t].u=v;    edge[t].v=u;    edge[t].w=0;    edge[t].next=head[v];    head[v]=t++;}int Fabs(int a){    if(a<0)        a=-a;    return a;}int bfs(int S,int T){    memset(dis,-1,sizeof(dis));    int rear=0;    dis[S]=0;    q[rear++]=S;    for(int i=0;i<rear;i++)    {        for(int j=head[q[i]];j!=-1;j=edge[j].next)        {            int v=edge[j].v;            if(edge[j].w&&dis[v]==-1)            {                dis[v]=dis[q[i]]+1;                q[rear++]=v;                if(v==T)                    return 1;            }        }    }    return 0;}int dfs(int cur,int a,int T){    if(cur==T)        return a;    for(int i=head[cur];i!=-1;i=edge[i].next)    {        int v=edge[i].v;        if(edge[i].w&&dis[v]==dis[cur]+1)        {            int tt=dfs(v,min(a,edge[i].w),T);            if(tt)            {                edge[i].w-=tt;                edge[i^1].w+=tt;                return tt;            }        }    }    return 0;}int Dinic(int S,int T){    int ans=0;    while(bfs(S,T))    {        while(int tt=dfs(S,inf,T))            ans+=tt;    }    return ans;}int main(){    int T,i;    cin>>T;    while(T--)    {        int n,m;        while(scanf("%d%d",&n,&m)!=-1)        {            init();            for(i=1;i<=m;i++)            {                int a,b,c;                scanf("%d%d%d",&a,&b,&c);                out[a]++;                in[b]++;                if(c==0)                    add(a,b,1);            }            int flag=0;            for(i=1;i<=n;i++)            {                if(Fabs(out[i]-in[i])&1)                    flag++;            }            if(flag)            {                printf("impossible\n");continue;            }            int tmp=0;            for(i=1;i<=n;i++)            {                if(out[i]>in[i])                    add(0,i,Fabs(out[i]-in[i])/2);                if(in[i]>out[i])                {                    add(i,n+1,Fabs(out[i]-in[i])/2);                    tmp+=Fabs(out[i]-in[i])/2;                }            }            int ans=Dinic(0,n+1);            if(ans==tmp)                printf("possible\n");            else                printf("impossible\n");        }    }}


0 0
原创粉丝点击