HDU 1272 Little hope of solving the maze report
Problem-solving ideas: This title is not difficult, but there are local pit. Literal understanding the problem, only ancestors solving comparing each input two numbers, if not equal, to connect it, if they are equal, then there must be a ring, i.e., output NO. However, also more than that, since the subject further requires that each node can connect the two, it is not FIG forests, i.e., can not have two root nodes. Think of this far on it.
#include <stdio.h>
#include<iostream>
#include <algorithm>
#include<string.h>
#pragma warning(disable:4996)
#define mod 1000000007
#define ll unsigned long long
const int N = 100005;
using namespace std;
int father[N], visit[N];
int mx;
int mi;
int flag;
int find(int x)
{
if (father[x] == x)
return x;
return father[x] = find(father[x]);
}
void judge()
{
int cnt = 0;
for (int i = mi; i <= mx; i++)
{
if (visit[i] && i == find(i))//不能有两个根节点
{
cnt++;
if (cnt > 1)
{
flag = 1;
return;
}
}
}
}
int main()
{
int a, b;
for (int i = 1; i < N; i++)
{
father[i] = i;
}
mi = 1000010;
while (scanf("%d%d", &a, &b))
{
if (a == -1 && b == -1)
{
return 0;
}
if (a == 0 && b == 0)
{
judge();
if (flag)
{
printf("No\n");
}
else
{
printf("Yes\n");
}
for (int i = 1; i < N; i++)
{
father[i] = i;
}
memset(visit, 0, sizeof(visit));
flag = 0;
mx = 0;
mi = 1000010;
continue;
}
mx = max(mx, max(a, b));
mi = min(mi, min(a, b));
visit[a] = 1;
visit[b] = 1;
if (flag)
{
continue;
}
int fa = find(a);
int fb = find(b);
if (fa == fb)
{
flag = true;
continue;
}
else
{
father[fa] = fb;
}
}
}