您的位置:首页 > Web前端

USACO Section 3.3 Riding The Fences (fence)

2013-02-13 20:12 543 查看
RidingtheFences

FarmerJohnownsalargenumberoffencesthatmustberepaired
annually.Hetraversesthefencesbyridingahorsealongeachand
everyoneofthem(andnowhereelse)andfixingthebrokenparts.

FarmerJohnisaslazyasthenextfarmerandhatestoridethesame
fencetwice.Yourprogrammustreadinadescriptionofanetworkof
fencesandtellFarmerJohnapathtotraverseeachfencelengthexactly
once,ifpossible.FarmerJcan,ifhewishes,startandfinishatany
fenceintersection.

Everyfenceconnectstwofenceintersections,whicharenumbered
inclusivelyfrom1through500(thoughsomefarmshavefarfewerthan
500intersections).Anynumberoffences(>=1)canmeetatafence
intersection.Itisalwayspossibletoridefromanyfencetoanyother
fence(i.e.,allfencesare"connected").

Yourprogrammustoutputthepathofintersectionsthat,ifinterpreted
asabase500number,wouldhavethesmallestmagnitude.

Therewillalwaysbeatleastonesolutionforeachsetofinput
datasuppliedtoyourprogramfortesting.

PROGRAMNAME:fence

INPUTFORMAT

Line1:Thenumberoffences,F(1<=F<=1024)
Line2..F+1:Apairofintegers(1<=i,j<=
500)thattellwhichpairofintersectionsthisfenceconnects.

SAMPLEINPUT(filefence.in)

9
12
23
34
42
45
25
56
57
46

OUTPUTFORMAT

TheoutputconsistsofF+1lines,eachcontainingasingleinteger.Printthenumberofthestartingintersectiononthefirstline,thenextintersection'snumberonthenextline,andsoon,untilthefinalintersectiononthelastline.Theremightbemanypossibleanswerstoanygiveninputset,butonlyoneisorderedcorrectly.

SAMPLEOUTPUT(filefence.out)

1
2
3
4
2
5
4
6
5
7

思路:dfs+欧拉路(欧拉回路,欧拉通路)的基本定理,不得不说,这题实在是做不下去了,看了别人的代码才过

Executing...
Test1:TESTOK[0.000secs,6460KB]
Test2:TESTOK[0.000secs,6460KB]
Test3:TESTOK[0.000secs,6460KB]
Test4:TESTOK[0.000secs,6460KB]
Test5:TESTOK[0.000secs,6460KB]
Test6:TESTOK[0.000secs,6460KB]
Test7:TESTOK[0.011secs,6460KB]
Test8:TESTOK[0.022secs,6460KB]

AlltestsOK.


/*
ID:wuhuaju2
PROG:fence
LANG:C++
*/
#include<cstdio>
#include<iostream>
#include<cstdlib>
#include<algorithm>
#include<cstring>
#include<string>
usingnamespacestd;

constintmaxn=510,maxm=1110;
intsum[10],tot[maxn];
intf[maxn][maxn];
inta[maxn][maxm],b[10],ans[maxm];
intcnt,n,x,y,maxx;
boolvis[maxn];

voidclose()
{
fclose(stdin);
fclose(stdout);
exit(0);
}

voiddfs(intk)
{

for(inti=1;i<=maxn;i++)
{
if(f[k][i]>0)
{
f[k][i]--;
f[i][k]--;
dfs(i);
}
}
cnt++;
ans[cnt]=k;
}

voidwork()
{
x=-100;
for(inti=1;i<=maxx;i++)
if(tot[i]%2==1)
{
x=i;
break;
}
if(x==-100)
{
for(inti=1;i<=maxx;i++)
{
if(tot[i]>0)
{
x=i;
break;
}
}
}
dfs(x);
for(inti=1;i<=cnt;i++)
cout<<ans[cnt-i+1]<<endl;
}

voidinit()
{
freopen("fence.in","r",stdin);
freopen("fence.out","w",stdout);
scanf("%d",&n);
memset(tot,0,sizeof(tot));
memset(f,0,sizeof(f));
maxx=0;
for(inti=1;i<=n;i++)
{
scanf("%d%d",&x,&y);
maxx=max(max(maxx,x),y);
f[x][y]++;
f[y][x]++;
tot[x]++;
tot[y]++;
//vis[x]=true;
//vis[y]=true;
}

}

intmain()
{
init();
work();
close();
return0;
}





                                            
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: