N - Is It A Tree? 并查集
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
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.
#include <iostream>
#include <string>
#include<cstring>
#include<cstdio>
#include <vector>
#include <algorithm>
using namespace std;
const int MAXN = ;
int pre[MAXN],tmp[MAXN],cnt=;
int find(int x)
{
if(pre[x]==-)
return x;
else
return pre[x] = find(pre[x]);
};
int main()
{
int x,y;
bool f = false;
memset(pre,-,sizeof(pre));
int cas=;
while(scanf("%d%d",&x,&y))
{
if(x==-&&y==-) break;
if(x==&&y==)
{
int num = ,ft = find(tmp[]);
if(!f)
{
for(int i=;i<cnt;i++)
{
if(find(tmp[i])!=ft)
num++;
}
if(num) f = true;
}
if(!f)
printf("Case %d is a tree.\n",cas);
else
printf("Case %d is not a tree.\n",cas);
memset(pre,-,sizeof(pre));
cas++;
f = false;
cnt = ;
continue;
}
int fx=find(x),fy=find(y);
tmp[cnt++] = x;
tmp[cnt++] = y;
if(fx!=fy)
pre[fy] = fx;
else
f = true;
}
return ;
}
N - 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 ...
随机推荐
- less新手入门(五)—— CssGuards、循环、合并
九. CssGuards 警卫也可以应用于css选择器,这是一种语法糖,用于声明mixin,然后立即调用它. 例如,在1.5.0之前,您必须这样做 .my-optional-style() when ...
- Bryce1010 Acm模板
目录 STL标准模板库 STL简介 STL pair STL set STL vector STL string STL stack STL queue STL map upper_bound和low ...
- 树形DP UVA 1292 Strategic game
题目传送门 /* 题解:选择一个点,它相邻的点都当做被选择,问最少选择多少点将所有点都被选择 树形DP:dp[i][0/1]表示当前点选或不选,如果选,相邻的点可选可不选,取最小值 */ /***** ...
- 常用JavaScript代码库(又名:WFang.js)
1.根据公司项目封装ajax请求,结合layer框架一起使用 /*提取接口公共部分*/ var ApiConf = { server:"http://localhost:8080/Batte ...
- 【转】HIVE UDF UDAF UDTF 区别 使用
原博文出自于:http://blog.csdn.net/longzilong216/article/details/23921235(暂时) 感谢! 自己写代码时候的利用到的模板 UDF步骤: 1 ...
- WCF学习笔记(2)-WCF的通讯过程
一.WCF中的ABC 场景:公司让你送一份合同文件,送文件的过程你可以选择的交通方式有打的,地铁或公交. 到了对方公司后,你要找到某负责人,并且要一份收到合同文件的回执和相应文件 要完成这项工作任务主 ...
- python学习笔记(2)——练习小程序之 " input " 隐藏陷阱
练习小程序之 ----------" input " 隐藏陷阱 age=input('please enter your age:') if age>=18: print(' ...
- 语义分割:使用关系图辅助图像分割-Capsule Network、IceNet
文章:欲取代CNN的Capsule Network究竟是什么来头?它能为AI界带来革命性转折么? 文章:用于分类.检测和分割的移动网络 MobileNetV2 网络 文章:后RCNN时代的物体检测及分 ...
- golang 自定义time.Time json输出格式
工作中使用golang时,遇到了一个问题.声明的struct含time.Time类型.使用json格式化struct时,time.Time被格式化成”2006-01-02T15:04:05.99999 ...
- 【sqli-labs】 less64 GET -Challenge -Blind -130 queries allowed -Variation3 (GET型 挑战 盲注 只允许130次查询 变化3)
双括号整型 http://192.168.136.128/sqli-labs-master/Less-64/?id=1)) or ((1