混合图欧拉回路 POJ 1637

来源:互联网 发布:软件外包公司分录 编辑:程序博客网 时间:2024/05/18 03:07

Sightseeing tour
Time Limit: 1000MS Memory Limit: 10000KTotal Submissions: 7741 Accepted: 3242

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

Source

Northwestern Europe 2003

先随意规定无向边的方向,统计混合图中每个点的出入度,若有一个点的出入度只差为奇数,则不存在欧拉回路,应为无论怎样改变开始时规定的无向图方向,都不可能使改点的出入度之差变成0,来满足欧拉回路的条件。

如果每个点的出入度之差都为偶数,就需要判断有没有可能通过改变初始时规定的无向边方向来使得每个点的出入度之差变成0。

对于每个出度大于入度的节点,从源点s向该点连边,容量为该点出入度之差的一半;对于每个入度大于出度的节点,从该点想汇点t连边,容量为该点出入度之差的一半。并且加入所有刚开始时已经定向的无向边(有向边不加入,应为它们不能反转)按照这样构图后,每一个流都对应了一个改变无向边方向的方案,如果满流,意味着原图存在欧拉回路。

那么此时欧拉图是什么?只需把最大流的网络中的每个有流量的边反转,就是欧拉图。

#include <iostream>#include <cstdio>#include <cstdlib>#include <algorithm>#include <ctype.h>#include <cstring>#include <string>#include <queue>#include <cmath>#define MAXN 220#define MAXM 2050#define INF 2100000000#define pu system("pause")#pragma comment(linker, "/STACK:102400000,102400000")using namespace std;class Edge{    public:    int u, v, next, w;};Edge edge[MAXM];int cnte;int n, m, head[MAXN], in[MAXN], out[MAXN], x[MAXN];int fa[MAXN], cur[MAXN], gap[MAXN], dis[MAXN];void insert(int u, int v, int w){    edge[cnte].u = u; edge[cnte].v = v;    edge[cnte].w = w; edge[cnte].next = head[u]; head[u] = cnte++;    edge[cnte].u = v; edge[cnte].v = u;    edge[cnte].w = 0; edge[cnte].next = head[v]; head[v] = cnte++;}int sap(int s, int t, int n){memset(fa, -1, sizeof(fa));memcpy(cur, head, sizeof(head));//初始时当前弧就是当前节点的第一条弧//不使用bfs时加上这三条memset(dis, 0, sizeof(dis));memset(gap, 0, sizeof(gap));gap[0] = n;//初始时距离为0的节点有n个//bfs(t, n);int lowf = INF, top = s, flow = 0;//lowf是增广路上的最小流量,top是增广过程最前端的节点,flow待返回的流量while(dis[s] < n)//当汇点不可达时,dis[s]的值会被更改为节点总数n{bool flag = 0;//标记通过top节点能否找到允许弧for(int i = cur[top]; i != -1; i = edge[i].next)//从当前弧开始找允许弧{int v = edge[i].v;if(dis[top] == dis[v]+1 && edge[i].w > 0)//找到允许弧{flag = 1;//更改标记cur[top] = i;//更改当前节点的当前弧,下次搜索时从这条弧开始lowf = min(lowf, edge[i].w);//更新增广路上的流量fa[v] = top;//记录父节点top = v;//更改top节点if(top == t)//如果找到终点,说明找到一条增广路,更新总流量{flow += lowf;while(top != s)//沿父节点回溯更新残余网络{top = fa[top];edge[cur[top]].w -= lowf;edge[cur[top]^1].w += lowf;}lowf = INF;//重置最小流量}break;}}//如果没找到允许弧,撤退,更改当前top节点的距离值int mindis;//更改top节点的距离值if(!flag){mindis = n;//初始化为节点总数for(int i = head[top]; i != -1; i = edge[i].next){if(edge[i].w > 0 && dis[edge[i].v] < mindis)//如果top节点能从距离比当前节点的距离更小的节点转移来{mindis = dis[edge[i].v];//更新top节点的距离值cur[top] = i;//修改top节点的当前弧}}if((--gap[dis[top]]) == 0) break;//gap优化,如果节点距离值出现断层,必然找不到增广路,直接退出gap[dis[top] = mindis+1]++;//更新top节点的距离值以及gap数组if(top != s) top = fa[top];//top撤退到它的父节点}}return flow;}int main(){//freopen("C:/Users/zts/Desktop/in.txt", "r", stdin);    int t; scanf("%d", &t);    while(t--)    {        memset(head, -1, sizeof(head));        memset(in, 0, sizeof(in));        memset(out, 0, sizeof(out));        cnte = 0;        scanf("%d%d", &n, &m);        int u, v, kind;        for(int i = 0; i < m; i++)        {            scanf("%d%d%d", &u, &v, &kind);            if(!kind) insert(u, v, 1);            in[v]++; out[u]++;        }        bool ok = 1;        for(int i = 1; i <= n; i++)        {            if(abs(out[i]-in[i])&1)            {                ok = 0;                break;            }            else            {                x[i] = (out[i]-in[i])/2;            }        }        if(!ok)        {            printf("impossible\n");            continue;        }        int s = 0, e = n+1;        int flowin = 0, flowout = 0;        bool allzero = 1;        for(int i = 1; i <= n; i++)        {            if(x[i] > 0) insert(s, i, abs(x[i])), flowin += abs(x[i]), allzero = 0;            else if(x[i] < 0) insert(i, e, abs(x[i])), flowout += abs(x[i]), allzero = 0;        }        if(allzero)        {            printf("possible\n");            continue;        }        int ans = sap(s, e, e+1);        if(ans == flowin && ans == flowout) printf("possible\n");        else printf("impossible\n");    }    return 0;}

0 0