NYOJ-129 树的判定(并查集)

树的判定

时间限制: 1000 ms  |  内存限制: 65535 KB
难度: 4
描述

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.

The number of test cases will not more than 20,and the number of the node will not exceed 10000.
The inputs will be ended by a pair of -1.
输出
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.
来源
POJ
上传者
张云聪

思路:
        首先定义一个结构体表示树的节点,内容包括父亲节点和孩子节点的值,用两个可变长数组实现(因为父亲孩子的个数位置)
        然后分两步判断:
               第一步依据树的性质,包括 
                       1.一个节点最多只能有1个父亲节点;  
                       2.一棵树最多只能有一个节点没有父亲;
                       3.一个几点不能的父亲和孩子不能包含同一个节点;
                       如果不满足这三点,直接报错即可;
               第二步利用并查集判断集合个数, 因为第一步无法判断出带环的情况(例如: 1 2  1 3  7 8  8 9  9 7),所以如果最后判断出集合个数>1, 就肯定不是树了;

       另:此题也可以用深搜实现

代码:
#include <stdio.h>
#include <stdlib.h>
#include <vector>

#define N 10010

using namespace std;

struct Node{
	vector<int>father;
	vector<int>son;
};

int fa[N];
int ct;

int getfather(int i)
{
	if(fa[i] == i) return i;
	else return  fa[i] = getfather(fa[i]);
}

void merge(int a, int b)
{
	a = getfather(a);
	b = getfather(b);
	if(a == b) return;
	else{
		fa[b] = a;
		ct --;
	}
}


int main()
{
	int z = 0;
	while(1){
		ct = 0;
		z ++;
		Node *Nlist[N] = {};
		int m, n;
		scanf("%d%d", &m, &n);
		if(m == -1 && n == -1) break;
		int list[N];
		int li = 0;
		int ok;
		for(int i = 0; i < N; i ++) fa[i] = i;
		if(!(m == 0 && n == 0)){
			while(1){
				ct ++;
				ok = 0;
				merge(m, n);
				for(int i = 0; i < li; i ++){
					if(list[i] == m){
						ok = 1;
						break;
					}
				}
				if(!ok) list[li++] = m;
				ok = 0;
				for(int i = 0; i < li; i ++){
					if(list[i] == n){
						ok = 1;
						break;
					}
				}

				if(!ok) list[li++] = n;
				if(Nlist[m] == NULL) Nlist[m] = (Node *)calloc(1, sizeof(Node));
				Nlist[m]->son.push_back(n);
				if(Nlist[n] == NULL) Nlist[n] = (Node *)calloc(1, sizeof(Node));
				Nlist[n]->father.push_back(m);		
			
				scanf("%d%d", &m, &n);
				if(m == 0 && n == 0) break;
			}
			ok = 0;
			int head = 0;
			if(ct != 0) ok = 1;
			if(!ok) for(int i = 0; i < li; i ++){
				Node * nw = Nlist[list[i]];
				if(nw ->father.size() == 0){
					head ++;
					if(head > 1){
						ok = 1;
						break;
					}
					continue;
				}
				if(nw ->father.size() > 1){
					ok = 1;
					break;
				}
				int father = nw ->father.at(0);
				for(int t = 0; t < (int)nw ->son.size(); t++){
					if(father == nw ->son.at(t)){
						ok = 1;
						break;
					}
				}
				if(ok) break;
			}
			if(head == 0) ok = 1;
		}
		else ok = 0;
		if(!ok)	printf("Case %d is a tree.\n", z);
		else printf("Case %d is not a tree.\n", z);

		for(int i = 0; i < li; i ++){
			free(Nlist[i]);
		}
	}

	return 0;
}                


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
孪生素数是指两个素数之间的差值为2的素数对。通过筛选法可以找出给定素数范围内的所有孪生素数的组数。 在引用的代码中,使用了递归筛选法来解决孪生素数问题。该程序首先使用循环将素数的倍数标记为非素数,然后再遍历素数数组,找出相邻素数之间差值为2的素数对,并统计总数。 具体实现过程如下: 1. 定义一个数组a[N,用来标记数字是否为素数,其中N为素数范围的上限。 2. 初始化数组a,将0和1标记为非素数。 3. 输入要查询的孪生素数的个数n。 4. 循环n次,每次读入一个要查询的素数范围num。 5. 使用两层循环,外层循环从2遍历到num/2,内层循环从i的平方开始,将素数的倍数标记为非素数。 6. 再次循环遍历素数数组,找出相邻素数之间差值为2的素数对,并统计总数。 7. 输出总数。 至此,我们可以使用这个筛选法的程序来解决孪生素数问题。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* [python用递归筛选法求N以内的孪生质数(孪生素数)](https://blog.csdn.net/weixin_39734646/article/details/110990629)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v92^chatsearchT0_1"}}] [.reference_item style="max-width: 50%"] - *2* *3* [NYOJ-26 孪生素数问题](https://blog.csdn.net/memoryofyck/article/details/52059096)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v92^chatsearchT0_1"}}] [.reference_item style="max-width: 50%"] [ .reference_list ]

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值