判断是否是树(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 ...
随机推荐
- 两种mysql文件安装方式——win7 32位OS
官网给出的安装包有两种格式,一个是msi格式,一个是zip格式的. 1. .ZIP格式安装 http://jingyan.baidu.com/article/f3ad7d0ffc061a09c3345 ...
- shopnc b2b2c如何开启伪静态??
shopnc b2b2c开启伪静态的方法 一. windows环境下 1.先下载isapi rewrite插件,安装,然后我们把根目录下面的htaccess.txt那么修改成.htaccess即可. ...
- (Problem 46)Goldbach's other conjecture
It was proposed by Christian Goldbach that every odd composite number can be written as the sum of a ...
- 关于Qt信号与槽机制的传递方向性研究(结论其实是错误的,但是可以看看分析过程)
最近由于项目的需求,一直在研究Qt.信号与槽机制是Qt的一大特色,该机制允许两者间传递参数,依次来实现对象间的通信.这个参数会分别存在于信号的参数列表和槽函数的参数列表中.需要注意的是,若将槽函数绑定 ...
- 通往WinDbg的捷径
通往WinDbg的捷径(一) 原文:http://www.debuginfo.com/articles/easywindbg.html译者:arhat时间:2006年4月13日关键词:CDB WinD ...
- spring boot 下 500 404 错误页面处理
spring boot 作为微服务的便捷框架,在错误页面处理上也有一些新的处理,不同于之前的spring mvc 500的页面处理是比较简单的,用java config或者xml的形式,定义如下的be ...
- [转载]cin、cin.get()、cin.getline()、getline()、gets()函数的用法
1.cin>> 用法1:最基本,也是最常用的用法,输入一个数字: #include <iostream>using namespace std;main ( ...
- Jedis中的一致性hash
Jedis中的一致性hash 本文仅供大家参考,不保证正确性,有问题请及时指出 一致性hash就不多说了,网上有很多说的很好的文章,这里说说Jedis中的Shard是如何使用一致性hash的,也为大家 ...
- paip.最新的c++ qt5.1.1环境搭建跟hello world
paip.最新的c++ qt5.1.1环境搭建跟hello world 作者Attilax , EMAIL:1466519819@qq.com 来源:attilax的专栏 地址:http://bl ...
- [译]Stairway to Integration Services Level 18 – 部署和执行
介绍 在本文中,我们要创建一个SSIS Catalog 实例,部署我们的项目,并且运行 weather data loader 包. SSIS 2012 部署模型 SSIS 2012 Deploy ...