POJ 1041 欧拉回路

题意:
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[N],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写完的!!!

原文地址:https://www.cnblogs.com/SiriusRen/p/6532411.html