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
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.
Output
"Case k is not a tree.", where k corresponds to the test case number
(they are sequentially numbered starting with 1).
Sample Input
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
Sample Output
Case 1 is a tree.
Case 2 is a tree.
Case 3 is not a tree. 题目意思:判断所给的数据能否构成一颗树。 解题思路:题目中所给的是有向树,并给出了性质:
1.只有一个节点,称为根节点,没有定向边指向它。
2.除了根节点外,每个节点都只有有一条指向它的边。
3.从树根到任一结点有一条有向通路。
抽象过来就是三个条件:
1.只有一个入度为0的点,作为根节点。
2.除根节点外,其他点的入度只能为1。
3.所有点都能连通,也就是所有点需要在一个集合中,使用并查集来划分集合。 注意!!!可能会出现空树,也就是0 0,空树也是树。
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
const int MAX=1e4+;
int pre[MAX];///并查集记录父亲节点
int in[MAX];///入度
int vis[MAX];///节点是否存在
void init()
{
int i;
for(i=; i<MAX; i++)
{
vis[i]=;
in[i]=;
pre[i]=i;
}
}
int Find(int x)
{
if(pre[x]==x)
{
return x;
}
else
{
return Find(pre[x]);
}
}
void Union(int root1,int root2)
{
int x,y;
x=Find(root1);
y=Find(root2);
if(x!=y)
{
pre[x]=y;
}
}
int main()
{
int i,root,counts,a,b,flag,ans=;
while(scanf("%d%d",&a,&b)!=EOF)
{
if(a==-&&b==-)
{
break;
}
if(a==&&b==)///空树
{
printf("Case %d is a tree.\n",ans);
ans++;
continue;
}
init();
vis[a]=;
vis[b]=;
in[b]++;
Union(a,b);
while(scanf("%d%d",&a,&b)!=EOF)
{
if(a==&&b==)
{
break;
}
vis[a]=;
vis[b]=;
in[b]++;
Union(a,b);
}
flag=;
root=;
counts=;
for(i=;i<MAX;i++)
{
if(vis[i]&&in[i]==)///根节点个数
{
root++;
}
if(in[i]>=)///除根节点外,其他点入度需为1
{
flag=;
}
if(vis[i]&&pre[i]==i)///所有点都在一个集合中
{
counts++;
}
}
if(root!=||counts>)
{
flag=;
}
if(flag)
{
printf("Case %d is a tree.\n",ans);
ans++;
}
else
{
printf("Case %d is not a tree.\n",ans);
ans++;
}
}
return ;
}
Is It A Tree?(并查集)的更多相关文章
- Hdu.1325.Is It A Tree?(并查集)
Is It A Tree? Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) To ...
- Is It A Tree?(并查集)
Is It A Tree? Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 26002 Accepted: 8879 De ...
- CF109 C. Lucky Tree 并查集
Petya loves lucky numbers. We all know that lucky numbers are the positive integers whose decimal re ...
- HDU 5606 tree 并查集
tree 把每条边权是1的边断开,发现每个点离他最近的点个数就是他所在的连通块大小. 开一个并查集,每次读到边权是0的边就合并.最后Ansi=size[findset(i)],size表示每个并 ...
- [Swust OJ 856]--Huge Tree(并查集)
题目链接:http://acm.swust.edu.cn/problem/856/ Time limit(ms): 1000 Memory limit(kb): 10000 Description T ...
- Codeforces Round #363 (Div. 2)D. Fix a Tree(并查集)
D. Fix a Tree time limit per test 2 seconds memory limit per test 256 megabytes input standard input ...
- Is It A Tree?(并查集)(dfs也可以解决)
Is It A Tree? Time Limit:1000MS Memory Limit:10000KB 64bit IO Format:%I64d & %I64u Submi ...
- tree(并查集)
tree Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)Total Submis ...
- 树上统计treecnt(dsu on tree 并查集 正难则反)
题目链接 dalao们怎么都写的线段树合并啊.. dsu跑的好慢. \(Description\) 给定一棵\(n(n\leq 10^5)\)个点的树. 定义\(Tree[L,R]\)表示为了使得\( ...
- hdu 1325 Is It A Tree? 并查集
Is It A Tree? Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Tot ...
随机推荐
- java8 新特性 Stream流 分组 排序 过滤 多条件去重
private static List<User> list = new ArrayList<User>(); public static void main(String[] ...
- winfroms更换皮肤
一.添加控件lrisSkin.dll 然后把继承的窗体更换成别人做好的窗体类 能达到换肤的效果 二. 全部源代码就一行: skinEngine1.SkinFile = "WaveCo ...
- CF451E Devu and Flowers(组合数)
题目描述 Devu想用花去装饰他的花园,他已经购买了n个箱子,第i个箱子有fi朵花,在同一个的箱子里的所有花是同种颜色的(所以它们没有任何其他特征).另外,不存在两个箱子中的花是相同颜色的. 现在De ...
- 仓位 001 998 AUFNAHME不存在(L9009)
测试做一个物料库存561初始化时,库位是上启用了WM的.提示“C01 998 AUFNAHME 不存在”,998 库存余额的初始条目 是缺省的存储类型.用LS25在正式系统中,CO1 998下有AUF ...
- python使用findall正则匹配出所有符合条件的字符串
# -*- coding:utf-8 -*- import re mystr="qqq key:www.baidu.com<br>key:www.tengxun.com<b ...
- Python学习 :面向对象 -- 类的成员
类的成员 一.字段 - 字段可以分为'普通字段'和'静态字段'两种 - 普通字段(保存在对象中,执行时只能通过对象来进行访问) - 静态字段(保存在类中,在内存中只保存一份数据(可以有效地节约内存的空 ...
- 纪中OJ 2019.02.15【NOIP提高组】模拟 B 组 梦回三国 比赛题解(第一个)
声明 旁边的同学小 H(胡)对我说: “哟,比赛拿了 140,强!要知道,如果哥第三题 AC 了,哥就 230 了,你个废柴!!!(比赛实际分数 130 额呵)” 顿时,千万草泥马从我心中奔腾而过:你 ...
- 20155322 2016-2017-2 《Java程序设计》 第一周学习总结
20155322 2016-2017-2 <Java程序设计> 第一周学习总结 教材学习内容总结 本周学习内容的主要是: 一.浏览教材,根据自己的理解每章提出一个问题. 在浏览教材后,我提 ...
- vue 3.0使用 BUG解决
最近在做vue的项目,吊进了很多坑,这些坑很浅,但一旦掉进去了,不花点功夫还爬不出来.所以总结下,当做下笔记,持续更新 1.<router-link> 里加的事件没反应 错误代码: < ...
- Ajax在Django中的应用
一.什么是Ajax AJAX(Asynchronous Javascript And XML)翻译成中文就是“异步Javascript和XML”.即使用Javascript语言与服务器进行异步交互,传 ...