POJ 1637 Sightseeing tour (混合图欧拉回路,网络最大流)

http://poj.org/problem?id=1637


Sightseeing tour

Time Limit: 1000MS   Memory Limit: 10000K
Total Submissions: 7498   Accepted: 3123

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

Northwestern Europe 2003

混合图的欧拉回路求解方法为:

任意给无向边加方向,然后统计图中点的入度出度,如果所有点的入度和出度的差为偶数则有可能构成欧拉回路;

删除原来的有向边,加方向的无向边容量为1;

增加源点和汇点,如果某点入度大于出度,则和汇点相连,容量为(入度-出度)/2;如果某点出度大于入度,则源点与其相连,容量为(出度-入度)/2;

在新图中跑最大流,如果满流则有可能构成欧拉回路。

#include<cstdio>
#include<iostream>
#include<cstdlib>
#include<algorithm>
#include<ctime>
#include<cctype>
#include<cmath>
#include<string>
#include<cstring>
#include<stack>
#include<queue>
#include<list>
#include<vector>
#include<map>
#include<set>
#define sqr(x) ((x)*(x))
#define LL long long
#define itn int
#define INF 0x3f3f3f3f
#define PI 3.1415926535897932384626
#define eps 1e-10
#define maxm 4004
#define maxn 404

using namespace std;

int fir[maxn];
int u[maxm],v[maxm],cap[maxm],flow[maxm],nex[maxm];
int e_max;
int iter[maxn],q[maxn],lv[maxn];

void add_edge(int _u,int _v,int _w)
{
    int e;
    e=e_max++;
    u[e]=_u;v[e]=_v;cap[e]=_w;
    nex[e]=fir[u[e]];fir[u[e]]=e;
    e=e_max++;
    u[e]=_v;v[e]=_u;cap[e]=0;
    nex[e]=fir[u[e]];fir[u[e]]=e;
}

void dinic_bfs(int s)
{
    int f,r;
    memset(lv,-1,sizeof lv);
    q[f=r=0]=s;
    lv[s]=0;
    while(f<=r)
    {
        int x=q[f++];
        for (int e=fir[x];~e;e=nex[e])
        {
            if (cap[e]>flow[e] && lv[v[e]]<0)
            {
                lv[v[e]]=lv[u[e]]+1;
                q[++r]=v[e];
            }
        }
    }
}

int dinic_dfs(int _u,int t,int _f)
{
    if (_u==t)  return _f;
    for (int &e=iter[_u];~e;e=nex[e])
    {
        if (cap[e]>flow[e] && lv[_u]<lv[v[e]])
        {
            int _d=dinic_dfs(v[e],t,min(_f,cap[e]-flow[e]));
            if (_d>0)
            {
                flow[e]+=_d;
                flow[e^1]-=_d;
                return _d;
            }
        }
    }

    return 0;
}

int max_flow(int s,int t)
{

    memset(flow,0,sizeof flow);
    int total_flow=0;

    for (;;)
    {
        dinic_bfs(s);
        if (lv[t]<0)    return total_flow;
        memcpy(iter,fir,sizeof iter);
        int _f;

        while ((_f=dinic_dfs(s,t,INF))>0)
            total_flow+=_f;
    }

    return total_flow;
}

struct EDGE
{
    int u,v,w;
}edge[1111];
int in[222],out[222];

int main()
{
    #ifndef ONLINE_JUDGE
        freopen("/home/fcbruce/文档/code/t","r",stdin);
    #endif // ONLINE_JUDGE

    int T_T;
    unsigned long long seed=~0ULL;
    scanf("%d",&T_T);

    while (T_T--)
    {
        int n,m,s,t,full_flow=0;
        e_max=0;
        memset(fir,-1,sizeof fir);
        memset(in,0,sizeof in);
        memset(out,0,sizeof out);
        scanf("%d%d",&n,&m);
        seed=seed^T_T^n^m^(~0ULL);

        srand(seed);

        s=0;t=n+1;

        for (int i=0;i<m;i++)
        {
            scanf("%d %d %d",&edge[i].u,&edge[i].v,&edge[i].w);
            if (edge[i].w==1)
            {
                out[edge[i].u]++;
                in[edge[i].v]++;
                edge[i].w=0;
                continue;
            }
            if (rand()&1)
            {
                edge[i].w=1;
                out[edge[i].u]++;
                in[edge[i].v]++;
                add_edge(edge[i].u,edge[i].v,1);
            }
            else
            {
                edge[i].w=-1;
                out[edge[i].v]++;
                in[edge[i].u]++;
                add_edge(edge[i].v,edge[i].u,1);
            }
        }

        bool flag=true;
        for (int i=1;i<=n;i++)
        {
            if (abs(in[i]-out[i])&1)
            {
                flag=false;
                break;
            }
            else
            {
                if (in[i]==out[i])  continue;
                if (in[i]>out[i])
                    add_edge(i,t,in[i]-out[i]>>1);
                else
                    add_edge(s,i,out[i]-in[i]>>1),full_flow+=out[i]-in[i]>>1;
            }
        }

        if (!flag)
        {
            puts("impossible");
            continue;
        }
        else
        {
            if (max_flow(s,t)==full_flow)
                puts("possible");
            else
                puts("impossible");
        }

    }

    return 0;
}

