3649: 欧拉回路时间限制(普通/Java):1000MS/3000MS 内存限制:65536KByte总提交: 1064 测试通过:413 描述
|
思路:如果对应一进一出并且根节点相同则输出1,反之则0
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N = 1e6 + 10;
int a[10005], b[10005], tong[10005],n,m;
void chushi()
{
memset(tong, 0, sizeof tong);
memset(a, 0, sizeof a);
for (int i = 1; i <= n; i++)
b[i] = i;
return;
}
int findroot(int x)
{
if (x != b[x])
return findroot(b[x]);
return b[x];
}
int main()
{
ios::sync_with_stdio(false);
cin.tie(0); cout.tie(0);
while (cin >> n, n != 0)
{
chushi();
cin >> m;
for (int i = 0; i < m; i++)
{
int x, y;
cin >> x >> y;
int c = findroot(x);
int d = findroot(y);
if (c != d) //转化为同一根节点,老大找老大
b[x] = y;
tong[x]++, tong[y]++;
}
int f = 0;
for (int i = 1; i <= n; i++)
{
if (tong[i] % 2 == 1) //并非是一进一出对应
{
f = 1;
break;
}
}
if (f)
{
cout << 0 << endl;
continue;
}
int t = 1;
int ff = findroot(t);
int flag = 0;
for (int i = 2; i <= n; i++)
{
int xx = findroot(i);
if (xx != ff) //有一个节点的根节点与其他不同
{
flag = 1;
break;
}
}
if (flag)cout << 0 << endl;
else cout << 1 << endl;
}
return 0;
}