POJ1308 Is It A Tree? (并查集判断图是否是一棵树)

题目链接

http://poj.org/problem?id=1308

题目

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.

题意

给定一个有向图,判断该图是否是一棵树。

分析

并查集的本质是森林,每个集合都是一棵树,树根是集合的代表元。
故图要是一棵树,则每条边的两个点在合并前必须在两个不同集合中且最后只能剩下一棵树。

和HDU1272 小希的迷宫是一道题欸。

AC代码

//16ms 0.9MB
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
const int maxn=1e5+100;
int par[maxn];
void init()
{
    for(int i=0;i<maxn;i++) par[i]=i;
}
int find(int x)
{
    return par[x]==x?x:par[x]=find(par[x]);
}
void unit(int x,int y)
{
    par[find(x)]=find(y);
}
bool same(int x,int y)
{
    return find(x)==find(y);
}
int vis[maxn];
int main()
{
    int x,y,d=0;
    while(1)
    {

        bool ok=true;
        init();
        int sum=0;
        int kase=0;
        memset(vis,0,sizeof(vis));
        while(~scanf("%d%d",&x,&y))
        {
            kase++;
            if(x==-1 && y==-1) return 0;
            if(x==0 && y==0)
            {
                break;
            }
            if(!vis[x]) vis[x]=1,sum++;
            if(!vis[y]) vis[y]=1,sum++;
            if(same(x,y)) ok=false;
            else sum--,unit(x,y);

        }
        printf("Case %d ",++d);
        if(kase==1)//0 0需要特判,很坑
        {
            printf("is a tree.\n");
            continue;
        }
        if(ok && sum==1) printf("is a tree.\n");
        else printf("is not a tree.\n");
    }
    return 0;
}

猜你喜欢

转载自blog.csdn.net/qq_37685156/article/details/80578389
今日推荐