| Time Limit: 1000MS | Memory Limit: 32768KB | 64bit IO Format: %I64d & %I64u |
Description
Input
Output
Sample Input
6 8 5 3 5 2 6 4 5 6 0 0 8 1 7 3 6 2 8 9 7 5 7 4 7 8 7 6 0 0 3 8 6 8 6 4 5 3 5 6 5 2 0 0 -1 -1
Sample Output
Yes Yes No
简单的并查集,符合的无向图要求是连通的并且没有环。点的编号好像没有什么规律吧。
#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
using namespace std;
const int MAXN = 100000 + 100;
int parent[MAXN];
bool vis[MAXN];
int find_set(int t)
{
if (parent[t] == -1)
return t;
else
return parent[t] = find_set(parent[t]);
}
bool union_set(int a, int b)
{
int t1 = find_set(a);
int t2 = find_set(b);
if (t1 != t2)
{
parent[t2] = t1;
return true;
}
return false;
}
int main()
{
int a, b;
while (scanf("%d%d", &a, &b) != EOF && a != -1)
{
if (a == 0 && b == 0)
{
printf("Yes\n");
continue;
}
memset(vis, false, sizeof(vis));
memset(parent, -1, sizeof(parent));
union_set(a, b);
vis[a] = vis[b] = true;
bool ret = true;
bool ok = true;
while (scanf("%d%d", &a, &b) && a)
{
ret = union_set(a, b);
vis[a] = vis[b] = true;
if (!ret) ok = false;
}
int sum = 0;
for (int i = 0; i < MAXN; i++)
{
if (vis[i] && parent[i] == -1)
sum++;
}
if (sum != 1) ok = false;
printf("%s\n", ok ? "Yes" : "No");
}
}版权声明:本文为博主原创文章,未经博主允许不得转载。
原文地址:http://blog.csdn.net/qq_18738333/article/details/47977961