Is It A Tree?(并查集)
Time Limit: 1000MS | Memory Limit: 10000K | |
Total Submissions: 26002 | Accepted: 8879 |
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
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
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.
Source
并查集的简单应用,就是给的是不是一棵树,就在节点插入集合的时候看看两个节点的根节点是不是相同,如果相同,则说明这不是一棵树,如果都符合,则判断是不是形成了森林,比较坑的是空树也是一棵树
#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <queue>
#include <map>
#include <algorithm>
#define INF 0x3f3f3f3f
using namespace std; typedef unsigned long long LL; const int MAX = 1e5+10; int pre[11000]; int a[11000]; bool vis[11000]; int top;
bool flag; int Find(int x)
{
return pre[x]==-1?x:pre[x]=Find(pre[x]);
} void Join(int u,int v)
{
int Fx=Find(u);
int Fy=Find(v);
if(Fx!=Fy)
{
pre[Fx]=Fy;
}
else
{
flag=true;
}
} int main()
{
int u,v; int w=1;
while(scanf("%d %d",&u,&v))
{
if(u==-1&&v==-1)
{
break;
}
if(u==0&&v==0)//空树
{
printf("Case %d is a tree.\n",w++);
continue;
}
top=0;
memset(pre,-1,sizeof(pre));
memset(vis,false,sizeof(vis));
flag=false;
if(!vis[u])
{
a[top++]=u;
vis[u]=true;
}
if(!vis[v])
{
a[top++]=v;
vis[v]=true;
}
Join(u,v);
while(scanf("%d %d",&u,&v)&&(u||v))
{
if(!vis[u])
{
a[top++]=u;
vis[u]=true;
}
if(!vis[v])
{
a[top++]=v;
vis[v]=true;
}
Join(u,v);
}
printf("Case %d ",w++);
if(flag)
{
printf("is not a tree.\n");
}
else
{
int ans=0;
for(int i=0;i<top;i++)
{
if(pre[a[i]]==-1)
{
ans++;
if(ans>1)
{
break;
}
}
}
if(ans==1)
{
printf("is a tree.\n");
}
else
{
printf("is not a tree.\n");
}
}
}
return 0;
}
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 ...
- 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 ...
随机推荐
- wampserver下修改mysql root用户的登录密码
1.安装好wamp后,运行WampServer程序,进入MYSQL控制台; 2.进入控制台后,提示输入密码(不用输入任何密码,因为密码为空),按回车键进入; 3.输入“USE mysql;”然后回车, ...
- 解决Ueditor 不兼容IE7 和IE8
引用Ueditor的js 的时候用 绝对路径
- hduoj 4707 Pet 2013 ACM/ICPC Asia Regional Online —— Warmup
http://acm.hdu.edu.cn/showproblem.php?pid=4707 Pet Time Limit: 4000/2000 MS (Java/Others) Memory ...
- SQL查询 练习题
设有一数据库,包括四个表:学生表(Student).课程表(Course).成绩表(Score)以及教师信息表(Teacher).四个表的结构分别如表1-1的表(一)~表(四)所示,数据如表1-2的表 ...
- Java基础(48):归并排序的Java封装含原理,完整可运行,结合VisualGo网站更好理解)
原理: 归并排序建立在归并操作上的一种有效的排序算法,该算法是采用分治法(Divide and Conquer)的一个非常典型的应用. 将已有序的子序列合并,得到完全有序的序列:即先使每个子序列有序, ...
- demo03linearlayoutdemo;
package com.example.demo03linearlayoutdemo; import android.os.Bundle; import android.app.Activity; i ...
- MOPSO 多目标例子群优化算法
近年来,基于启发式的多目标优化技术得到了很大的发展,研究表明该技术比经典方法更实用和高效.有代表性的多目标优化算法主要有NSGA.NSGA-II.SPEA.SPEA2.PAES和PESA等.粒子群优化 ...
- PAT乙级 1020. 月饼 (25)(只得到23分)
1020. 月饼 (25) 时间限制 100 ms 内存限制 65536 kB 代码长度限制 8000 B 判题程序 Standard 作者 CHEN, Yue 月饼是中国人在中秋佳节时吃的一种传统食 ...
- 常用的网络配置命令 ifconfig 所在的包
通过rpm的数据库反查 ifconfig这个可执行文件的提供者,得出这个文件属于一个叫 net-tools 的包
- batch批的概念
批处理(Batch Requests), 批处理简单理解为同时执行的一批SQL处理语句,一个批处理中可能有多个DML.多个存储过程等等.如在SSMS操作,每个'GO'执行前都属于一个批处理. 注意区分 ...