判断是否是树(Is It A Tree?)
Description
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.
Input
Output
Sample Input
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
Sample Output
Case 2 is a tree.
Case 3 is not a tree.
#include <stdio.h>
#define max_num 100000+10
typedef struct
{
int vis, root, conn;
}Node;
Node node[max_num];
void init()
{
for(int i = ; i < max_num; i++)
{
node[i].vis = ;
node[i].root = i;
node[i].conn = ;
}
}
int find_root(int x)
{
while(x != node[x].root)
x = node[x].root;
return x;
}
void union_set(int x, int y)
{
x = find_root(x);
y = find_root(y);
if(x != y)
node[y].root = x;
}
int main()
{
int n, m;
bool flag = true;
int case_num = ;
init();
//freopen("input.txt", "r", stdin);
while(scanf("%d%d", &n, &m), n >= && m >= )
{
if(!flag && (n != && m != ))
continue;
if(n == && m == )
{
int root_num = ;
for(int i = ; i < max_num; i++)
{
if(find_root(i) == i && node[i].vis)
root_num++;
if(node[i].conn > )
flag = false;
}
if(root_num > )
flag = false;
if(flag)
printf("Case %d is a tree.\n", case_num++);
else
printf("Case %d is not a tree.\n", case_num++);
flag = true;
init();
continue;
}
if(n != m && find_root(n) == find_root(m))
flag = false;
else
{
node[n].vis = ;
node[m].vis = ;
node[m].conn++;
union_set(n, m);
}
}
return ;
}
判断是否是树(Is It A Tree?)的更多相关文章
- poj1308+HOJ1325,判断是否为树
POJ 应该是判断是否为简单无环连通图,用并查集直接秒杀即可,而HOJ的是有向树,还需判断所有点的入度必需小于2,用一个类似hash[]数组判断一下即可, ////判断树之一:入度<=1:三:点 ...
- 动态树之LCT(link-cut tree)讲解
动态树是一类要求维护森林的连通性的题的总称,这类问题要求维护某个点到根的某些数据,支持树的切分,合并,以及对子树的某些操作.其中解决这一问题的某些简化版(不包括对子树的操作)的基础数据结构就是LCT( ...
- [剑指Offer]判断一棵树为平衡二叉树(递归)
题目链接 https://www.nowcoder.com/practice/8b3b95850edb4115918ecebdf1b4d222?tpId=0&tqId=0&rp=2&a ...
- HDU-1232 畅通工程 (并查集、判断图中树的棵数)
Description 某省调查城镇交通状况,得到现有城镇道路统计表,表中列出了每条道路直接连通的城镇.省政府“畅通工程”的目标是使全省任何两个城镇间都可以实现交通(但不一定有直接的道路相连,只要互相 ...
- 判断一棵树是否为二叉搜索树(二叉排序树) python
输入一棵树,判断这棵树是否为二叉搜索树.首先要知道什么是排序二叉树,二叉排序树是这样定义的,二叉排序树或者是一棵空树,或者是具有下列性质的二叉树: (1)若左子树不空,则左子树上所有结点的值均小于它的 ...
- 【UOJ#388】【UNR#3】配对树(线段树,dsu on tree)
[UOJ#388][UNR#3]配对树(线段树,dsu on tree) 题面 UOJ 题解 考虑一个固定区间怎么计算答案,把这些点搞下来建树,然后\(dp\),不难发现一个点如果子树内能够匹配的话就 ...
- E - Is It A Tree? 并查集判断是否为树
题目链接:https://vjudge.net/contest/271361#problem/E 具体思路:运用并查集,每一次连接上一个点,更新他的父亲节点,如果父亲节点相同,则构不成树,因为入读是2 ...
- 15.Subtree of Another Tree(判断一棵树是否为另一颗树的子树)
Level: Easy 题目描述: Given two non-empty binary trees s and t, check whether tree t has exactly the s ...
- POJ-1308 Is It A Tree?(并查集判断是否是树)
http://poj.org/problem?id=1308 Description A tree is a well-known data structure that is either empt ...
随机推荐
- BestCoder Round #46
1001 YJC tricks time 题目链接:1001 题意:给你时针和分针所成的角度,输出现在的时间,以10秒为单位 思路:每10秒,分针走1度,时针走分针的1/12,我们可以根据时间来分别计 ...
- dubbo架构演变之路
背景 (#) 随着互联网的发展,网站应用的规模不断扩大,常规的垂直应用架构已无法应对,分布式服务架构以及流动计算架构势在必行,亟需一个治理系统确保架构有条不紊的演进. 单一应用架构 当网站流量很小时, ...
- Android 开发笔记 “Sqlite Cursor 使用”
使用过 SQLite 数据库的童鞋对 Cursor 应该不陌生,如果你是搞.net 开发你大可以把Cursor理解成 Ado.net 中的数据集合相当于dataReader.今天特地将它单独拿出来谈, ...
- LintCode-两个字符串是变位词
题目描述: 写出一个函数 anagram(s, t) 去判断两个字符串是否是颠倒字母顺序构成的 样例 给出 s="abcd",t="dcab",返回 true ...
- Protel99Se使用方法详解
Protel99SE是应用于Windows9X/2000/NT操作系统下的EDA设计软件,采用设计库管理模式,可以进行联网设计,具有很强的数据交换能力和开放性及3D模拟功能,是一个32位的设计软件,可 ...
- 在windows下配置对github的操作--基本操作
一.下载安装 git for widows软件 git for widows 是专门用来在windows下操作 github的软件,提供bash(命令行) 和 gui两种方式. 在bash下,其实就是 ...
- MySQL对于有大量重复数据表的处理方法
需要在MySQL的一张innodb引擎的表(tableA)上添加一个唯一索引(idx_col1_u).但是对于每个key(col1)表中已经有大量重复数据.此时,做数据的手工清理,或者SQL处理是非常 ...
- 开始QT+OpenCV学问
最近一个月.由于超声造影软件工具做.因此,开始接触OpenCV.使用OpenCV的话.除了图像处理,其它功能都非常弱.所以又開始学习MFC. 从原先的.net C#编程环境一下变成MFC还真有点不习惯 ...
- Ext JS学习第三天 我们所熟悉的javascript(二)
•javascript之函数 •对于Ext开发者,我还是希望你能对javascript原生的东西非常了解.甚至熟练掌握运用.那么函数,无疑是非常重要的概念.首先在前面一讲,我们知道了函数也是一种数据类 ...
- C#语言基础之运算符
运算符分类.优先级 运算符:一.数学运算符:+,-,*,/,++,-- 示例1: 示例2: 示例3: 1.递增运算符:++(1)前缀递增运算符 int x=4; x++;//输出结果,x的 ...