Is It A Tree?[HDU1325][PKU1308]
Is It A Tree?
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 22720 Accepted Submission(s): 5168
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.
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
#include <stdio.h>
#include <algorithm>
#include <math.h>
#include <string.h>
using namespace std;
class Union_Find_Set {
#define MAX_UNION_FIND_SET_SIZE 100005
public:
int setSize;
int father[MAX_UNION_FIND_SET_SIZE];
Union_Find_Set() {
setSize = ;
}
Union_Find_Set(int x) {
setSize = x;
clear(x);
}
void clear(int x) {
for (int i = ; i < x; i++) {
father[i] = i;
}
}
int getFather(int x) {
if (x != father[x]) {
father[x] = getFather(father[x]);
}
return father[x];
}
bool merge(int a, int b) {
a = getFather(a);
b = getFather(b);
if (a != b) {
father[a] = b;
return true;
} else {
return false;
}
}
int countRoot() {
int ret = ;
for (int i = ; i < setSize; i++) {
if (father[i] = i) {
ret++;
}
}
return ret;
}
};
Union_Find_Set ufs();
bool v[];
int in[];
int main() {
int a, b, k = , n = , m = ;
bool f = true;
memset(v, false, sizeof(v));
memset(in, , sizeof(in));
while (scanf("%d%d", &a, &b) != EOF) {
if (a == && b == ) {
if (n != m + ) {
f = false;
}
printf("Case %d is %sa tree.\n", ++k, f ? "" : "not ");
ufs.clear();
n = m = ;
memset(v, false, sizeof(v));
memset(in, , sizeof(in));
f = true;
} else if (a < && b < ) {
break;
} else {
m++;
if (!v[a]) {
v[a] = true;
n++;
}
if (!v[b]) {
v[b] = true;
n++;
}
in[b]++;
if (in[b] > ) {
f = false;
}
if (!ufs.merge(b, a)) {
f = false;
}
}
}
return ;
}
Is It A Tree?[HDU1325][PKU1308]的更多相关文章
- Is It A Tree?(hdu1325)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1325 Is It A Tree? Time Limit: 2000/1000 MS (Java/Oth ...
- 【HDU1325】Is It A Tree?(并查集基础题)
有以下坑点: 1.结束输入不一定-1,题目中的叙述只是说所有权值都为正值. 2.是否构成一棵树不能只判断是否只有一个根节点,没有环路,而且还需要判断每个节点的入度一定是1,不然就不是一棵树. (无环路 ...
- hdu1325 Is It A Tree?并检查集合
pid=1325">职务地址 试想一下,在词和话题hdu1272是一样的. 可是hdu1272的博文中我也说了.数据比較水,所以我用非并查集的方法就AC了. 可是这题的数据没那么水,要 ...
- HDU1325 Is It A Tree? 【并查集】
Is It A Tree? Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) To ...
- hdu1325 Is It A Tree? 基础并查集
#include <stdio.h> #include <string.h> ], g[]; int find(int x) //并查集的查找,找到共同的父亲 { if (f[ ...
- POJ1308/HDU1325/NYOJ129-Is It A Tree?,并查集!
Is It A Tree? Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 28838 Accepted: 9843 -& ...
- hdu1325 Is It A Tree?(二叉树的推断)
Is It A Tree? Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) To ...
- [数据结构]——二叉树(Binary Tree)、二叉搜索树(Binary Search Tree)及其衍生算法
二叉树(Binary Tree)是最简单的树形数据结构,然而却十分精妙.其衍生出各种算法,以致于占据了数据结构的半壁江山.STL中大名顶顶的关联容器--集合(set).映射(map)便是使用二叉树实现 ...
- SAP CRM 树视图(TREE VIEW)
树视图可以用于表示数据的层次. 例如:SAP CRM中的组织结构数据可以表示为树视图. 在SAP CRM Web UI的术语当中,没有像表视图(table view)或者表单视图(form view) ...
随机推荐
- centos 软件包安装与管理
http://rpmfind.net/linux/RPM/index.html http://pkgs.org/ https://sourceforge.net/
- Python之路【第二十篇】Tornado框架
Tornado Tornado是使用Python编写的一个强大的.可扩展的Web服务器.它在处理严峻的网络流量时表现得足够强健,但却在创建和编写时有着足够的轻量级,并能够被用在大量的应用和工具中. 我 ...
- redis3.2 最新版本启动配置文件redis.conf详细说明
Redis.conf文件内容详细说明: # 默认redis不是以后台进程的方式启动,如果需要在后台运行,需要将这个值设置成yes # 以后台方式启动的时候,redis会写入默认的进程文件/var/ru ...
- Spring 入门知识
------------------------------------------------------------------------------------- Spring是什么? Spr ...
- 二叉树的层序遍历 BFS
二叉树的层序遍历,或者说是宽度优先便利,是经常考察的内容. 问题一:层序遍历二叉树并输出,直接输出结果即可,输出格式为一行. #include <iostream> #include &l ...
- golang笔记——map
通过 new 创建的引用类型对象是不完整创建,比如 map,它仅分配了字典类型本身所需的内存(指针包装),而没有分配键值存储内存,也没有初始化散列桶等内部属性,因此无法工作,如下代码就是错误的: p ...
- IBAction和IBOutlet
- IBAction: - 本质就是void - 能让方法具备连线的功能- IBOutlet - 能让属性具备连线的功能
- PHP如何获取Post请求中的Json字符串数据?
摘自:http://dianjingjiaoyu.blog.163.com/blog/static/18347920820114194642257/ 最近用到ext与PHP交互,ext把json数据p ...
- Extjs 制作柱状图
在JSP页面制作柱状图,可以根据数据的变化动态实时的变化 主要是使用EXTJS自带的插件达到效果 Ext.require('Ext.chart.*'); Ext.require([ 'Ext.Wind ...
- Qml 写的弹出层控件
QML弹出窗口组件,灯箱效果.动画效果,可拖拽 核心思路:一个mask层,一个最顶层,都用rectangle,禁止事件穿透 使用 Popup { id: popup width: 200; heigh ...