Watchcow
Time Limit: 3000MS | Memory Limit: 65536K | |||
Total Submissions: 8724 | Accepted: 3799 | 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.
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.
* 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...
Bessie starts at 1 (barn), goes to 2, then 3, etc...
Source
题目大意:
给定N个点M条边的无向图(1<=N<=10000,1<=M<=50000),求一条路径,从节点1出发,最后回到节点1,并且满足每条边恰好被沿正反方向分别经过一次,若有多种方案,输出任意一种即可。
解析:
欧拉回路。
几乎是模板,鉴于每条边需要正反经过一次,所以每条边的vis要分正反两条有向边进行标记。
代码:
#include <iostream>
#include <algorithm>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <string>
#include <cmath>
#include <cctype>
#include <queue>
using namespace std;
const int Max=100101;
int n,m,size=1,tot,top;
int first[Max],ans[Max],p[Max],vis[Max];
struct shu{int to,next;};
shu bian[Max];
inline int get_int()
{
int x=0,f=1;
char c;
for(c=getchar();(!isdigit(c))&&(c!='-');c=getchar());
if(c=='-') {f=-1;c=getchar();}
for(;isdigit(c);c=getchar()) x=(x<<3)+(x<<1)+c-'0';
return x*f;
}
inline void build(int x,int y)
{
size++;
bian[size].next=first[x];
first[x]=size;
bian[size].to=y;
}
inline void pre()
{
p[++top]=1;
while(top>0)
{
int point=p[top],u=first[point];
while(u&&vis[u]) u=bian[u].next;
if(u)
{
p[++top]=bian[u].to;
vis[u]=1;
first[point]=bian[u].next;
}
else
{
top--;
ans[++tot]=point;
}
}
}
int main()
{
//freopen("lx.in","r",stdin);
//freopen("lx.out","w",stdout);
n=get_int();
m=get_int();
for(int i=1;i<=m;i++)
{
int x=get_int(),y=get_int();
build(x,y);
build(y,x);
}
pre();
for(int i=1;i<=tot;i++) cout<<ans[i]<<"\n";
return 0;
}