用并查集可以判断一组节点是否构成了树;
首先我们要清楚,纯空的节点也是树,并且树不能形成自环,也就是说,从一个点到达另一个点,有且只有一条路。
题目链接:1308 -- Is It A Tree? (poj.org)
Is It A Tree?
Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 53555 Accepted: 16823 Description
A tree is a well-known data structure that is either empty (null, void, nothing) or is a set of one or more nodes connected by directed edges between nodes satisfying the following properties.
There is exactly one node, called the root, to which no directed edges point.
Every node except the root has exactly one edge pointing to it.
There is a unique sequence of directed edges from the root to each node.
For example, consider the illustrations below, in which nodes are represented by circles and edges are represented by lines with arrowheads. The first two of these are trees, but the last is not.
In this problem you will be given several descriptions of collections of nodes connected by directed edges. For each of these you are to determine if the collection satisfies the definition of a tree or not.Input
The input will consist of a sequence of descriptions (test cases) followed by a pair of negative integers. Each test case will consist of a sequence of edge descriptions followed by a pair of zeroes Each edge description will consist of a pair of integers; the first integer identifies the node from which the edge begins, and the second integer identifies the node to which the edge is directed. Node numbers will always be greater than zero.
Output
For each test case display the line "Case k is a tree." or the line "Case k is not a tree.", where k corresponds to the test case number (they are sequentially numbered starting with 1).
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 -1Sample Output
Case 1 is a tree. Case 2 is a tree. Case 3 is not a tree.Source
North Central North America 1997
首先我们要明白,为什么并查集可以用来判断是否符合树。
我们通过并查集,将一组节点链接在一起,如果我们发现有一组节点的父节点相同,那么有两种情况:
1: 这组节点的两个数相同,也就是说,形成了一个自环。
2:这组节点的两个数不同,但是已经在之前形成了一次通路,也就是不止一条路到达某个节点,同样这种情况也不满足条件。
另外,树不一定是就是二叉树,所以一个一个节点可能连接多条路,但是必须要满足一个节点到达某个节点只有一条路。(如题目的第二幅图)。
题目思路:
利用并查集,连接各个节点,同时判断是否满足树的条件。
在全部输入完之后,要判断对于每个节点,判断是否为根,如果满足树的条件,根只能有一个。
下面代码会有详细注释
Code:
#include<stdio.h>
#include<string.h>
#include<cmath>
#include<iostream>
#include<algorithm>
#include<set>
#include<map>
#include<stack>
#include<vector>
#define ll long long
#define pll pair < ll , ll >
#define buff ios::sync_with_stdio(false)
using namespace std;
const int N = 1e5 + 10;
const int mod = 1e9 + 7;
ll n, m ,k;
ll xa[N];
bool kk = 0;
ll find(ll x)
{
if(x == xa[x])
{
return x;
}
else
{
return xa[x] = find(xa[x]);
}
}
int main()
{
// buff;
ll q = 0;
while(scanf("%lld %lld",&n,&m))
{
if(n == -1 && m == -1)
{
break;
}
q ++;
if(n == 0 && m == 0) // 如果是空的,那么仍满足是树的条件。
{
printf("Case %lld is a tree.\n",q);
continue;
}
for(int i = 0; i <= 100002; i ++)
{
xa[i] = i;
}
kk = 0;
if(find(m) == find(n)) // 对第一个进行判断,判断是否为自环。
{
kk = 1;
}
map < ll , ll > s; // 用map记录出现的节点,也可以用数组。
s[n] = 1;// 并查集的连接。
s[m] = 1;
xa[m] = n;
ll x,y;
while(scanf("%lld %lld",&x,&y))
{
if(x == 0 && y == 0) // 截至条件
{
break;
}
s[x] = 1;
s[y] = 1;
ll aa = find(x);
ll bb = find(y);
if(aa == bb || find(bb) != bb) // 不满足只有一条路或者自环的条件,不是树的结构。
{
kk = 1;
}
else
{
xa[bb] = aa;
}
}
ll sum = 0;
map< ll,ll >::iterator it = s.begin();// 对map进行遍历,用数组的化,需要先判断数组不为0。
/*
for(auto i : s)
{
if(xa[i.first] == i.first)
}
for(auto [x , y] : s)
{
}
这样写也可以,但是poj编译器太老了。
*/
while(it != s.end())
{
if(xa[it->first] == it->first) // 满足条件,为一个根。
{
sum ++;
}
it ++;
}
if(sum > 1)
{
kk = 1;
}
if(!kk) //判断输出;
{
printf("Case %lld is a tree.\n",q);
}
else
{
printf("Case %lld is not a tree.\n",q);
}
}
}
End :
总有人间一两风,
填我十万八千梦 。