无向图的欧拉路及欧拉回路1.1

#include <bits/stdc++.h>
using namespace std;
int n,m,pic[1100][1100],d[1100],road[1500],now;
void dfs(int x)
{
    for(int i=1; i<=500; i++)
    {
        if(pic[x][i])
        {
            pic[x][i]--;
            pic[i][x]--;
            dfs(i);
        }
    }
    road[++now]=x;
}
int main()
{
    cin>>m;
    for(int i=1; i<=m; i++)
    {
        int x,y;
        cin>>x>>y;
        pic[x][y]++;
        pic[y][x]++;
        d[x]++;
        d[y]++;
    }
    int st=1;
    for(int i=1; i<=500; i++)
    {
        if(d[i]%2)
        {
            st=i;
            break;
        }
    }
    dfs(st);
    for(int i=now; i>0; i--)
    {
        cout<<road[i]<<" ";
    }
}

猜你喜欢

转载自blog.csdn.net/lanshan1111/article/details/83545504