POJ 1637 Sightseeing tour (网络流解决混合图欧拉回路问题)

来源:互联网 发布:timcat如何绑定域名 编辑:程序博客网 时间:2024/05/17 20:27

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
此题题意:给你n个点,m条边,x,y,d表示哪两个点相连,d=0表示为双向边,d=1表示为单向边x->y,问你可不可能找出这么一个点,使得从这个点出发,经过每条边仅一次,然后回到该点,每条边都必须走遍。
分析:欧拉回路分为有向图,无向图,混合图。显然此题就是混合图,既有有向边,又有无向边,下面粘贴一个在网上找到的关于欧拉回路的总结:
欧拉回路问题。
1 定义
欧拉通路 —— 通过图中每条边一次且仅一次,并且过每一顶点的通路。
欧拉回路——通过图中每条边一次且仅一次,并且过每一顶点的回路。
欧拉图——存在欧拉回路的图。
2 无向图是否具有欧拉通路或回路的判定
G有欧拉通路的充分必要条件为:G 连通,G中只有两个奇度顶点(它们分别是欧拉通路的两个端点)。
G有欧拉回路(G为欧拉图):G连通,G中均为偶度顶点。
3 有向图是否具有欧拉通路或回路的判定
D有欧拉通路:D连通,除两个顶点外,其余顶点的入度均等于出度,这两个特殊的顶点中,一个顶点的入度比出度大1,另一个顶点的入度比出度小1。
D有欧拉回路(D为欧拉图):D连通,D中所有顶点的入度等于出度。
4 混合图。混合图也就是无向图与有向图的混合,即图中的边既有有向边也有无向边。
5 混合图欧拉回路(混合图欧拉回路用的是网络流)
把该图的无向边随便定向(按题目给的默认顺序就行),计算每个点的入度和出度。如果有某个点出入度之差为奇数,那么肯定不存在欧拉回路,因为欧拉回路要求每点入度 = 出度,也就是总度数为偶数,存在奇数度点必不能有欧拉回路。 现在每个点入度和出度之差均为偶数。将这个偶数除以 2,得 x。即是说,对于每一个点,只要将 x 条边反向(入>出就是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 连接的点,自然早在开始就已经满足入 = 出了。那么在网络流过程中,这些点属于“中间点”。我们知道中间点流量不允许有累积的,这样,进去多少就出来多少,反向之后,自然仍保持平衡。
dinici模板为大白书上的模板:
#include<stdio.h>#include<string.h>#include<vector>#include<queue>#include<algorithm>#define maxn 2000#define LL long long#define inf 0x7fffffff#define INF 1e18using namespace std;struct edge{    int from,to,cap,flow;};vector<edge> edges;vector<int> G[maxn];int d[maxn],cur[maxn];bool vis[maxn];int s,t;void init(){    for(int i=0;i<maxn;i++) G[i].clear();    edges.clear();}void add(int from,int to,int cap){    edges.push_back((edge){from,to,cap,0});    edges.push_back((edge){to,from,0,0});    int m=edges.size();    G[from].push_back(m-2);    G[to].push_back(m-1);}bool bfs(){    memset(vis,false,sizeof(vis));    memset(d,0,sizeof(d));    queue<int> q;    q.push(s);    d[s]=0,vis[s]=true;    while(!q.empty())    {        int x=q.front();        q.pop();        for(int i=0;i<G[x].size();i++)        {            edge& e=edges[G[x][i]];            if(!vis[e.to]&&e.cap>e.flow)            {                vis[e.to]=true;                d[e.to]=d[x]+1;                q.push(e.to);            }        }    }    return vis[t];}int dfs(int x,int a){    if(x==t||a==0) return a;    int flow=0,f;    for(int& i=cur[x];i<G[x].size();i++)    {        edge& e=edges[G[x][i]];        if(d[x]+1==d[e.to]&&(f=dfs(e.to,min(a,e.cap-e.flow)))>0)        {            e.flow+=f;            edges[G[x][i]^1].flow-=f;            flow+=f;            a-=f;            if(a==0) break;        }    }    return flow;}int maxflow(){    int flow=0;    while(bfs())    {        memset(cur,0,sizeof(cur));        flow+=dfs(s,inf);    }    return flow;}int in[300],out[300];///记录每个点的入度和出度int main(){    int T;    scanf("%d",&T);    while(T--)    {        int m,p,x,y,d;        scanf("%d%d",&m,&p);        init();        s=0,t=m+1;        memset(in,0,sizeof(in));        memset(out,0,sizeof(out));        for(int i=1;i<=p;i++)        {            scanf("%d%d%d",&x,&y,&d);            in[y]++,out[x]++;            if(!d) add(x,y,1);///无向边建图,有向边不用管        }        int flog=1;        for(int i=1;i<=m;i++)        {            if(abs(in[i]-out[i])&1)///入度和出度之差为奇数,不存在这样的欧拉回路            {                flog=0;                break;            }        }        if(!flog)        {            puts("impossible");            continue;        }        int sum=0;        for(int i=1;i<=m;i++)        {            if(in[i]>out[i]) add(i,t,(in[i]-out[i])>>1),sum+=(in[i]-out[i])>>1;            if(in[i]<out[i]) add(s,i,(out[i]-in[i])>>1);        }        if(maxflow()==sum) puts("possible");        else puts("impossible");    }    return 0;}

0 0
原创粉丝点击