hdoj 1325 Is It A Tree? 【并查集】

做了一上午,终于ac了 wa了一次主要是忘了还有环!!!

主要是运用并查集知识,又复习了一次!!

思路:输入之后找能不能成环,成环就不是,其次还要判断是不是有两个父节点,如果有两个父节点也不是,之后就找相关的祖先就好了;

还要注意:如果只有一个节点,也是树,如果有两个或多个根节点也不是树;如果没有根节点也不是

链接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? 【并查集】,布布扣,bubuko.com

时间: 2024-10-27 04:51:59

hdoj 1325 Is It A Tree? 【并查集】的相关文章

HDU 1325 Is It A Tree? 并查集

判断是否为树 森林不是树 空树也是树 成环不是树 数据: 1 2 2 3 3 4 4 5 5 6 6 7 7 8 8 9 9 1 0 0 1 2 2 3 4 5 0 0 2 5 0 0 ans: no no yes #include <stdio.h> #include <string.h> #include <stdlib.h> #include <limits.h> #include <malloc.h> #include <ctype

HDU1325 Is It A Tree? 并查集

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1325 这题与HDU1272 小希的迷宫 (并查集) 非常像,不过细细看,还是有一点区别的.就是这题的路径是单向的,每次只能由起点指向终点,在连接之前终点必须是根节点. 注意的问题: 1.不能成环,即每次输入的两个数的根节点不能相同: 2.最终根节点数目为一 3.注意当只输入"0 0" 时要输出"Case %d is a tree." 4.路径是单向的,即每次只能由起点指

HDU 1325 POJ 1308 Is It A Tree? (并查集)

这道题就是裸并查集,关键在于对不是树几种的判断 1. 空树是树 2. 森林不是树 3. 无环 或者从入度来看:1,无环:2,除了根,所有的入度为1,根入度为0:3,这个结构只有一个根,不然是森林了. 这道题本来暑假做的POJ 1308 但是HDU没有过.在于空树没有考虑. 用并查集判断有多少个森林注意编号是随机的,不是次序.... /* input: 0 0 1 1 0 0 1 2 1 2 0 0 1 2 2 3 4 5 0 0 1 2 2 3 3 4 4 5 5 6 6 7 7 8 8 9 9

HDU 5606 tree 并查集

tree 把每条边权是1的边断开,发现每个点离他最近的点个数就是他所在的连通块大小. 开一个并查集,每次读到边权是0的边就合并.最后Ans?i??=size[findset(i)],size表示每个并查集根的size Ans_i=size[findset(i)],sizeAns?i??=size[findset(i)],size表示每个并查集根的sizesize. #include<cstdio> #include<cstring> #include<algorithm>

hdu5606 tree (并查集)

tree Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)Total Submission(s): 823    Accepted Submission(s): 394 Problem Description There is a tree(the tree is a connected graph which contains n points and n−1 edges),t

hdoj 1116 Play on Words 【并查集】+【欧拉路】

Play on Words Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 5622    Accepted Submission(s): 1850 Problem Description Some of the secret doors contain a very interesting word puzzle. The team

swust oj 856--Huge Tree(并查集)

题目链接:http://acm.swust.edu.cn/problem/856/ Time limit(ms): 1000 Memory limit(kb): 10000 There are N trees in a forest. At first, each tree contains only one node as its root. And each node is marked with a number. You're asked to do the following two

[POJ 1308]Is It A Tree?(并查集判断图是否为一棵有根树)

Description A tree is a well-known data structure that is either empty (null, void, nothing) or is a set of one or more nodes connected by directed edges between nodes satisfying the following properties. There is exactly one node, called the root, t

CF109 C. Lucky Tree 并查集

Petya loves lucky numbers. We all know that lucky numbers are the positive integers whose decimal representations contain only the lucky digits 4 and 7. For example, numbers 47, 744, 4 are lucky and 5, 17, 467 are not. One day Petya encountered a tre