poj 1637 Sightseeing tour(混合欧拉)

46 篇文章 0 订阅
5 篇文章 0 订阅
Sightseeing tour
Time Limit: 1000MS Memory Limit: 10000K
Total Submissions: 6937 Accepted: 2877

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
 
题意:给出一个混合图,有无向边和有向边,问是否存在欧拉回路。
思路:首先对于每条无向边,随便定向,把它变为有向边。对于每条边(包括原来有向的和后来变有向的),统计出度和入度,若存在一个点|出度-入度|为奇数,那么就不存在欧拉回路。如果不存在这样的点,就要用网络流来判断。
构图如下:
对于某个点,若出度>入度,则从源点连一条边到该点,容量为(出度-入度)/2; 若入度>出度,则从该点连一条边到汇点,容量为(入度-出度)/2。
然后,对于每条原来是无向的边,根据前面的定向,连一条边,容量为1。
最后,判断是否满流。若满流,则存在欧拉回路。

AC代码:
#include <iostream>
#include <cmath>
#include <cstdlib>
#include <cstring>
#include <cstdio>
#include <queue>
#include <ctime>
#include <algorithm>
#define ll __int64

using namespace std;

const int INF = 1000000000;
const int maxn = 205;

struct Edge
{
    int u, v, cap, flow, next;
} et[maxn * maxn];
int low[maxn], cnt[maxn], cur[maxn], dis[maxn], pre[maxn], eh[maxn];
int in[maxn], out[maxn];
int n, m, s, t, num;
void init()
{
    memset(eh, -1, sizeof(eh));
    num = 0;
}
void add(int u, int v, int cap, int flow)
{
    Edge e = {u, v, cap, flow, eh[u]};
    et[num] = e;
    eh[u] = num++;
}
void addedge(int u, int v, int cap)
{
    add(u, v, cap, 0);
    add(v, u, 0, 0);
}
int isap(int s, int t, int nv)
{
    int u, v, now, flow = 0;
    memset(cnt, 0, sizeof(cnt));
    memset(dis, 0, sizeof(dis));
    memset(low, 0, sizeof(low));
    for(u = 0; u <= nv; u++) cur[u] = eh[u];
    low[s] = INF, cnt[0] = nv, u = s;
    while(dis[s] < nv)
    {
        for(now = cur[u]; now != -1; now = et[now].next)
            if(et[now].cap - et[now].flow && dis[u] == dis[v = et[now].v] + 1) break;
        if(now != -1)
        {
            cur[u] = pre[v] = now;
            low[v] = min(et[now].cap - et[now].flow, low[u]);
            u = v;
            if(u == t)
            {
                for(; u != s; u = et[pre[u]].u)
                {
                    et[pre[u]].flow += low[t];
                    et[pre[u]^1].flow -= low[t];
                }
                flow += low[t];
                low[s] = INF;
            }
        }
        else
        {
            if(--cnt[dis[u]] == 0) break;
            dis[u] = nv, cur[u] = eh[u];
            for(now = eh[u]; now != -1; now = et[now].next)
                if(et[now].cap - et[now].flow && dis[u] > dis[et[now].v] + 1)
                    dis[u] = dis[et[now].v] + 1;
            cnt[dis[u]]++;
            if(u != s) u =et[pre[u]].u;
        }
    }
    return flow;
}
int main()
{
    int tt;
    int a, b, c;
    scanf("%d", &tt);
    while(tt--)
    {
        scanf("%d%d", &n, &m);
        init();
        s = 0;
        t = n + 1;
        memset(in, 0, sizeof(in));
        memset(out, 0, sizeof(out));
        while(m--)
        {
            scanf("%d%d%d", &a, &b, &c);
            out[a]++;
            in[b]++;
            if(c != 1) addedge(a, b, 1);
        }
        bool possible = true;
        int sum = 0;
        for(int i = 1; i <= n; i++)
        {
            if(abs(out[i] - in[i]) & 1)
            {
                possible = false;
                break;
            }
            if(out[i] > in[i])
            {
                addedge(s, i, (out[i] - in[i]) / 2);
                sum += (out[i] - in[i]) / 2;
            }
            else if(in[i] > out[i]) addedge(i, t, (in[i] - out[i]) / 2);
        }
        if(!possible)
        {
            printf("impossible\n");
            continue;
        }
        if(isap(s, t, t + 1) == sum) printf("possible\n");
        else printf("impossible\n");
    }
    return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值