首页 > 代码库 > uva-10596-欧拉回路

uva-10596-欧拉回路

并不要求所有点都联通,只要出现的所有边能形成欧拉回路就行了

做成有向图的欧拉回路wa成了狗

#include <iostream>#include<memory.h>#include<stdio.h>using namespace std;const int N = 205;void initSet(int a[N]){    for (int i = 0; i < N; i++)        a[i] = i;}int find(int key, int a[N]){    return key == a[key] ? key : a[key] = find(a[key], a);}void join(int k1, int k2, int a[N]){    int p1 = find(k1, a);    int p2 = find(k2, a);    if (p1 != p2)    {        if (p1 < p2)            a[p2] = p1;        else            a[p1] = a[p2];    }}int main(){    freopen("d:\\1.txt", "r", stdin);    int n, m;    while (cin >> n >> m)    {        if(m==0)        {            cout<<"Not Possible"<<endl;            continue;        }        int a[N];        int du[N];        initSet(a);        memset(du, 0, sizeof(du));        int s, e;        for (int i = 0; i < m; i++)        {            cin >> s >> e;            du[e]++;            du[s]++;            join(s,e,a);        }        int ok = 1;        for (int i = 0; i < n; i++)            if (du[i] %2 !=0)            {                ok = 0;                break;            }        int  p = find(s,a);        //联通        for (int i = 0; i < n; i++)        {            if (find(i, a) != p && du[i]!=0)            {                ok = 0;                break;            }        }        if (ok)            cout << "Possible" << endl;        else            cout << "Not Possible" << endl;    }    return 0;}

 

uva-10596-欧拉回路