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.
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; }
相关文章推荐
- USACO 3.3 Riding The Fences (fence)
- USACO section 3.3 Riding the Fences(欧拉通路的遍历,dfs)
- USACO-Section3.3 Riding the Fences【欧拉路】
- USACO section 3.3 Riding the Fences(欧拉通路的遍历,dfs)
- USACO-Section 3.3 Riding the Fences(欧拉回路)
- 【USACO 3.3】Riding The Fences(欧拉路径)
- USACO3.3.1 Riding the Fences(fence)
- Riding the Fences USACO 3.3 欧拉路径(dfs+floodfill)
- USACO-Section 3.3-PROB Riding The Fences
- USACO3.3.1 Riding The Fences (fence)
- usaco Chapter 3 section 3.3 Riding the Fences
- usaco 3.3 Riding the Fences
- Riding The Fences_usaco3.3_dfs
- USACO 3.3 Riding the Fences(欧拉路输出路径)
- USACO 3.3 Riding the Fences
- Riding the Fences (USACO 3.3) 无向图欧拉通路/回路
- usaco Chapter 3 section 3.3 A game
- [USACO 3.3.1 Riding The Fences]
- USACO Section 3.3 Shopping Offers - 多重背包
- USACO Section 4.1 Fence Loops - 简单深搜~