HDU

@[网络流判断混合欧拉图]()

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

欧拉回路:

  • 概念:在一个图中 ,从一个点出发,每次走不重复的边,可以将图走完并且回到原点。

  • 判断方法: 每个节点的出度与入度相等(有向图);没有奇度节点的连通图(无向图);

判断是否有奇度节点:

首先统计每个点的度,图中有向边和无向边同时存在,那么思考一下,对于有向边直接计算出入度,对于无向边任意定义一个方向 (明明是无向边,定义成有向边怎么行? 答:我们的目的是用出入度之差判断是否有奇度节点,方向是不影响的) 计算出入度,得出每个节点的出入度之差;对于无向边建图 ,u->v w = 1, v -> u w = 0 (因为无向边只能走一次,所以这样搞)

  • 如果存在奇数差,那么这个图一定不是欧拉回路;
  • 如果差是不为零的偶数 x :
  • * 出度>入度 : 可以将x/2条边反过来,也就是在 源点和该点之间建一条流量为 x 的有向边(同时计算流量 x 的和 sum)
  • 入度>出度 :在该点和汇点之间建一条流量为 x 的有向边

求最大流

若求出的最大流 ans 与 sum 相等,说明中间无累积,节点的出入度相等!


注意!!!

复制 cur 数组时要从 S 到 T!!!

邻接表存图因为用的是Dinic 算法,一定要成对存图,cnt 从偶数开始!!!


这是在网上找的解题报告:

一些常见的混合欧拉图的判断
POJ PKU 1637 混合欧拉图
题目描述:
很裸的混合欧拉图,给你一些有向边,无向边,问你能不能找一个能够走完所有边(有且只有一次)的路。
解题报告:
原来混合图欧拉回路用的是网络流。把该图的无向边随便定向,计算每个点的入度和出度。
如果有某个点出入度之差为奇数,那么肯定不存在欧拉回路。因为欧拉回路要求每点入度 = 出度,
也就是总度数为偶数,存在奇数度点必不能有欧拉回路。
好了,现在每个点入度和出度之差均为偶数。
那么将这个偶数除以2,得x。也就是说,对于每一个点,只要将x条边改变方向(入>出就是变入,出>入就是变出),
就能保证出 = 入。如果每个点都是出 = 入,那么很明显,该图就存在欧拉回路。
  现在的问题就变成了:我该改变哪些边,可以让每个点出 = 入?构造网络流模型。
首先,有向边是不能改变方向的,要之无用,删。一开始不是把无向边定向了吗?
定的是什么向,就把网络构建成什么样,边长容量上限1。另新建s和t。
对于入 > 出的点u,连接边(u, t)、容量为x,对于出 > 入的点v,连接边(s, v),容量为x(注意对不同的点x不同)。
之后,察看是否有满流的分配。有就是能有欧拉回路,没有就是没有。欧拉回路是哪个?
察看流值分配,将所有流量非 0(上限是1,流值不是0就是1)的边反向,就能得到每点入度 = 出度的欧拉图。
由于是满流,所以每个入 > 出的点,都有x条边进来,将这些进来的边反向,OK,入 = 出了。
对于出 > 入的点亦然。那么,没和s、t连接的点怎么办?和s连接的条件是出 > 入,
和t连接的条件是入 > 出,那么这个既没和s也没和t连接的点,自然早在开始就已经满足入 = 出了。
那么在网络流过程中,这些点属于“中间点”。我们知道中间点流量不允许有累积的,
这样,进去多少就出来多少,反向之后,自然仍保持平衡。
所以,就这样,混合图欧拉回路问题,解了。

撸代码:

#include<queue>
#include<stdio.h>
#include<string.h>
#include<algorithm>
#include<iostream>
using namespace std;
struct node
{

    int nex,to,val;
} edge[220000];
int n,m,cnt,head[210],ss,tt;
int book[210],cur[210];
void init()
{
    cnt=0;
    for(int i=0; i<=205; i++)
        head[i]=-1;
}
void addEdge(int u,int v,int w)
{
    edge[cnt].to=v;
    edge[cnt].val=w;
    edge[cnt].nex=head[u];
    head[u]=cnt++;/*注意边号 ,必须成对,0^1 = 1 ,1^1=0 这是一对边*/

    edge[cnt].to=u;
    edge[cnt].val=0;
    edge[cnt].nex=head[v];
    head[v]=cnt++;
}
bool bfs()
{
    memset(book,-1,sizeof(book));
    queue<int>q;
    q.push(ss);
    book[ss]=0;
    while(!q.empty())
    {
        int k=q.front();
        q.pop();
        for(int i=head[k]; i!=-1; i=edge[i].nex)
        {
            int v=edge[i].to;
            if(book[v]<0&&edge[i].val>0)
            {
                book[v]=book[k]+1;
                q.push(v);
            }
        }
    }
    if(book[tt]>=0)return 1;
    return 0;
}
int dfs(int v,int sum)/*Dinic*/
{

    if(v==tt)
        return sum;
    int t,s,i,use=0;
    for(int i=cur[v]; i!=-1; i=edge[i].nex)
    {
        cur[v]=i;
        int to=edge[i].to;
        if(edge[i].val>0&&book[to]==book[v]+1)
        {
            t=dfs(to,min(sum,edge[i].val));
            use+=t;
            edge[i].val-=t;
            edge[i^1].val+=t;
            sum-=t;
        }
        if(sum<=0)
            break;
    }
    if(!use)book[v]=-0x3f3f3f3f;
    return use;
}
int main()
{
    int t;
    scanf("%d",&t);
    while(t--)
    {
        scanf("%d%d",&n,&m);
        init();
        int d[210];
        memset(d,0,sizeof(d));
        ss=0,tt=n+1;
        int u,v,w;
        for(int i=1; i<=m; i++)
        {
            scanf("%d%d%d",&u,&v,&w);
            d[v]++;
            d[u]--;/*入度减出度*/
            if(!w)
            {
                addEdge(u,v,1);
            }
        }
        int flag=0,ans=0;
        for(int i=1; i<=n; i++)
        {
            if(d[i]&1)
            {
                flag=1;
                break;
            }
            if(d[i]>0)/*入度大于出度*/
            {
                addEdge(i,tt,d[i]/2);
            }
            else if(d[i]<0)
            {
                addEdge(ss,i,-d[i]/2);
                ans+=(-d[i]/2);
            }
        }
        if(flag)
        {
            printf("impossible ");
            continue;
        }
        int sum=0;
        while(bfs())
        {
            for(int i=ss; i<=tt; i++)/*!!! 这一点要特别注意*/
                cur[i]=head[i];
            int temp;
            while(temp=dfs(ss,0x3f3f3f3f))
                sum+=temp;
        }
        if(ans==sum)
            printf("possible ");
        else
            printf("impossible ");
    }
    return 0;
}
原文地址:https://www.cnblogs.com/HappyKnockOnCode/p/12695677.html