Is It A Tree?(并查集)

来源:互联网 发布:黑客用linux 编辑:程序博客网 时间:2024/05/21 19:36

Is It A Tree?

Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 1206 Accepted Submission(s): 378 
Problem 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, 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 45 6 0 08 1 7 3 6 2 8 9 7 57 4 7 8 7 6 0 03 8 6 8 6 45 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
North Central North America 1997
 
Recommend
Ignatius.L
 

树(tree)是包含n(n>0)个结点的有穷集,其中:

(1)每个元素称为结点(node);
(2)有一个特定的结点被称为根结点或树根(root)。
(3)除根结点之外的其余数据元素被分为m(m≥0)个互不相交的集合T1,T2,……Tm-1,其中每一个集合Ti(1<=i<=m)本身也是一棵树,被称作原树的子树(subtree)。
树也可以这样定义:树是由根结点和若干颗子树构成的。树是由一个集合以及在该集合上定义的一种关系构成的。集合中的元素称为树的结点,所定义的关系称为父子关系。父子关系在树的结点之间建立了一个层次结构。在这种层次结构中有一个结点具有特殊的地位,这个结点称为该树的根结点,或称为树根。
我们可以形式地给出树的递归定义如下:
单个结点是一棵树,树根就是该结点本身。
设T1,T2,..,Tk是树,它们的根结点分别为n1,n2,..,nk。用一个新结点n作为n1,n2,..,nk的父亲,则得到一棵新树,结点n就是新树的根。我们称n1,n2,..,nk为一组兄弟结点,它们都是结点n的子结点。我们还称T1,T2,..,Tk为结点n的子树。
空集合也是树,称为空树。空树中没有结点。

由m(m>=0)棵互不相交的树的集合称为森林;

这道题和前面小希的迷宫有很大的相似之处!只不过一个是无向图一个是有向图,给你节点信息,让你判断是不是树!无环,n个结点最多有n-1条边,不然就会有环。只有一个入度为0的结点,不存在入度大于1的结点,其次还要注意几种特殊的情况

1: 0 0 空树是一棵树2: 1 1 0 0 不是树 不能自己指向自己3: 1 2 1 2 0 0 不是树4: 1 2 2 3 4 5 不是树  森林不算是树5: 1 2 2 3 3 4 4 5 5 6 6 7 7 8 8 9 9 1  注意 一个节点在指向自己的父亲或祖先 都是错误的 即 9-->1 错6: 1 2 2 1 0 0 也是错误的代码:
#include <bits/stdc++.h>using namespace std;const int max_num = 100000+10;struct  Node {    int num,root,conn;};Node node[max_num];   void init()   //初始化{    for(int i = 0; i < max_num; i++)    {        node[i].conn = 0;        node[i].root= i;        node[i].num=0;    }}int find_root(int a)        //查找树根{    if(node[a].root!=a)    return node[a].root = find_root(node[a].root);    return node[a].root;}void union_set(int a,int b)       //建立新的集合{    a = find_root(a);    b = find_root(b);    if(a==b)    return;    node[b].root=a;}int main(){    int n,m;    int i = 1;    bool flag=true;    init();    while(cin>>n>>m&&n>=0&&m>=0)        {        if(!flag&&n!=0&&n!=0)continue;        if(n==0&&m==0)        {            int root_num=0;            for(int j = 1; j < max_num;j++)            {                if(node[j].num && find_root(j)==j)                root_num++;                                  //树根                if(node[j].conn>1)                {                    flag = false;                    break;                }            }            if(root_num>1)                flag=false;     //森林            if(flag)            cout<<"Case "<<i++<<" is a tree."<<endl;            else cout<<"Case "<<i++<<" is not a tree."<<endl;            flag = true;            init();            continue;        }        if(m!=n&&find_root(n)==find_root(m))             flag = false;        else        {                    node[m].num = 1;            node[n].num = 1;            node[m].conn++;            union_set(n,m);        }    }    return 0;}



原创粉丝点击