并查集---0 0 空树也是树--poj1308:Is It A Tree?
1308:Is It A Tree?
总时间限制:
1000ms
内存限制:
65536kB
描述
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.
输入
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.
输出
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).
样例输入
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
样例输出
Case 1 is a tree. Case 2 is a tree. Case 3 is not a tree.
AC代码:
#include<cstdio>
#include<stdlib.h>
#include<iostream>
#include<algorithm>
#include<cmath>
#include<cstring>
#pragma warning(disable:4996)
using namespace std;
#include<string>
#include<vector>
#include<map>
#include<set>
#include<stack>
#include<queue>
#include<list>
#include<unordered_map>
#include<unordered_set>
const int maxn = 100 + 10;
const int INF = 0x7fffffff;
const double eps = 1e-6;
int father[maxn];
int findFather(int x) {//找爹+路径压缩
int a = x;
while (x != father[x])
x = father[x];
while (a != father[a]) {
int z = a;
a = father[a];
father[z] = x;
}
return x;
}
void Union(int a, int b) {//让a是b的父节点
int faA = findFather(a);
int faB = findFather(b);
if (faA != faB)
father[faB] = faA;
}
int main(){
//想是树,首先只能由一个根节点,其次不能有圈
int case_num = 0 , x, y, flag = 1;
int pre1 = 0, pre2 = 0;
int book[maxn] = { 0 };//一对结点里,第一个结点可能是根节点,第二个结点一定不是根节点。
while (scanf("%d%d", &x, &y)) {
if (pre1 == 0 && pre2 == 0 && x == 0 && y == 0) {
printf("Case %d is a tree.\n", case_num++);
continue;
}
pre1 = x;
pre2 = y;
if (x == -1 && y == -1)return 0;
else if (case_num == 0 || x == 0 && y == 0) {
if (case_num != 0) {//判断输出
int cnt_root = 0;//数下共有多少个结点的根节点是自己,想是树只能有一个
for (int i = 0; i < maxn; i++) {
if (book[i] == 2 && father[i] == i)
cnt_root++;
if (cnt_root > 1)break;
}
if (cnt_root != 1)flag = 0;
printf("Case %d %s.\n", case_num, flag == 1 ? "is a tree" : "is not a tree");
}
//下面是初始化
flag = 1;
case_num++;
memset(book, 0, sizeof(book));
for (int i = 0; i < maxn; i++)
father[i] = i;
if (case_num == 1) {//第一次输入
if (book[x] == 0)
book[x] = 2;//2表示是某个点的父结点
book[y] = -1;//-1表示是某个点的孩子节点
if (findFather(x) == findFather(y)) {//俩点的父节点要是相同说明有环,肯定不是树
flag = 0;
}
else {
Union(x, y);
}
}
}
else {
if (book[x] == 0)
book[x] = 2;//2表示是某个点的父结点
book[y] = -1;//-1表示是某个点的孩子节点
if (findFather(x) == findFather(y)) {//俩点的父节点要是相同说明有环,肯定不是树
flag = 0;
}
else {
Union(x, y);
}
}
}
return 0;
}
上一篇: 邻接矩阵的深度以及广度优先遍历
下一篇: 树及树的遍历