NYOJ129 决策树 【并检查集合】
树的判定
- 描写叙述
- 
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、节点入度数不能大于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;
}
版权声明:本文博客原创文章,博客,未经同意,不得转载。
NYOJ129 决策树 【并检查集合】的更多相关文章
- HDU 1272 小希迷宫(并检查集合)
		意甲冠军:被判处无向图无环和连接无处不在 思考:并检查集合,trap 您可能有一个直接输入0 0 并且....合并的时候按某一个方向会爆栈,爆了好几次...下次考虑一下直接递归找祖先吧 #includ ... 
- hdu1325 Is It A Tree?并检查集合
		pid=1325">职务地址 试想一下,在词和话题hdu1272是一样的. 可是hdu1272的博文中我也说了.数据比較水,所以我用非并查集的方法就AC了. 可是这题的数据没那么水,要 ... 
- URAL - 1966 - Cycling Roads(并检查集合 + 判刑线相交)
		意甲冠军:n 积分,m 边缘(1 ≤ m < n ≤ 200),问:是否所有的点连接(两个边相交.该 4 点连接). 主题链接:http://acm.timus.ru/problem.aspx? ... 
- CodeForces 277A Learning Languages (并检查集合)
		A. Learning Languages time limit per test:2 seconds memory limit per test:256 megabytes The "Be ... 
- HDU 1198 Farm Irrigation (并检查集合 和 dfs两种实现)
		Farm Irrigation Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) ... 
- zoj 3659 并检查集合
		http://acm.zju.edu.cn/onlinejudge/showProblem.do? problemId=4882 现在在牡丹江,明天regional直播比赛,我会在一个月内退休.求祝福 ... 
- uva  11987  Almost Union-Find (并检查集合)
		标题效果: 三操作. 1. 合并两个集合 2.代替所述第二组的第一个元素 3.输出设置数量,并.. IDEAS: 使用p该元素的记录数,其中集合,建立并查集. #include <cstdio& ... 
- 《算法导论》2.3-7 检查集合中是否存在两数字和为指定的X--算法和证明
		习题2.3-7:设计一个算法,对于一个给定的包含n个整数的集合S和另一个给定的整数X,该算法可以在时间内确定S中是否存在两个元素,使得它们的和恰为X. 解题思路:首先应该想到的是先用一个的排序算法对S ... 
- HDU 3081Marriage Match II(二分法+并检查集合+网络流量的最大流量)
		职务地址:http://acm.hdu.edu.cn/showproblem.php? pid=3081 有一段时间没写最大流的题了,这题建图竟然想了好长时间... 刚開始是按着终于的最大流即是做多轮 ... 
随机推荐
- 在bmp上添加字符
			//打开位图文件,得到位图句柄 HBITMAP OpenBmpFile(HDC hDC, LPSTR lpszFileName) { HBITMAP hBmp = NULL; ... 
- 模仿《百度音乐HD》添加到下载框动画
			上次听有人说喜欢<百度音乐HD>添加到下载动画 ,我就尝试模仿了下,没想到,今天code4app(地址)也有了这个,但是 这个动画基本相同,我们的思路还是部一样的. 都可以参考 .主要关键 ... 
- 深度学习系列之CNN核心内容
			导读 怎么样来理解近期异常火热的深度学习网络?深度学习有什么亮点呢?答案事实上非常简答.今年十月份有幸參加了深圳高交会的中科院院士论坛.IEEE fellow汤晓欧做了一场精彩的报告,这个问题被汤大神 ... 
- Python+Django+SAE系列教程14-----使表单更安全
			还记得我们上一章提到过的加入页面吗? watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvaGVtZW5nMTk4MA==/font/5a6L5L2T/fonts ... 
- 一致性哈希算法(consistent hashing)样例+測试。
			一个简单的consistent hashing的样例,非常easy理解. 首先有一个设备类,定义了机器名和ip: public class Cache { public String name; pu ... 
- HDU 4597 Play Game 2013 ACM-ICPC吉林通化全国邀请赛H题
			九野的博客,转载请注明出处: http://blog.csdn.net/acmmmm/article/details/10833941 题意:给定T个测试数据,下面有2副牌,每副n张,每张都有一个分 ... 
- poj 3270 更换使用
			1.确定初始和目标状态. 明确.目标状态的排序状态. 2.得出置换群,.比如,数字是8 4 5 3 2 7,目标状态是2 3 4 5 7 8.能写为两个循环:(8 2 7)(4 3 5). 3.观察当 ... 
- 警惕!iPhone 6即将上市 诈骗邮件已现身网络
			随着iPhone 6即将上市,各路小道消息已经開始满天飞.就在近几日,一些记者还收到了假的iPhone 6将要上市的通知邮件.趋势科技也收到了几封,下面是样本之中的一个: (垃圾邮件样本) 不清楚iP ... 
- CentOS Kernel Source Install
			http://linuxmoz.com/centos-kernel-source-install/ 
- 西南民大oj(两园交求面积)
			西南民大oj:http://www.swunacm.com/acmhome/welcome.do?method=index 我的几何不可能那么可爱 时间限制(普通/Java) : 1000 MS/ 3 ... 
