题目传送门:http://www.nocow.cn/index.php/Translate:USACO/fence
这道题直接模拟就好了
/* ID:abc31261 LANG:C++ TASK:fence */ #include<cstdio> #include<cstring> #include<vector> #include<iostream> using namespace std; const int maxn=600,n=500; vector<int> g[maxn]; int r[maxn],road[maxn],num=0,f[maxn][maxn]; void find(int x) { int i,j; if (r[x]==0) { road[++num]=x; return; } for (i=1;i<=500;i++) if (f[x][i]!=0) { f[x][i]--; f[i][x]--; r[x]--; r[i]--; find(i); } road[++num]=x; } int main() { int m,i,j,l; freopen("fence.in","r",stdin); freopen("fence.out","w",stdout); scanf("%d",&m); memset(r,0,sizeof(r)); for (i=1;i<=m;i++) { scanf("%d%d",&j,&l); f[j][l]++; f[l][j]++; r[l]++; r[j]++; } for (i=1;i<=n;i++) if (r[i]%2==1) { find(i); break; } if (num==0)find(1); for (i=num;i>=1;i--)cout<<road[i]<<endl; return 0; }