luogu P2731 骑马修栅栏 Riding the Fences

时间:2023-03-09 04:14:01
luogu P2731 骑马修栅栏 Riding the Fences

欧拉回路。

入度为奇数的点,搜他。

最好邻接矩阵。。。

#include<cstdio>
#include<iostream>
#define R register int
using namespace std;
inline int g() {
R ret=,fix=; register char ch; while(!isdigit(ch=getchar())) fix=ch=='-'?-:fix;
do ret=ret*+(ch^); while(isdigit(ch=getchar())); return ret*fix;
}
int m,top,mn,mx,st=;
int e[][],stk[],r[];
void dfs(int u) {
for(R v=mn;v<=mx;++v) if(e[u][v])
--e[u][v],--e[v][u],dfs(v);
stk[++top]=u;
}
signed main() {
m=g();
for(R i=,u,v;i<=m;++i) u=g(),v=g(),
++e[u][v],++e[v][u],++r[u],++r[v],
mn=min(min(u,v),mn),mx=max(max(u,v),mx);
for(R i=mn;i<=mx;++i) if(r[i]&) {st=i; break;}
dfs(st); for(;top>;--top) printf("%d\n",stk[top]);
}

2019.04.11