poj 1637判定混合图欧拉回路 uva10735 混合欧拉回路+路径输出
来源:互联网 发布:飞秋软件下载 编辑:程序博客网 时间:2024/06/08 05:07
Description
Input
Output
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
此题求欧拉回路啊。
把该图的无向边随便定向,计算每个点的入度和出度。如果有某个点出入度之差为奇数,那么肯定不存在欧拉回路。因为欧拉回路要求每点入度 = 出度,也就是总度数为偶数,存在奇数度点必不能有欧拉回路。好了,现在每个点入度和出度之差均为偶数。那么将这个偶数除以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;}/*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*/
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 followsE lines specifying the edges. Each such line will be in the format a b type wherea 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 ata 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<<25struct 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;}
- poj 1637判定混合图欧拉回路 uva10735 混合欧拉回路+路径输出
- UVA10735 混合图欧拉回路
- POJ 1637 混合图欧拉回路的判定
- POJ 1637 混合图欧拉回路判定
- POJ 1637 混合欧拉回路
- UVA 10735 最大流 混合欧拉回路 输出路径
- UVA 10735 最大流 混合欧拉回路 输出路径
- UVA 10735 混合图的欧拉回路+输出路径
- Poj 1637 Sightseeing tour (混合图的欧拉回路判定)
- POJ-1637 Sightseeing tour(通过网络流判定混合图的欧拉回路)
- poj 1637(混合图欧拉回路)
- 【混合图欧拉回路】POJ 1637
- POJ 1637 混合图欧拉回路
- 混合图欧拉回路 POJ 1637
- 混合图的欧拉回路 POJ 1637
- poj 1637 & zoj 1992 混合图的欧拉回路
- POJ 1637 混合图的欧拉回路
- POJ 1637 Sightseeing tour (混合图的欧拉回路)
- 【转载】hibernate组件映射
- 获取程序的版本
- cf262b
- 如何进行输入确认
- 查看linux ssh服务信息及运行状态
- poj 1637判定混合图欧拉回路 uva10735 混合欧拉回路+路径输出
- 八年级作文-信箱上的油漆印
- Why doesn't C++ provide heterogenous containers?
- 大小端的几种判断方法
- powder designer 与数据库
- JAVA类的生命周期
- asp.net 返回上一页的实现方法小集
- 线程的互斥
- 大话设计模式4 简单工厂模式VS工厂模式