这道题目很意思,呵呵……
代码好下:
/*
ID: guo geer
PROG: fence
LANG: C++
*/
#include<iostream>
#include<cstring>
#include<cmath>
#include<fstream>
using namespace std;
int a[510][510],b[510],path[1029],stack[1029];
int getMin(int s[])
{
for(int i=1; i<=500; i++)
if(s[i] > 0) return i;
return -1;
}
int main()
{
ifstream fin("fence.in");
ofstream fout("fence.out");
int F;
while(fin>>F)
{
memset(a, 0, sizeof(a));
memset(b, 0, sizeof(b));
int x,y,st=510;
for(int i=0; i<F; i++)
{
fin>>x>>y;
st=min(st,min(x,y));
a[x][y] ++;
a[y][x] ++;
b[x] ++;
b[y] ++;
}
for(int i=1; i<=500; i++) if(b[i]%2 == 1){st = i; break;}
int p = 0, top=-1;
stack[++ top] = st;
while(top != -1)
{
st = stack[top];
if(b[st] == 0) path[p ++] = st, top --;
while(b[st] > 0)
{
int tt = getMin(a[st]);
b[st] --;
b[tt] --;
a[st][tt] --;
a[tt][st] --;
st = tt;
stack[++top] = st;
}
}
for(int i=F; i>=0; i--) fout<<path[i]<<endl;
}
return 0;
}