Sightseeing tour POJ - 1637(混合欧拉回路判定)

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

这题可以作为混合欧拉图判断的标准题(也算是裸题吧),混合的意思就是图中既有有向边又有无向边,这类的判定可以转化成网络流来做(实际上的原理是根据了有向图中,若要满足欧拉回路,入读必须等于出度),因为对于无想边,对走过这条边是有两种可能的(显然),而实际上,若此图可以构成欧拉回路,必然是选择了一个方向,那么假设一开始我们给定了方向(对于无向图),然后判断每个点的出度和入读情况,但是万一方向反了,入度不等于出度怎么办(实际上是经常要发生的),那么就是通过网络流来改变边的方向(逻辑上),还有一个,若一个点出度和入度差为奇数是肯定构不成回路,因为即使调整了一条边的方向,其差值仍为奇数,在排除了奇数情况,我们设一个超级源点s,把所有出度大于入读的点与其相连,边的权值为 (out[i]in[i])/2 ,还有一个超级汇点t,把所有如度大于出读的点与其相连,边的权值为 (in[i]out[i])/2 ,还有在点与点之间,只建立无向边,有向边删除,因为边的方向不能改变只对每个点的出入度的判断有贡献
大体流程就是:
1.先通过并查集判断图的连通
2.在1基础上判断每个点是否存在度数差为奇数的点
3.在2的基础上通过网络流判断

#include<cstdio>
#include<iostream>
#include<algorithm>
#include<cstring>
#include<cmath>
#include<set>
#include<vector>
using namespace std;
int n,m;
struct node
{
    int to;
    int flow;//这个队列里的元素没有最小流的记录原因是,每个边容量最大只能是1,一旦s和t连通,只能就是加1
}edge[4005];
vector<int> graph[205];
int k;
void addEdge(int x,int y,int c)//建边的同时建反向边
{
    edge[k].to=y;
    edge[k].flow=c;
    graph[x].push_back(k);
    k++;
    edge[k].to=x;
    edge[k].flow=0;
    graph[y].push_back(k);
    k++;
}
int in[205],out[205];
int pre[205];
int sum;//记录出度减入读大于0的和
int findd(int x)
{
    return pre[x]==x?x:pre[x]=findd(pre[x]);
}
void init(int n)
{
    for(int i=0;i<=n+1;i++)
        graph[i].clear();
    for(int i=1;i<=n;i++)
        pre[i]=i;
    memset(in,0,sizeof(in));
    memset(out,0,sizeof(out));
    k=0;
    sum=0;
}
struct se
{
    int point;
    int index;
    int lastIndex;
}que[205],start,endd;
bool vis[205];
int head,tail;
bool work()
{
    int ans=0;
    bool sign;
    for(;;)
    {
        //cout<<1<<endl;
        sign=false;
        memset(vis,false,sizeof(vis));
        head=tail=0;
        que[tail++]=start;
        vis[0]=true;
        while(head<tail)
        {
            //cout<<2<<endl;
            se now=que[head++];
            int p=now.point;
            for(int i=0;i<graph[p].size();i++)
            {
                int v=edge[graph[p][i]].to;
                int  f=edge[graph[p][i]].flow;
                if(vis[v]||f==0)
                    continue;
                if(v==n+1)
                {
                    endd.index=graph[p][i];
                    endd.lastIndex=head-1;
                    endd.point=v;
                    sign=true;
                    break;
                }
                else
                {
                    se temp;
                    temp.index=graph[p][i];
                    temp.lastIndex=head-1;
                    temp.point=v;
                    vis[v]=true;
                    que[tail++]=temp;
                }
            }
        }
        if(sign)
        {
            ans++;
            while(endd.lastIndex!=-1)//修改边的权值
            {
                //cout<<3<<endl;
                edge[endd.index].flow--;
                if((endd.index)&1)
                    edge[endd.index-1].flow++;
                else
                    edge[endd.index+1].flow++;
                endd=que[endd.lastIndex];
            }
        }
        else
            break;
    }
    //cout<<"ans:"<<ans<<endl;
    return ans==sum;
}
int main()
{
    int t;
    int x,y,s;
    start.lastIndex=-1;
    start.point=0;
    scanf("%d",&t);
    while(t--)
    {
        scanf("%d%d",&n,&m);
        init(n);
        while(m--)
        {
            scanf("%d%d%d",&x,&y,&s);
            out[x]++;
            in[y]++;
            if(!s)//只保留无向边的建图
                addEdge(x,y,1);
            int xx=findd(x);
            int yy=findd(y);
            if(xx!=yy)
                pre[xx]=yy;

        }
        int countt=0;
        for(int i=1;i<=n;i++)
            if(pre[i]==i)
            {
                countt++;
                if(countt>1)
                    break;
            }
        if(countt>1)
            printf("impossible\n");/先是并查集判断连通性
        else
        {
            bool sign=false;
            for(int i=1;i<=n;i++)
            {
                if(abs(in[i]-out[i])&1)//判断奇性
                {
                    sign=true;
                    break;
                }
                else
                {
                    if(out[i]>in[i])
                    {
                        sum+=(out[i]-in[i])>>1;
                        addEdge(0,i,(out[i]-in[i])>>1);
                    }
                    if(out[i]<in[i])
                        addEdge(i,n+1,(in[i]-out[i])>>1);
                }
            }
            if(sign)
                printf("impossible\n");
            else
                if(work())//最大流判断即可
                    printf("possible\n");
                else
                    printf("impossible\n");
        }
    }
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值