hdoj 1325 Is It A Tree? 【并查集】
https://blog.csdn.net/shengweisong/article/details/34099151
做了一上午,最终ac了 wa了一次主要是忘了还有环!。!
主要是运用并查集知识。又复习了一次!!
思路:输入之后找能不能成环,成环就不是,其次还要推断是不是有两个父节点,假设有两个父节点也不是。之后就找相关的祖先就好了。
还要注意:假设仅仅有一个节点,也是树。假设有两个或多个根节点也不是树;假设没有根节点也不是
链接 pid=1325" rel="nofollow">http://acm.hdu.edu.cn/showproblem.php?pid=1325
代码
#include<stdio.h>
int fat[1000];
int father( int n ) //寻找祖先
{
if( fat[n] != n ) fat[n] = father(fat[n]);
return fat[n];
}
int main()
{
int i, j, n, m, a[1000], b[1000], v = 1;
while( 1 ){
for( i = 0; i < 1000; i ++ )
fat[i] = i;
int flag = 0;
scanf( "%d%d", &a[0], &b[0] );
if( a[0]< 0&&b[0]< 0 ) break;
fat[b[0]] = a[0];//由题意可知fat[b[i]] = a[i];
i = 1;
while( scanf("%d%d", &a[i], &b[i]), a[i]||b[i] ){
if( flag == 0 ){ //假设flag=1那就表明成环了,或者有两个父节点
if( fat[a[i]] == b[i] ){
flag = 1; //推断是不是成环
continue;
}
if( fat[b[i]] != b[i] ) {
flag = 1;//推断是不是有两个父节点
continue;
}
if( fat[a[i]]!= a[i] )
fat[a[i]] = father(a[i]);
fat[b[i]] = a[i];
}
else{
fat[b[i]] = a[i];
}
i++;
}
// for( j = 0; j < i; ++ j ) //以下四行没有必要,假设没有根节点。就是环(多亏了某仙鹤同学)
// if( fat[a[j]] == a[j] ) break;;
// if( i == j )
// flag = 1;
if( flag ){
printf( "Case %d is not a tree.\n", v++ );
continue;
}
else{
if( i == 1 ){ //假设仅仅有一组
printf( "Case %d is a tree.\n", v++ );
continue;
}
for( j = 1; j < i; ++ j ) //推断是不是有多个根节点
if( fat[a[j]] != fat[a[0]] ){
printf( "Case %d is not a tree.\n", v++ );
break;
}
if( j == i )
printf( "Case %d is a tree.\n", v++ );
}
}
return 0;
} hdoj 1325 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 ...
- 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 ...
- 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 ...
随机推荐
- 阿里云 Aliplayer高级功能介绍(四):直播时移
基本介绍 时移直播基于常规的HLS视频直播,直播推流被切分成TS分片,通过HLS协议向播放用户分发,用户请求的m3u8播放文件中包含不断刷新的TS分片地址:对于常规的HLS直播而言,TS分片地址及相应 ...
- alias用于设置当前数据表的别名,
alias用于设置当前数据表的别名,便于使用其他的连贯操作例如join方法等. 示例: $Model = M('User'); $Model->alias('a')->join('__DE ...
- Box 'laravel/homestead' could not be found.
vbox vagrant.box Homestead 都安裝好的情況下.在homestead目錄下,執行vagrant up出錯 Homestead\scripts\文件夹中,打开homestead. ...
- 最棒的7种R语言数据可视化
最棒的7种R语言数据可视化 随着数据量不断增加,抛开可视化技术讲故事是不可能的.数据可视化是一门将数字转化为有用知识的艺术. R语言编程提供一套建立可视化和展现数据的内置函数和库,让你学习这门艺术.在 ...
- Cesium官方教程7--三维模型
原文地址:https://cesiumjs.org/tutorials/3D-Models-Tutorial/ 三维模型 (3D Models) 这篇教程给大家介绍,如何在Cesium中通过Primi ...
- PAT甲级——A1095 Cars on Campus
Zhejiang University has 8 campuses and a lot of gates. From each gate we can collect the in/out time ...
- eclipse安装m2e
Installation You can install last M2Eclipse release by using the following update site from within E ...
- CentOS 编译golang
CentOS 安装Mercurial http://hi.baidu.com/lang2858/item/cda8f6026cd522e0f45ba67f 获取代码 $ hg clone -u rel ...
- rpm包安装过程中依赖问题“libc.so.6 is needed by XXX”解决方法-转
原文:http://raksmart.idcspy.com/781 在CentOS上的Canon LBP2900安装打印机驱动,中间遇到了一些问题,主要是安装rpm包出现的依赖问题,现在解决了,现在简 ...
- Referenced assembly does not have a strong name
Step 1 : Run visual studio command prompt and go to directory where your DLL located. For Example my ...