Is It A Tree?

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.

这个就是一个比较简单的并查集的应用,但是因为我少了一步在找到根节点后进行压缩,就一直TLE;卡了好久;
思路是:判断两点:
(1)每一个子节点只有一个父节点;
(2)只有一个根节点;
在查找每一个子节点的根节点的时候如果没有进行压缩就会TLE;

#include <stdio.h>
#include <math.h>
#include <stdlib.h>
#include <math.h>
#include <string.h>
#include <algorithm>
#include <iostream>
#define N 105
#define INF 0x3f3f3f3f

using namespace std;
int fa[N];
bool flag[N];

void Begin()
{
    for(int i=1;i<N;i++)
    {
        fa[i]=i;
        flag[i]=false;
    }
}

int Find(int x)///寻找根节点,并且压缩(如果没有压缩就会TLE)
{
    if(x != fa[x])
        fa[x] = Find(fa[x]);
    return fa[x];
}

void Union(int x, int y)
{
    x=Find(x);
    y=Find(y);
    if(x==y)
        return;
    fa[y]=x;
}

bool Root(int n)///判断只有一个根
{
    int i=1;
    while(i<=n&&!flag[i])
    {
        ++i;
    }
    int root=Find(i);
    while(i<=n)
    {
        if(flag[i]&&Find(i)!=root)
        {
            return false;
        }
        ++i;
    }
    return true;
}

int main()
{
    int i, j, a, b, f, t, k=0;
    bool tree=true;
    t=1;
    Begin();
    while(~scanf("%d%d", &a, &b))
    {
        if(a<0 || b<0)
        {
            break;
        }
        if(a==0||b==0)
        {
            if(tree&&Root(k))
            {
                printf("Case %d is a tree.\n", t++);
            }
            else
            {
                printf("Case %d is not a tree.\n", t++);
            }
            tree=true;
            k=0;
            Begin();
            continue;
        }
        if(!tree)
        {
            continue;
        }
        k=(a>k)?a:k;
        k=(b>k)?b:k;
        flag[a]=flag[b]=true;
        if(Find(a)==Find(b))///如果两者有同一个根节点,并且两者还有父子关系,那么无法形成树
        {
            tree=false;
        }
        Union(a, b);
    }
    return 0;
}
原文地址:https://www.cnblogs.com/zct994861943/p/7161500.html