题目链接

描述

A tree is a well-known data structure that is either empty (null, void, nothing) or is a set of one or more nodes connected by directed edges between nodes satisfying the following properties.

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.

  • 输入

    The input will consist of a sequence of descriptions (test cases) 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.The number of test cases will not more than 20,and the number of the node will not exceed 10000.The inputs will be ended by a pair of -1.

  • 输出

    For each test case display the line "Case k is a tree." or the line "Case k is not a tree.", where k corresponds to the test case number (they are sequentially numbered starting with 1).

  • 样例输入

    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
  • 样例输出

    Case 1 is a tree.

    Case 2 is a tree.

    Case 3 is not a tree.

分析:

首先我们要理解对于一棵树的要求:

1.树中的每一个节点的父节点有且仅有一个(根节点除外,根节点没有父节点),每一个节点的孩子节点可以有一个到多个;

2.树是一种从父节点向下指向孩子节点的结构,不能出现一个孩子节点指向其父节点,或则父节点的父节点等情况;

3.对于一棵树来说,有且仅能有一个根节点。

应用并查集的知识来判断,如果要加入的子节点已经有父节点,或则是说要加入的父节点和孩子节点有相同的父节点的话,这都是在加入节点的时候都不满足树的条件的,之后的系欸按就没有必要加入了,因为已经不满足。或则树建成以后,发现不仅仅有一棵树,也不满足。

代码:

#include<stdio.h>
#include<iostream>
#include<stack>
#include<string.h>
#include<stdlib.h>
#include<queue>
#include<algorithm>
#include<set>
using namespace std;
int parent[10000];
int flag;
void init()///初始化,把每个节点的父节点都当作其本身
{
for(int i=1; i<=10000; i++)
parent[i]=i;
} int Find(int a)///递归查找结点a的父节点
{
if(parent[a]==a) return a;
else
return Find(parent[a]);
}
void UpDate(int a,int b)
{
int x=Find(a);
int y=Find(b);
// cout<<"a=="<<a<<"x=="<<x<<"b=="<<b<<"y=="<<y<<endl;
if(parent[b]!=b||x==y)///表示b在添加父节点a之前已经有父节点,或则a与b有相同的父节点这就构成了环
{
flag=1;
}
if(x!=y)
{
parent[y]=x;
}
}
int main()
{
int a,b;
int op=1;
init();
flag=0;
set<int>s;
set<int>::iterator it;///为set集合定义一个迭代器
while(~scanf("%d%d",&a,&b))
{
if(a==0&&b==0)///当前这棵树结束的标志
{
int sum=0;
for(it=s.begin(); it!=s.end(); it++)
{
if(Find(*it)==*it)///如果他的父节点是他自己,也就意为着他是整棵树的根节点
sum++;
if(sum>1)///根结点的个数多于一个,肯定不是一棵树
{
flag=1;///标记当前的已经不满足一棵树的条件了
break;
}
}
if(flag==0)
printf("Case %d is a tree.\n",op);
if(flag==1)
printf("Case %d is not a tree.\n",op);
s.clear();///set集合清空
init();///再重新把每个节点的父节点指向其本身
op++;
flag=0;
}
else if(a==-1&&b==-1)///整个输入结束的标志
break;
else
{
s.insert(a);
s.insert(b);
if(flag==0)
UpDate(a,b);///只有当前的这个树还满足数的结构在加入判断
else///如果已经不满足树的结构了,就没有必要在进行了
continue;
}
}
return 0;
}

