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 ...
随机推荐
- mongodb远程连接以及备份、还原、导出、导入
一.远程连接mongodb 连接命令:mongo -u username -p pwd 192.168.41.215:27017/database(用户名对应的数据库) 二.mongodump备份数据 ...
- GTA项目 三, 使用 bootstrap table展示界面,使得data和UI分离
/** bootstrap-table - v1.5.0 - 2014-12-12* https://github.com/wenzhixin/bootstrap-table* Copyright ( ...
- 不等高cell的搭建(一)
一.界面搭建 1.确定开发模式 如果界面是固定的,可以用xib 界面的一些内容不固定,就用纯代码 cell用什么方式去开发(我们采用纯代码和xib结合的方式) 2 ...
- devexpress13学习系列(四)PDFViewer(4)
PdfViewer的属性: CurrentPageNumber:显示当前页的页码. DocumentFilePath:当前文件. DocumentProperties:文件属性. PageCount: ...
- MAXFLOAT
CGSizeMake(300, MAXFLOAT),是计算宽和高的,里面的MAXFLOAT通俗点说就是最大的数值,代表你的label的宽和高是随着你label内容而变化,不用担心因为label内容过长 ...
- Summary: Trie Data Structure
Implement a Trie Data Structure, and search() & insert() function: we need to implement both Cla ...
- eclipse jsp 加载服务器tomcat
1.window->Preferences
- LDA-math-LDA 文本建模
http://cos.name/2013/03/lda-math-lda-text-modeling/ 5. LDA 文本建模 5.1 游戏规则 对于上述的 PLSA 模型,贝叶斯学派显然是有意见的, ...
- android环境搭建—— 工欲善其事必先利其器
安卓开发环境配置: 准备软件 a) JDK 点击下载 b) adt-bundle-windows-x86-20140702.zip 点击下载 [sdk + 特定版本platform + e ...
- [待解决] sudo unable to resolve host
怪哉怪哉, 大debian突然就出现了这个问题 , 问题的现象是只要使用 sudo 执行命令就会出现 sudo unable to resolve host </etc/hostname中的内容 ...