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 ...
随机推荐
-
centos下编译phantomjs2.0
phantomjs是一个无头浏览器,可以用来做测试和爬虫,但是因为有一些问题没有解决,所以官网不提供2.0版本的binary包,所以要自己编译. 1.安装需要的依赖: sudo yum -y inst ...
-
java之真假分页
真分页(要的是什么范围的记录在数据库查的时候就只查这几条记录):select s.* from (select *,row_number() over(order by SLoginId) as ro ...
-
提纲挈领webrtc之vad检测
顾名思义,VAD(Voice Activity Detection)算法的作用是检测是否是人的语音,它的使用 范围极广,降噪,语音识别等领域都需要有vad检测.vad检测有很多方法,这里我们之介绍一 ...
-
[java基础] java中的自动装箱与自动拆箱
自动装箱的一个例子: Integer i = 1; //实际上是执行了Integer i = Integer.valueOf(1) 自动拆箱的一个例子: Integer a =1; int b = a ...
-
一天搞懂深度学习-训练深度神经网络(DNN)的要点
前言 这是<一天搞懂深度学习>的第二部分 一.选择合适的损失函数 典型的损失函数有平方误差损失函数和交叉熵损失函数. 交叉熵损失函数: 选择不同的损失函数会有不同的训练效果 二.mini- ...
-
SQL设置时间格式
SELECT STR_TO_DATE('Jul 20 2013 7:49:14:610AM','%b %d %Y %h:%i:%s:%f%p') from DUAL; -- 执行后得到结果:'2013 ...
-
Centos配置tomcat服务并且开机自启动
把要配置成服务的tomcat文件夹中的catalina.sh脚本文件拷一份到/etc/init.d目录,并且改文件名称为tomcat6 cp /usr/web/tomcat/tomcat-/bin/c ...
-
第三章 document对象及数组
1.数组的使用(1)声明数组var 数组名=new Array();(2)数组赋值数组名[下标]=值: 2.数组声明,分配空间,赋值同时进行var 数组名=new Array(值1,值2....)va ...
-
Docker(十二)-Docker Registry镜像管理
Registry删除镜像.垃圾回收 Docker仓库在2.1版本中支持了删除镜像的API,但这个删除操作只会删除镜像元数据,不会删除层数据.在2.4版本中对这一问题进行了解决,增加了一个垃圾回收命令, ...
-
scala 时间格式转换(String、Long、Date)
1)scala 时间格式转换(String.Long.Date) 1.时间字符类型转Date类型 [java] view plain copy import java.text.SimpleDateF ...