NYOJ 129 树的判定 (并查集)的更多相关文章

  1. nyoj 129 树的判定

    并查集+欧拉 树的判定 时间限制:1000 ms  |  内存限制:65535 KB 难度:4   描述 A tree is a well-known data structure that is e ...

  2. [BZOJ3038]上帝造题的七分钟2 树状数组+并查集

    考试的时候用了两个树状数组去优化,暴力修改,树状数组维护修改后区间差值还有最终求和,最后骗了40分.. 这道题有好多种做法,求和好说,最主要的是开方.这道题过的关键就是掌握一点:在数据范围内,最多开方 ...

  3. hdu 5458 Stability(树链剖分+并查集)

    Stability Time Limit: 3000/2000 MS (Java/Others)    Memory Limit: 65535/102400 K (Java/Others)Total ...

  4. 【BZOJ4025】二分图(线段树分治,并查集)

    [BZOJ4025]二分图(线段树分治,并查集) 题面 BZOJ 题解 是一个二分图,等价于不存在奇环. 那么直接线段树分治,用并查集维护到达根节点的距离,只计算就好了. #include<io ...

  5. 【CF938G】Shortest Path Queries(线段树分治,并查集,线性基)

    [CF938G]Shortest Path Queries(线段树分治,并查集,线性基) 题面 CF 洛谷 题解 吼题啊. 对于每个边,我们用一个\(map\)维护它出现的时间, 发现询问单点,边的出 ...

  6. 【loj6038】「雅礼集训 2017 Day5」远行 树的直径+并查集+LCT

    题目描述 给你 $n$ 个点,支持 $m$ 次操作,每次为以下两种:连一条边,保证连完后是一棵树/森林:询问一个点能到达的最远的点与该点的距离.强制在线. $n\le 3\times 10^5$ ,$ ...

  7. 【bzoj2870】最长道路tree 树的直径+并查集

    题目描述 给定一棵N个点的树,求树上一条链使得链的长度乘链上所有点中的最小权值所得的积最大. 其中链长度定义为链上点的个数. 输入 第一行N 第二行N个数分别表示1~N的点权v[i] 接下来N-1行每 ...

  8. POJ 2985 The k-th Largest Group(树状数组 并查集/查找第k大的数)

    传送门 The k-th Largest Group Time Limit: 2000MS   Memory Limit: 131072K Total Submissions: 8690   Acce ...

  9. BZOJ-2049 Cave洞穴勘测 动态树Link-Cut-Tree (并查集骗分TAT)

    2049: [Sdoi2008]Cave 洞穴勘测 Time Limit: 10 Sec Memory Limit: 259 MB Submit: 5833 Solved: 2666 [Submit] ...

随机推荐

  1. centOS7设置静态ip后无法上网的解决,【亲可测】

    最近在VMware虚拟机里玩Centos,装好后发现上不了网.经过一番艰辛的折腾,终于找到出解决问题的方法了.最终的效果是无论是ping内网IP还是ping外网ip,都能正常ping通.方法四步走: ...

  2. PHP中类中成员及常量

    类中成员概述 面向对象编程,是需要通过“对象”去做什么事情(以完成某种任务): 而: 对象总是来源于类: 所以: 面向对象的编程,一切都是从定义类开始: 类中成员分为3大类: 属性: 方法: 常量: ...

  3. 第203天:js---Array对象常用方法

    1.shift:删除原数组的第一项,返回删除元素的值:如果数组为空则返回undefined //shift:删除原数组的第一项,返回删除元素的值:如果数组为空则返回undefined var arr ...

  4. 51nod 1574 排列转换(贪心+鸽巢原理)

    题意:有两个长度为n的排列p和s.要求通过交换使得p变成s.交换 pi 和 pj 的代价是|i-j|.要求使用最少的代价让p变成s. 考虑两个数字pi和pj,假如交换他们能使得pi到目标的距离减少,p ...

  5. luogu 1066 引水入城(bfs+贪心)

    90分,有一个点TLE.... 首先可以证明一个东西,如果从上面一排的某个点bfs一次到最下面一排的饮水点不是一个区间的话,那么最后一定所有饮水点不会被覆盖完的. 证明考虑反证法. 所以从上面一排的每 ...

  6. BZOJ 1566 管道取珠(DP)

    求方案数的平方之和.这个看起来很难解决.如果转化为求方案数的有序对的个数.那么就相当于求A和B同时取,最后序列一样的种数. 令dp[i][j][k]表示A在上管道取了i个,下管道取了j个,B在上管道取 ...

  7. BZOJ3574 HNOI2014抄卡组(哈希)

    容易发现通配符中间的部分可以任意匹配,会造成的无法匹配的仅仅是前后缀,前缀和后缀可以分别独立处理.如果字符串均有通配符,只需要按前/后缀长度排序然后暴力匹配就可以了. 问题在于存在无通配符的字符串.显 ...

  8. 【ListBox】ListBox的相关操作

    Winform中两个listbox的操作是平时比较常用的操作. 本次将以一个Winform实例来分享一下两个listbox的操作,包括:listbox添加项,项的上移下移等操作. 假设有两个listb ...

  9. 【BZOJ4945】【NOI2017】游戏(搜索,2-sat)

    [NOI2017]游戏(搜索,2-sat) 题面 BZOJ的SPJ是假的 兹磁洛谷 题解 如果没有\(x\)地图的影响 这就是一个裸的\(2-sat\)问题 但是现在有不超过\(8\)个\(x\)地图 ...

  10. 使用Unity5.1进行VR开发的配置(最新的未必是最好的!!!)

    随着Unity5.1的发布,之前的Oculus Rift和Gear VR 开发流程发生了巨大的变化,这也算是小白鼠们必须付出的代价了~ 那么Unity5.1和Oculus的整合究竟发生了哪些变化,对开 ...