NYOJ129 决策树 【并检查集合】

NYOJ129 决策树 【并检查集合】

大家好,又见面了,我是全栈君,今天给大家准备了Idea注册码。

树的判定

时间限制:
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. 

NYOJ129 决策树 【并检查集合】

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 08 1  7 3  6 2  8 9  7 5 7 4  7 8  7 6  0 03 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、节点入度数不能大于1;

3、不能成环,比方一棵树的叶子节点指向根节点就是非法的;

4、自环是非法的。

#include <stdio.h>
#include <string.h>

#define maxn 10010

int pre[maxn];
bool vis[maxn];

int unionFind(int k){
	int a = k, b;
	while(pre[k] != -1) k = pre[k];
	while(a != k){
		b = pre[a];
		pre[a] = k;
		a = b;
	}
	return k;
}

int main() {
	// freopen("stdin.txt", "r", stdin);
	memset(pre, -1, sizeof(pre));
	int u, v, cas = 1, ok = 1, count = 0;
	while(scanf("%d%d", &u, &v) != EOF) {
		if(u < 0) break;
		if(!(u | v)) {
			printf("Case %d ", cas++);
			if(count > 1) ok = 0;
			if(ok) printf("is a tree.\n");
			else printf("is not a tree.\n");
			memset(pre, -1, sizeof(pre));
			memset(vis, 0, sizeof(vis));
			count = 0; ok = 1; continue;
		}
		if(!ok) continue;

		if(!vis[u]) {
			vis[u] = 1; ++count;
		}
		if(!vis[v]) {
			vis[v] = 1; ++count;
		}
		if(pre[v] != -1 || u == v) {
			ok = 0; continue;
		}
		u = unionFind(u);
		if(u == v) {
			ok = 0; continue;
		}
		pre[v] = u; --count;
	}
	return 0;
}

版权声明:本文博客原创文章,博客,未经同意,不得转载。

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请联系我们举报,一经查实,本站将立刻删除。

发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/117745.html原文链接:https://javaforall.net

(0)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

  • python dtype o_python – 什么是dtype(’O’)? – 堆栈内存溢出「建议收藏」

    python dtype o_python – 什么是dtype(’O’)? – 堆栈内存溢出「建议收藏」当你在数据帧中看到dtype(‘O’),这意味着Pandas字符串。什么是dtype?什么属于pandas或numpy,或两者,或其他什么?如果我们检查一下pandas代码:df=pd.DataFrame({‘float’:[1.0],’int’:[1],’datetime’:[pd.Timestamp(‘20180310′)],’string’:[‘foo’]})print…

    2022年5月17日
    103
  • 反射getmethod参数_java通过反射获取属性值

    反射getmethod参数_java通过反射获取属性值1、forName方法forName是一个静态方法,其作用:通过调用来获取类名对应的Class对象,同时将Class对象加载进来。如果将类名保存在字符串(如xml)中,就可以在程序运行时,动态调用加载。注意:只有调用的参数是类名或者方法时,才可用。2、newInstance()方法作用:将对象实例化。返回类型为Object。与new的区别在于,new可以带参,而newInstance()不可以,…

    2025年12月2日
    7
  • linux系添加路由,Linux添加路由的两种方法「建议收藏」

    linux系添加路由,Linux添加路由的两种方法「建议收藏」Linux中增加软路由的两种方法第一种:routeadd-net172.16.6.0netmask255.255.255.0gw172.16.2.254deveth0/*增加一条网络172.16.6.0/24经过172.16.2.254eth0*//*-net增加网络-host增加主机netmask子网掩码gw网关dev装置,设备,这里是你的网卡名*/ro…

    2022年10月5日
    4
  • 微型计算机原理与接口技术——8086指令系统之移位指令

    微型计算机原理与接口技术——8086指令系统之移位指令移位指令移动一位时由指令直接给出;移动两位及以上,则移位次数由CL指定。要求操作数不能是立即数;这类指令的执行大多会影响6个状态标志位。非循环移位指令逻辑左移SHL(ShiftLogicLeft)算术左移SAL(ShiftArithmeticLeft)逻辑右移SHR(ShiftLogicRight)算术右移SAR(ShiftArithmeticRight)4条指令的格式完全相同,可实现对8位或16位寄存器操作数或内存操作数进行指定次数的移位。逻辑移位指令针对的

    2022年5月11日
    58
  • pycharm怎么缩小代码_pycharm快速缩进

    pycharm怎么缩小代码_pycharm快速缩进Pycharm编写代码的小技巧1、代码块缩进选中要缩进的代码块,按tab键,整个代码块缩进2、取消代码块的缩进选中要取消缩进的代码块,按shift+tab键,整个代码块取消缩进3、编写测试代码语句ifname==‘main’:输入main,然后按下Enter键4、在Pycharm中整块的代码进行注释选中要注释的代码,按下Ctrl+/5、取消整块代码的注释选中要取消注释的代…

    2022年8月27日
    4
  • hive是一个数据仓库基础架构_数据仓库ods层和dw层的区别

    hive是一个数据仓库基础架构_数据仓库ods层和dw层的区别软件环境Hadoop2.6.0-cdh5.9.0Hive1.1.0-cdh5.9.0Zookeeper3.4.5-cdh5.9.0需求背景数据来源是将8台服务器日志各自压缩成*.gz(8个gz文件)后,按天和小时分区传入到HDFS上,然后通过创建HiveODS外部表加载到表对应分区,这样一天下来会生产192个gz文件,gz文件是不能进行切分所以查询一天则会产生192

    2022年9月27日
    5

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

关注全栈程序员社区公众号