前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布

POJ1041

作者头像
triplebee
发布2018-01-12 15:25:57
5750
发布2018-01-12 15:25:57
举报
文章被收录于专栏:计算机视觉与深度学习基础

欧拉路问题

本次比赛的水题

对于这题的无向图,要每一个点的度数都为偶数,才存在欧拉回路。

代码语言:javascript
复制
#include<iostream>
#include<cstdio>
#include<cstring>
using namespace std;
int G[50][2000];  //G[点][边] = 点
bool vis[2000];
int degree[50];
int stack[2000], top;
int max(int a, int b)
{
    return a > b ? a : b;
}
void euler(int cur, int nRoads)
{
    for(int i = 1; i <= nRoads; i++)
    {
        if(!vis[i] && G[cur][i])
        {
            vis[i] = true;
            euler(G[cur][i], nRoads);
            stack[++top] = i;//找到路之后,再入栈
        }
    }
}
int main()
{
    int x, y, z, nRoads, nPoints, start;
    while(cin>>x>>y && x && y)
    {
        nRoads = nPoints = top = 0;
        memset(vis, false, sizeof(vis));
        memset(degree, 0, sizeof(degree));
        memset(G, 0, sizeof(G));
        start = x < y ? x : y;
        cin >> z;
        nRoads = max(nRoads, z);
        nPoints = max(nPoints, max(x, y));
        G[x][z] = y;
        G[y][z] = x;
        degree[x]++;
        degree[y]++;
        while(cin>>x>>y && x && y)
        {
            cin >> z;
            nRoads = max(nRoads, z);
            nPoints = max(nPoints, max(x, y));
            G[x][z] = y;
            G[y][z] = x;
            degree[x]++;
            degree[y]++;
        }
        bool flag = true;
        for(int i = 1; i <= nPoints; i++)//检查度数
            if(degree[i] %2)
                flag = false;
        if(!flag)
            cout << "Round trip does not exist." << endl;
        else
        {
            euler(start, nRoads);
            cout<<stack[top];
            for(int i = top - 1; i > 0; i--)
                cout<<" "<<stack[i];
            cout << endl;
        }
    }
    return 0;
}
本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2014-03-30 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档