您的位置:首页 > 其它

POJ 1041 欧拉回路

2016-07-12 22:02 411 查看
题意:

John要去访问他的朋友,在这个城市中有n条街道,每条街道与别的街道有两个交点,现在给出这些街道的信息(即输入x y z表示街道z的两个端点为x,y),问其是否可以每条街道只经过一次,如果存在,则输出字典序最小的路线,不存在则输出”Round trip does not exist.”

注意:

是边的字典序最小的路线。

思路:DFS+stack随便搞一搞就出来了

一开始脑残。。。用邻接表写。。。。。写了快半个小时 发现

woc?

然后怎么写。。。。。

就改成了邻接矩阵了。。

没想到和题解的思路竟然一样。

// by SiriusRen
#include <cstdio>
#include <cstring>
#include <algorithm>
#define N 55
using namespace std;
int xx,yy,zz,map[N*N][2],V
,ans,minn=50,tot,vis[N*N],top,s[N*N];
void dfs(int x){
for(int i=1;i<=tot;i++)
if(!vis[i]&&(map[i][0]==x||map[i][1]==x)){
vis[i]=1;
dfs(map[i][0]+map[i][1]-x);
s[++top]=i;
}
}
int main(){
while(scanf("%d%d",&xx,&yy)&&xx&&yy){
st: minn=min(minn,min(xx,yy));
scanf("%d",&zz);
tot++;
map[zz][0]=xx;map[zz][1]=yy;
V[xx]++;V[yy]++;
}
for(int i=1;i<=45;i++)if(V[i]&1)ans++;
if(!ans){
dfs(minn);
for(int i=top;i;i--)printf("%d ",s[i]);
puts("");
}
else puts("Round trip does not exist.");
scanf("%d%d",&xx,&yy);
top=tot=ans=0;minn=50;
memset(V,0,sizeof(V));
memset(vis,0,sizeof(vis));
if(xx)goto st;
else return 0;
}




Code length竟然进了前5

我只想问 第一名怎么23B写完的!!!
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  poj dfs