1878欧拉回路

趁热打铁,再做了道欧拉回路,

形成欧拉回路的条件

1、所有节点的度都是偶数

2、所有节点连通

#include <iostream>
#include <cstring>
using namespace std;

#ifndef ONLINE_JUDGE
#include <fstream>
ifstream fin("test.txt");
#define cin fin
#endif
int degree[1010],p[1010];
int find(int x)
{
    return x == p[x] ? x : p[x] = find(p[x]);
}
int main()
{
    ios::sync_with_stdio(false);
    int n,m,i,a,b;
    while(cin >> n >> m && n)
    {
        memset(degree,0,sizeof(degree));
        for(i = 1; i <= n; ++i)
        p[i] = i;
        for(i = 0; i < m; ++i)
        {
            cin >> a >> b;
            degree[a]++;
            degree[b]++;
            a = find(a);
            b = find(b);
            if(a != b)
            p[b] = a;
        }
        int cnt = 0;
        for(i = 1; i <= n; ++i)
        if(p[i] == i)
        cnt++;
        if(cnt!=1)
        {
            cout << 0 << endl;
            continue;
        }
        for(i = 1; i <= n; ++i)
        if(degree[i]%2==1)
        break;
        if(i == n+1)
        cout << 1 << endl;
        else
        cout << 0 << endl;
    }
    return 0;
}

原文地址:https://www.cnblogs.com/fchx/p/3097582.html