poj 1637判定混合图欧拉回路 uva10735 混合欧拉回路+路径输出

Sightseeing tour
Time Limit: 1000MS Memory Limit: 10000K
Total Submissions: 6103 Accepted: 2510

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,得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连接的
点,自然早在开始就已经满足入=出了。那么在网络流过程中,这些点属于“中
间点”。我们知道中间点流量不允许有累积的,这样,进去多少就出来多少,反
向之后,自然仍保持平衡。
所以,就这样,混合图欧拉回路问题,解了。
 
纠结很久不知为何错,看着别人的代码,改了又改,最后几乎成别人的代码了,算是过了,唉,囧……………………
#include<iostream>
#include<algorithm>
#include<cstdio>
#include<cstring>
using namespace std;
const int N=4002;
const int M=30002;
const int INF=1<<29;
int n,m;
int gap[M],dis[M],pre[M],head[N],cur[N];
int NE,NV,flag,t,in[N],out[N];
struct Node
{
    int c,pos,next;
} E[M*4];
#define FF(i,NV) for(int i=0;i<NV;i++)
int sap(int s,int t)
{
    memset(dis,0,sizeof(int)*(NV+1));
    memset(gap,0,sizeof(int)*(NV+1));
    FF(i,NV) cur[i] = head[i];
    int u = pre[s] = s,maxflow = 0,aug =INF;
    gap[0] = NV;
    while(dis[s] < NV)
    {
loop:
        for(int &i = cur[u]; i != -1; i = E[i].next)
        {
            int v = E[i].pos;
            if(E[i].c && dis[u] == dis[v] + 1)
            {
                aug=min(aug,E[i].c);
                pre[v] = u;
                u = v;
                if(v == t)
                {
                    maxflow += aug;
                    for(u = pre[u]; v != s; v = u,u = pre[u])
                    {
                        E[cur[u]].c -= aug;
                        E[cur[u]^1].c += aug;
                    }
                    aug =INF;
                }
                goto loop;
            }
        }
        if( (--gap[dis[u]]) == 0)   break;
        int mindis = NV;
        for(int i = head[u]; i != -1 ; i = E[i].next)
        {
            int v = E[i].pos;
            if(E[i].c && mindis > dis[v])
            {
                cur[u] = i;
                mindis = dis[v];
            }
        }
        gap[ dis[u] = mindis+1 ] ++;
        u = pre[u];
    }
    return maxflow;
}
void addedge(int u,int v,int c )
{
    E[NE].c = c;
    E[NE].pos = v;
    E[NE].next = head[u];
    head[u] = NE++;
    E[NE].c = 0;
    E[NE].pos = u;
    E[NE].next = head[v];
    head[v] = NE++;
}
int main()
{
    cin>>t;
    while (t--)
    {
        scanf("%d %d",&n,&m);
        NE = 0;NV=n+2;
        int i,u, v, c, src = 0, des = n + 1;
        for(i=0;i<=NV;i++)
        {
            head[i]=-1;
            in[i]=0;
            out[i]=0;
        }
        int sum=0;
        int flag=0;
        for(int i=0;i<m;i++)
        {
            cin>>u>>v>>c;
            out[u]++;
            in[v]++;
            if(!c)
            addedge(u,v,1);
        }
        for(int i=1;i<=n;i++)
        {
            int tmp=(in[i]-out[i])/2;
            if(abs(in[i]-out[i])%2!=0)
            {
                flag=1;
                break;
            }
            if(tmp<0)
            {
                addedge(src,i,-tmp);
                sum+=-tmp;

            }
            else
            {
                 addedge(i,des,tmp);
            }
        }

        if(flag)
        cout<<"impossible"<<endl;
        else
        {
            int ss=sap(src,des);
            if(sum==ss)
            cout<<"possible"<<endl;
            else
            cout<<"impossible"<<endl;
        }
    }
    return 0;
}

/*
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
*/

 

 

UVA 10735

Problem D
Euler Circuit
Input: standard input
Output: standard output
Time Limit: 2 seconds

An Euler circuit is a graph traversal starting and ending at the same vertex and using every edge exactly once. Finding an Euler circuit in an undirected or directed graph is a fairly easy task, but what about graphs where some of the edges are directed and some undirected? An undirected edge can only be traveled in one direction. However, sometimes any choice of direction for the undirected edges may not yield an Euler circuit.

Given such a graph, determine whether an Euler circuit exists. If so, output such a circuit in the format specified below. You can assume that the underlying undirected graph is connected.

Input

The first line in the input contains the number of test cases, at most 20. Each test case starts with a line containing two numbers, V and E: the number of vertices (1 <= V <= 100) and edges (1 <= E <= 500) in the graph. The vertices are numbered from 1 to V. Then follows E lines specifying the edges. Each such line will be in the format a b type where a and b are two integers specifying the endpoints of the edge. type will either be the character 'U', if the edge is undirected, or 'D', if the edge is directed. In the latter case, the edge starts at a and ends at b.

Output

If an Euler circuit exist, output an order in which the vertices can be traversed on a single line. The vertex numbers should be delimited with a single space, and the start and end vertex should be included both at the beginning and the end of the sequence. Since most graphs have multiple solutions, any valid solution will be accepted. If no solution exist, output the line "No euler circuit exist". Output a blank line between each test case.

