判断是否是树(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 ...
随机推荐
- virtualbox中新版本Ubuntu安装软件增强包后重启无限登录界面的解决办法
原来我虚拟机版本是4.2.10,装的Ubuntu3.3,因为版本过老使用出现了一些问题,于是换成14.04,安装成功,但是装增强包的时候,装完重启,无限登录界面,密码是对的. 看了网上的很多方法,什么 ...
- poj 2007 Scrambled Polygon 极角排序
/** 极角排序输出,,, 主要atan2(y,x) 容易失精度,,用 bool cmp(point a,point b){ 5 if(cross(a-tmp,b-tmp)>0) 6 retur ...
- 斯坦福 IOS讲义 课件总结 二
1,OC里面传参数个数不同,可以是两个完全不同的方法.如 - (void)addCard:(Card *)card atTop:(BOOL)atTop; - (void)addCard:(Card * ...
- Win32 SecuritySetting
http://flylib.com/books/en/2.21.1.207/1/ http://blogs.technet.com/b/heyscriptingguy/archive/2011/11/ ...
- 在 Windows Media Center 中观看电视
如果计算机具备了必要的硬件,则可以在电脑上使用 Windows Media Center 观看.暂停和快退直播的电视节目及录制的电视节目. 通过 Windows Media Center 观看直播电视 ...
- Nginx 的 Echo 模块 —— echo-nginx-module(转)
Nginx 有个 echo 模块可以用来输出一些简单的信息,例如: ? 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 2 ...
- Mysql的Debug模式实现
前一段领导开发了一个内核的模块,测试的过程中,发现导致MYSQL客户端无法连接服务器. 经过查询文档,追根溯源,终于找到了MYSQL实现链接客户端的代码,在源文件sql-common/client.c ...
- HDOJ 1043 Eight(A* 搜索)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1043 思路分析: <1> 搜索算法: A*算法, Heuristic函数:曼哈顿距离 &l ...
- ios7毛玻璃效果实现
首先看效果: 核心代码: //加模糊效果,image是图片,blur是模糊度 - (UIImage *)blurryImage:(UIImage *)image withBlurLevel ...
- Android项目导入时,出现的Could not write file 。。。。。。.classpath错误解决办法
导入到Eclipse中后选择了相应的API后,红叉的项目错误没有了. 工程列表也无任何错误了.但出现了这样的提示框错误 说明的是.classpath这个环境文件不能写.随后,查看工程文件主目录下的.c ...