1. 程式人生 > 實用技巧 >P2731 [USACO3.3]騎馬修柵欄 Riding the Fences

P2731 [USACO3.3]騎馬修柵欄 Riding the Fences

歐拉回路模板題

#include <bits/stdc++.h>
#define inf 2333333333333333
#define N 1000010
#define p(a) putchar(a)
#define For(i,a,b) for(int i=a;i<=b;++i)
//by war
//2020.8.6
using namespace std;
int n,m,x,y,s,cnt;
int d[N],ans[N];
map<int,map<int,int> >f;
void in(int &x){
    int y=1;char c=getchar();x=0
; while(c<'0'||c>'9'){if(c=='-')y=-1;c=getchar();} while(c<='9'&&c>='0'){ x=(x<<1)+(x<<3)+c-'0';c=getchar();} x*=y; } void o(int x){ if(x<0){p('-');x=-x;} if(x>9)o(x/10); p(x%10+'0'); } void dfs(int x){ For(i,1,n){ if(f[x][i]>=1){ f[x][i]
--; f[i][x]--; dfs(i); } } ans[++cnt]=x; } signed main(){ in(m); For(i,1,m){ in(x);in(y); f[x][y]++; f[y][x]++; n=max(n,max(x,y)); d[x]++;d[y]++; } s=1; For(i,1,n){ if(d[i]&1){ s
=i; break; } } dfs(s); for(int i=cnt;i;i--){ o(ans[i]);p('\n'); } return 0; }