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 有一段时间没写最大流的题了,这题建图竟然想了好长时间... 刚開始是按着终于的最大流即是做多轮 ...
随机推荐
- UVA 10313(完全背包变形)
Problem B Pay the Price Input: standard input Output: standard output Time Limit: 2 seconds Memory L ...
- java学习笔记04--数组
java学习笔记04--数组 数组复制的方法是使用System类提供的arraycopy()方法,其语法如下: System.arraycopy(Object src, int srcPos, Obj ...
- iOS:点击button卡死
场景: 在tableView的Cell中有一个button,我须要点击这个button然后使得其视图控制器上的一个视图改变frame,可是我点击这个button后,导致卡死,也不崩溃.所有事 ...
- C++晋升之dynamic_cast
danamic_cast 动态类型转换 ----RTTI提供的的操作符 ----动态:在执行阶段 ----类型转换:检測指针或引用类型,true->转换 ----体现价值的地方:用于多态 --- ...
- python安装依赖
yum install zlib zlib-devel openssl openssl-devel bzip2 bzip2-devel ncurses ncurses-devel readline r ...
- 整理自百度知道提问的几道Java编程题
蚂蚁爬杆 问题描述: 有一根27厘米的细木杆,在第3厘米.7厘米.11厘米.17厘米.23厘米这五个位置上各有一只蚂蚁.木杆很细,不能同时通过一只蚂蚁.开始时,蚂蚁的头朝左还是朝右是任意的,它们只会朝 ...
- 微信支付.net官方坑太多,我们来精简
原文:微信支付.net官方坑太多,我们来精简 微信支付官方坑太多,我们来精简 我把官方的代码,打包成了 an.wxapi.dll. 里面主要替换了下注释.呵呵.然后修改了几个地方. 修改一.Confi ...
- Android自己定义组件系列【2】——Scroller类
在上一篇中介绍了View类的scrollTo和scrollBy两个方法,对这两个方法不太了解的朋友能够先看<自己定义View及ViewGroup> scrollTo和scrollBy尽管实 ...
- 杭州电ACM1098——Ignatius's puzzle
这个话题.简单的数学. 对于函数,f(x)=5*x^13+13*x^5+k*a*x,输入k,对于休闲x,一个数字的存在a,使f(x)是65可分. 对于休闲x. 因此,当x = 1时间,f(x) = 1 ...
- poj-2195-Going Home最小费用最大流
重新切一遍最小费用最大流~~~ 这到题目的数据范围有问题,尽量开大就好了~~ #include<stdio.h> #include<iostream> #include< ...