POJ 1308 HDU 1358 Is It A Tree? 并查集 (好多WA点)

本文介绍了一种使用并查集算法判断一组节点连接是否构成有效树结构的方法,详细解析了实现思路与注意事项。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >


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, 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

The input will consist of a sequence of descriptions (test cases) followed by a pair of negative integers. Each test case will consist of a sequence of edge descriptions followed by a pair of zeroes Each edge description will consist 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

For each test case display the line ``Case k is a tree." or the line ``Case k is not a tree.", where k corresponds to the test case number (they are sequentially numbered starting with 1).
 

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.
题意:就是让你判断给定的一系列数能不能构成一棵树。
思路:先提一下什么是树吧:第一如果结点为0称之为空树,不是空树则有以下性质:
1.子树不相交
2.一棵N个结点数有N-1条边
3.除了根节点外,每个结点有且仅有一个父节点
掌握了这些让我们先转换下思想:
1.子树不相交就是说属于给定的数不会有相同的父节点
2.N个节点N-1条边表示加入组的数最后只有一个父节点就是说不是森林
细节问题:
1.超时:首先是并查集的朴素写法肯定会超时的,学会路径压缩。
2.WA点:a.是最后结束的时候是两个小于0的数,但是poj上写-1也可以过的,b.就是说一定是树不是森林,c.如果重复出现的根和叶,那也不是树(此题真是恶心)
3.并非简单的并查集。
AC代码:
#include <iostream> #include <cstdio>
using namespace std;
int flag; int fu[2010]; int zi[2010][2]; int be[2010]; int lizi; int sum;
void init(){     flag = 1;     for(int i = 1; i < 2001 ; i++){         fu[i] =i;     } }
int found(int x){     int r = x;     while(r != fu[r]){         r = fu[r];     }     int k = x;     while(k != r){         int j = fu[r];         fu[k] = r;         k = j;     }     return r; }
void  bing(int x,int y){     int xx = found(x);     int yy = found(y);     if( xx == yy ){         flag = 0;         return ;     }     if(xx != yy){         fu[yy] = x;     } }
int main(){      int a,b;      init();      int i;      bool tt = true;      lizi++;      while(tt){         init();         sum = 0;          while(scanf("%d%d",&a,&b) != EOF && a != 0 && b != 0){                 if(a < 0 && b < 0){                     tt = false;                     break;                 }                 for(i = sum - 1 ; i >= 0; i-- ){                     if((a == zi[i][1] && b == zi[i][0]) || (b == zi[i][1] && a != zi[i][0])){//形成环或者是森林(一个孩子有两个爸爸)                         flag = 0;                         break;                     }                     if(a == zi[i][0] && b == zi[i][1]){//数据重复                         flag = 0;                         break;                     }                 }                 if( i == -1){                     zi[sum][0] = a;                     zi[sum][1] = b;                     sum++;                 }          }          if(flag == 1){             for(i = 0; i < sum;++i){                 bing(zi[i][0],zi[i][1]);             }          }          for(i = 0;i < sum - 1;++i ){//判断所有的结点是否只有一个根             if(fu[zi[i][0]] != fu[zi[i+1][0]]){                 flag = 0;                 break;             }          }         if(flag == 0 && tt == true){             printf("Case %d is not a tree.\n",lizi);         }         if(flag == 1 && tt == true){             printf("Case %d is a tree.\n",lizi);         }         lizi++;
     }     return 0; } 给些测试数据:
0 0 是树
1 1 0 0不是树
1 2 1 2 0 0不是树
1 2 2 3 3 1 0 0不是树
总结:
这道题卡我好长时间,收获好多,知道了树的一些特性。很开心的AC 了。继续努力。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值