POJ 1637 Sightseeing tour (混合图欧拉回路,网络最大流)

时间: 2024-12-20 01:05:58

POJ 1637 Sightseeing tour (混合图欧拉回路,网络最大流)的相关文章

POJ 1637 Sightseeing tour 混合图欧拉回路存在性判断

没有想到网络流还能解决这一类问题,完全想不到@[email protected] 一开始把所有的无向边制定任意方向有当做有向边看,然后统计每个点的入度和出度.以前有向图的欧拉回路判定是每个点的入读都等于出度,这样可以保证可以回到起点,现在在一些边可以调换方向的情况下,所有定点的入度和出度之差必定为偶数,因为调换任意一条边的方向都会使两个定点的入度和出度变化2,所以要构成一个欧拉回路所有点的入度和出度之差都为偶数,并设差为deg. 现在问题转化成了能否通过改变一些边的方向来是的所有点的入度出度都为

POJ 1637 Sightseeing tour 混合图欧拉回路 最大流

题目大意:给出一张混合图,问是否存在欧拉回路. 思路:成题,直接看题解吧. http://www.cnblogs.com/Lyush/archive/2013/05/01/3052847.html CODE: #include <queue> #include <cstdio> #include <cstring> #include <iostream> #include <algorithm> #define MAX 510 #define M

POJ 1637 Sightseeing tour (混合图欧拉回路)

Sightseeing tour 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 visit

POJ 1637 Sightseeing tour(混合图的欧拉回路)

题目链接 建个图,套个模板. #include <cstdio> #include <cstring> #include <iostream> #include <map> #include <algorithm> #include <vector> #include <string> #include <queue> using namespace std; #define INF 0x3ffffff str

POJ1637 Sightseeing tour (混合图欧拉回路)(网络流)

Sightseeing tour Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 9100   Accepted: 3830 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 beauti

poj1637 Sightseeing tour 混合图欧拉回路判定

传送门 第一次做这种题, 尽管ac了但是完全不知道为什么这么做. 题目就是给一些边, 有向边与无向边混合, 问你是否存在欧拉回路. 做法是先对每个点求入度和出度, 如果一条边是无向边, 就随便指定一个方向, 然后连一条边, 权值为1. 最后统计入度出度, 如果一个点的(入度-出度)%2==1, 就说明不存在欧拉回路. 如果全都满足, 就判断每个点的入度出度的大小关系, 入度>出度, 就向汇点连一条边, 权值为(入度-出度)/2, 相反的话就向源点连边. 跑一遍最大流, 看是否满流, 如果满流就说

POJ 1637 Sightseeing tour (混合图欧拉回路)

POJ 1637 Sightseeing tour 链接:http://poj.org/problem?id=1637 题意:给定一个混合图,既有有向边,又有无向边,问是否存在欧拉回路. 思路: 1 定义 欧拉通路 (Euler tour)--通过图中每条边一次且仅一次,并且过每一顶点的通路. 欧拉回路 (Euler circuit)--通过图中每条边一次且仅一次,并且过每一顶点的回路. 欧拉图--存在欧拉回路的图. 2 无向图是否具有欧拉通路或回路的判定 G有欧拉通路的充分必要条件为:G 连通

POJ 1637 Sightseeing tour(最大流)

POJ 1637 Sightseeing tour 题目链接 题意:给一些有向边一些无向边,问能否把无向边定向之后确定一个欧拉回路 思路:这题的模型很的巧妙,转一个http://blog.csdn.net/pi9nc/article/details/12223693 先把有向边随意定向了,然后依据每一个点的入度出度之差,能够确定每一个点须要调整的次数,然后中间就是须要调整的边,容量为1,这样去建图最后推断从源点出发的边是否都满流就可以 代码: #include <cstdio> #includ

POJ 1637 Sightseeing tour(混合图欧拉回路+最大流)

http://poj.org/problem?id=1637 题意:给出n个点和m条边,这些边有些是单向边,有些是双向边,判断是否能构成欧拉回路. 思路: 构成有向图欧拉回路的要求是入度=出度,无向图的要求是所有顶点的度数为偶数. 但不管是那个,顶点的度数若是奇数,那都是不能构成的. 这道题目是非常典型的混合图欧拉回路问题,对于双向边,我们先随便定个向,然后就这样先记录好每个顶点的入度和出度. 如果有顶点的度数为奇数,可以直接得出结论,是不能构成欧拉回路的. 那么,如果都是偶数呢? 因为还会存在