Sample Input                               Output for Sample Input

2

6 8

1 3 U

1 4 U

2 4 U

2 5 D

3 4 D

4 5 U

5 6 D

5 6 U

4 4

1 2 D

1 4 D

2 3 U

3 4 U

 

1 3 4 2 5 6 5 4 1

 

No euler circuit exist

 

#include<iostream>
#include<stdio.h>
#include<string.h>
#include<queue>
#include<stack>
#include<algorithm>
using namespace std;
#define maxn 110
#define maxe 3000
#define inf 1<<25

struct Edge
{
    int from;
    int to;
    int next;
    int cap;
    int flow;
} edge[maxe],ee[maxe];

int head[maxn],cur[maxn],vis[maxn],d[maxn];
int degree[maxn];
int s,t,NE;

int Head[maxn];
int ne;
int used[maxe];
stack<int> st;

int m,n;

void init()
{
    NE=0;
    ne=0;
    memset(head,-1,sizeof(head));
    memset(Head,-1,sizeof(Head));
    memset(degree,0,sizeof(degree));
}
void addEdge(int u,int v,int c)
{
    edge[NE].from=u;
    edge[NE].to=v;
    edge[NE].cap=c;
    edge[NE].flow=0;
    edge[NE].next=head[u];
    head[u]=NE++;

    edge[NE].from=v;
    edge[NE].to=u;
    edge[NE].cap=0;
    edge[NE].flow=0;
    edge[NE].next=head[v];
    head[v]=NE++;
}

bool bfs()
{
    memset(vis,0,sizeof(vis));
    queue<int> q;
    q.push(s);
    d[s]=0;
    vis[s]=1;
    while(!q.empty())
    {
        int x=q.front();
        q.pop();
        for(int i=head[x]; i!=-1; i=edge[i].next)
        {
            Edge &e=edge[i];
            if(!vis[e.to]&&e.cap>e.flow)
            {
                vis[e.to]=1;
                d[e.to]=d[x]+1;
                q.push(e.to);
            }
        }
    }
    return vis[t];
}

int dfs(int x,int ff)
{
    if(x==t||ff==0)
        return ff;
    int flow=0,f;
    for(int& i=cur[x]; i!=-1; i=edge[i].next)
    {
        Edge& e=edge[i];
        if(d[x]+1==d[e.to]&&(f=dfs(e.to,min(ff,e.cap-e.flow)))>0)
        {
            e.flow+=f;
            edge[i^1].flow-=f;
            flow+=f;
            ff-=f;
            if(ff==0)
                break;
        }
    }
    return flow;
}

int maxFlow()
{
    int flow=0;
    while(bfs())
    {
        for(int i=s; i<=t; i++)
            cur[i]=head[i];
        flow+=dfs(s,inf);
    }
    return flow;
}

void add(int x,int y)
{
    ee[ne].from=x;
    ee[ne].to=y;
    ee[ne].next=Head[x];
    Head[x]=ne++;
}

void build()
{
    for(int i=1; i<=n; i++)
    {
        for(int j=head[i]; j!=-1; j=edge[j].next)
        {
            Edge &e=edge[j];
            if(e.to>=1&&e.to<=n&&e.cap)
            {
                if(e.flow==0)
                    add(i,e.to);
                else
                    add(e.to,i);
            }
        }
    }
}
//路径输出
void dfsPath(int x,int id)
{
    used[id]=1;
    for(int i=Head[x]; i!=-1; i=ee[i].next)
    {
        if(!used[i])
            dfsPath(ee[i].to,i);
    }
    st.push(x);
}

int main()
{
    int cas;
    cin>>cas;
    while(cas--)
    {
        init();
        int i;
        scanf("%d%d",&n,&m);
        s=0;
        t=n+1;
        int sum=0;
        for(i=0; i<m; i++)
        {
            char c;
            int a,b;
            scanf("%d %d %c",&a,&b,&c);
            degree[a]--;
            degree[b]++;
            if(c=='D')
                add(a,b);
            else
                addEdge(a,b,1);
        }

        for(i=1; i<=n; i++)
        {
            if(degree[i]&1)
                break;
        }

        if(i!=n+1)
        {
            printf("No euler circuit exist\n");
            if(cas)
            printf("\n");
            continue;
        }
        else
        {
            for(i=1; i<=n; i++)
            {
                if(degree[i]<0)
                {
                    sum-=degree[i]/2;
                    addEdge(s,i,-degree[i]/2);
                }
                else
                {
                    addEdge(i,t,degree[i]/2);
                }
            }

            if(maxFlow()!=sum)
            {
                printf("No euler circuit exist");
            }
            else
            {
                build();
                memset(used,0,sizeof(used));
                dfsPath(1,ne+10);
                int flag=0;
                while(!st.empty())
                {
                    if(flag)
                        printf(" ");
                    flag=1;
                    printf("%d",st.top());
                    st.pop();
                }
            }
             printf("\n");
        }
        if(cas)
            printf("\n");
    }
    return 0;
}


 


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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值