POJ 2230 Watchcow 欧拉回路输出解
来源:互联网 发布:标签 热敏打印机 编程 编辑:程序博客网 时间:2024/06/14 04:39
Watchcow
Time Limit: 3000MS Memory Limit: 65536K
Total Submissions: 8444 Accepted: 3673 Special Judge
Description
Bessie’s been appointed the new watch-cow for the farm. Every night, it’s her job to walk across the farm and make sure that no evildoers are doing any evil. She begins at the barn, makes her patrol, and then returns to the barn when she’s done.
If she were a more observant cow, she might be able to just walk each of M (1 <= M <= 50,000) bidirectional trails numbered 1..M between N (2 <= N <= 10,000) fields numbered 1..N on the farm once and be confident that she’s seen everything she needs to see. But since she isn’t, she wants to make sure she walks down each trail exactly twice. It’s also important that her two trips along each trail be in opposite directions, so that she doesn’t miss the same thing twice.
A pair of fields might be connected by more than one trail. Find a path that Bessie can follow which will meet her requirements. Such a path is guaranteed to exist.
Input
Line 1: Two integers, N and M.
Lines 2..M+1: Two integers denoting a pair of fields connected by a path.
Output
- Lines 1..2M+1: A list of fields she passes through, one per line, beginning and ending with the barn at field 1. If more than one solution is possible, output any solution.
Sample Input
4 5
1 2
1 4
2 3
2 4
3 4
Sample Output
1
2
3
4
2
1
4
3
2
4
1
Hint
OUTPUT DETAILS:
Bessie starts at 1 (barn), goes to 2, then 3, etc…
Source
USACO 2005 January Silver
题意:
给出一个图,求每条边都经过正反恰好两次的点的轨迹,任意输出一组合法解即可。保证存在解。可以看作是有向图求欧拉回路。
题解:
普遍地,求欧拉回路有2种方法。
1.dfs
我曾经搞欧拉路径的时候居然搞过这道题…果然越活越失败吗。
一些结论:
1.对于一个欧拉回路,去掉一个点以及它所关联的边,剩下的部分还是一个欧拉回路。
2.对于一个欧拉回路,去掉其中的一小部分的欧拉回路,剩下的部分依旧是一个欧拉回路。
选择一个起点,压入栈中,如果它还有边之前没有走过,就把这条边删掉,然后把这条边连的另一个节点压入栈。递归处理。如果这个点没有未遍历过的边,则出栈。可以证明在一个欧拉图中,最后一个入栈的元素即起点。
无向图,倒叙的答案就是一条欧拉回路。有向图正序倒叙皆可。
【???
#include<cstdio>#include<cstring>#include<algorithm>using namespace std;const int N = 100010;int n,m;int num=0;int head[N];struct node{ int pre,v;}edge[N];void addedge(int from,int to){ num++; edge[num].pre=head[from]; edge[num].v=to; head[from]=num;}int vis[N];int ans=0;void euler(int u){ for(int i=head[u];i;i=edge[i].pre){ if(!vis[i]){ vis[i]=true; int v=edge[i].v; euler(v); } } printf("%d\n",u);}int main(){ scanf("%d%d",&n,&m); for(int i=1;i<=m;i++){ int u,v; scanf("%d%d",&u,&v); addedge(u,v);addedge(v,u); } euler(1); return 0;}
2.fleury
//存个板?
//再根据有向图无向图等等来对删边等的细节做一些处理。
#include<cstdio>#include<cstring>#include<algorithm>#include<iostream>#include<stack>using namespace std;const int N = 10000 + 10;const int M = 50000 + 10;inline int read(){ int x=0,f=1;char ch=getchar(); while(ch<'0'||ch>'9') {if(ch=='-') f=-1;ch=getchar();} while(ch>='0'&&ch<='9') {x=x*10+ch-'0';ch=getchar();} return x*f;}int n,m;struct node{ int pre,v;}e[M<<2];int num=1,head[N];void addedge(int from,int to){ e[++num].pre=head[from],e[num].v=to; head[from]=num;}stack<int> s;int in[N],delet[M<<2];void dfs(int u){ s.push(u); for(int i=head[u];i;i=e[i].pre){ if(delet[i]) continue; delet[i]=true; dfs(e[i].v); break; }}void fleury(int u){ s.push(u); while(!s.empty()){ int u=s.top();s.pop();bool flag=false; for(int i=head[u];i;i=e[i].pre){ if(delet[i]) continue; flag=true;break; } if(flag==true) dfs(u); else printf("%d\n",u); }}int main(){ n=read(),m=read(); for(int i=1;i<=m;++i){ int u=read(),v=read(); ++in[u],++in[v]; addedge(u,v);addedge(v,u); } /*int sum=0,pos=1; for(int i=1;i<=n;++i) if((in[i]&1)==0) ++sum,pos=i; if(sum==0||sum==2) fleury(pos); //else printf("No path!");*/ fleury(1); return 0;}
- POJ 2230 Watchcow 欧拉回路输出解
- POJ 2230 - Watchcow 输出欧拉回路点的路径...
- POJ 2230 Watchcow(欧拉回路:输出点轨迹)
- POJ 2230 Watchcow 【欧拉回路】
- poj 2230 Watchcow (DFS + 欧拉回路)
- poj 2230 Watchcow(欧拉回路)
- poj - 2230 - Watchcow(欧拉回路)
- POJ 2230 Watchcow 欧拉回路题解
- [欧拉回路] poj 2230 Watchcow
- POJ 2230 Watchcow(欧拉回路)
- POJ 2230 Watchcow (欧拉回路)
- poj 2230 Watchcow 欧拉回路
- POJ 2230 Watchcow (欧拉回路)
- POJ 2230 Watchcow( 欧拉回路 )
- POJ 2230 Watchcow (欧拉回路)
- Poj 2230 Watchcow【欧拉回路】
- poj 2230 Watchcow(欧拉回路)
- POJ 2230 Watchcow 欧拉回路
- Maven相关问题总结
- AES加密(AES/ECB/PKCS5Padding)key UTF8 取前十六个字节
- halcon数据类型分析
- 数据库三大范式
- Android 缓存浅谈(一) LruCache
- POJ 2230 Watchcow 欧拉回路输出解
- go语言接口
- post、get处理中文乱码问题
- Python 爬虫入门-爬取拉勾网实战
- JSP 基础之 JSTL <c:forEach>用法
- kotlin中的when:强大的switch
- select 1 from dual 中的1表示的含义
- 设计模式20-状态模式
- R语言大数据分析讲座邀请