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 有一段时间没写最大流的题了,这题建图竟然想了好长时间... 刚開始是按着终于的最大流即是做多轮 ...
随机推荐
- C++的for循环细节,必看!
C++中.For(A;B;C) C语句是在每次循环后才运行. 如: y=10; for( i=0;i<10;y=++i) { cout<<y<<endl; } ...
- poj2299--B - Ultra-QuickSort(线段树,离散化)
Ultra-QuickSort Time Limit: 7000MS Memory Limit: 65536K Total Submissions: 41215 Accepted: 14915 ...
- JavaBean在DAO设计模式简介
一.信息系统开发框架 客户层-------显示层-------业务层---------数据层---------数据库 1.客户层:客户层是client,简单的来说就是浏览器. 2.显示层:JSP/Se ...
- 温故知新-------jQuery层次选择器
<html xmlns="http://www.w3.org/1999/xhtml"> <head> <title></titl ...
- 使用国内源解决Qt在线更新慢的问题
Qt在线安装更新工具默认使用官方的源,国内访问比较慢,可以在setting中增加国内的源来提高更新速度,如下面的源: http://mirrors.ustc.edu.cn/qtproject/onli ...
- linux+nginx+mysql+php
LNMP(linux+nginx+mysql+php)服务器环境配置 一.简介 Nginx是俄罗斯人编写的十分轻量级的HTTP服务器,Nginx,它的发音为 “engine X”, 是一个高性能的 ...
- OpenCL 查看设备信息
好久没搞OpenCL了.可是这是个好东西.不能不学,之前发了篇设置OpenCL的文章.看的人还真多,看来大家都知道这个好东西了,都想把OpenCL搞起.只是学习难度还是相当高的. 之前忙搞算法,所以非 ...
- MySQL将表a中查询的数据插入到表b中
MySQL将表a中查询的数据插入到表b中 假设表b存在 insert into b select * from a; 假设表b不存在 create table b as select * from a ...
- oracle ebs 12.20 安装成功其过程失败日记及总结(1)
由于公司业务须要,须要安装oracle ebs进行 form 开发,所以就開始了痛苦oracle ebs安装之过程.刚開始是在vm中win2003 server 中安装ebs,,不知是我自已的水平太差 ...
- Java8高中并发
Java8中学并发 本文翻译自:http://jaxenter.com/lean-concurrency-in-java-8-49924.html 转载请注明出处:http://blog.csdn.n ...