Is It A Tree?(HDU 1325)树型并查集

HDU1325 Is It A Tree?

题目链接

Problem 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.

1325
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 -1

Sample Output:

Case 1 is a tree.
Case 2 is a tree.
Case 3 is not a tree.

题目大意

以"0 0"结束一组样例输入,以"-1 -1"结束所有样例输入。每组样例由若干个"x y"组合组成,代表x和y之间有连接。将每组样例所有点连接起来,如果这些节点形成的树满足 1、只有一个根节点。2、每个叶子节点只有一条边(一个父节点)。那么输出"Case n is a tree.",否则输出"Case n is not a tree."

解题思路

经典并查集里的数组table[N]里有多少个点值为-1意味着有多少个根节点,在加入新的边过程中,这条边由节点x指向节点y,若加入之前,table[y]的值不为-1,说明y节点已经有了一个父节点,这个时候不符合题意,将flag置为假做标记。如果输入完毕所有节点只有一个点的table值是-1且flag为真,则是一棵树。

AC代码

#include <bits/stdc++.h>
using namespace std;

const int N = 1005;

int table[N];

int findt(int x){
	if(table[x]==-1){
		return x;
	}
	table[x]=findt(table[x]);
	return table[x];
}

void merge(int a, int b){
	int ta=findt(a);
	int tb=findt(b);
	if(ta!=tb){
		table[tb]=ta;
	}
}

int main(int argc, char **argv)
{
	int a,b;
	int Case=1;
	set<int> node;
	bool flag=true;
	memset(table, -1, sizeof(table));
	while(~scanf("%d%d",&a,&b)){
		if(a==-1 && b==-1){
			break;
		}
		if(a==0 && b==0){
			set<int>::iterator it;
			int ans=0;
			for(it=node.begin(); it!=node.end(); it++){
				if(table[*it]==-1){
					ans++;
				}
			}
			if(ans==1 && flag){
				printf("Case %d is a tree.\n", Case);
			}else{
				printf("Case %d is not a tree.\n", Case);
			}
			memset(table, -1, sizeof(table));
			node.clear();
			Case++;
			flag=true;
			continue;
		}
		if(table[b]!=-1){
			flag=false;
		}
		merge(a,b);
		node.insert(a);
		node.insert(b);
	}
	return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

征服所有不服

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值