POJ 1041 John's trip

来源:互联网 发布:淘宝网一摩托车头盔 编辑:程序博客网 时间:2024/06/06 17:50

欧拉回路,基础题,但我做得那叫一个乱。。

这道题只要把边和顶点的概念颠倒着想就可以了

#include <iostream>#include <cstdio>#include <cstring>#include <algorithm>#include <cmath>#include <vector>using namespace std;const int maxN=2000;int maX,degree[maxN],st[maxN],top,map[maxN][maxN];bool vis[maxN];void dfs(int x){    for(int z=1;z<=maX;z++){        if(map[x][z]&&!vis[z]){            vis[z]=true;            dfs(map[x][z]);            st[++top]=z;        }    }}int main(){#ifndef ONLINE_JUDGE          freopen("a.in","r",stdin);    freopen("a.out","w",stdout);#endif    int x,y;    while(cin>>x>>y,x||y){        memset(map,0,sizeof(map));        memset(degree,0,sizeof(degree));        memset(vis,false,sizeof(vis));        int z;        cin>>z;        map[x][z]=y;        map[y][z]=x;        degree[x]++;degree[y]++;        int start=min(x,y),maxV=max(x,y);        maX=z;        while(cin>>x>>y,x||y){            cin>>z;            map[x][z]=y;            map[y][z]=x;            degree[x]++;degree[y]++;            maX=max(maX,z);            maxV=max(x,max(maxV,y));            start=min(x,min(start,y));        }        bool solved=false;        for(int i=1;i<=maxV;i++)            if(degree[i]&1){                cout<<"Round trip does not exist."<<endl;                solved=true;                break;            }        if(solved) continue;        top=0;        dfs(start);        for(int i=top;i>0;i--) printf("%d%c",st[i],i==1?'\n':' ');    }return 0;}


原创粉丝点击