HDU1325 Is It A Tree?【并查集】

Is It A Tree?

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 27345    Accepted Submission(s): 6311


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

Source


问题链接:HDU1325 Is It A Tree?

问题简述

  若干组测试用例,每个测试用例包括若干组边(两个正整数组成),最后两个0(0 0)结束。判定每个测试用例是否为一棵树。

问题分析

  判定有向图是否为一棵树的问题。可以用那些边构造一个并查集,构建并查集时,如果有向边的两个结点的根相同则不是一棵树,同时所有的结点指向的根应该是相同的。

  注意点:结点虽然用整数表示,然而是随意的,而且范围不定。

程序说明

  程序中,假定最大的结点不超过1000。将所有结点放入集合中,判定结点的根是否相同时,只需要考虑集合中的元素。

  代码不够简洁,又写了一个简洁版。

参考链接UVALive5461 UVA615 POJ1308 HDU1325 Is It A Tree?


AC的C++语言程序(简洁版)如下:

/* HDU1325 Is It A Tree? */

#include <iostream>
#include <stdio.h>

using namespace std;

const int N = 1000;
int f[N + 1];
bool visited[N + 1];
bool nocircleflag;    // 环标记
int edgecount;      // 边计数

void UFInit(int n)
{
    for(int i = 0; i <=n; i++) {
        f[i] = i;
        visited[i] = false;
    }
    nocircleflag = true;
    edgecount = 0;
}

int Find(int a) {
    return a == f[a] ? a : f[a] = Find(f[a]);
}

void Union(int a, int b) {
    edgecount++;
    visited[a] = true;
    visited[b] = true;

    int pa = Find(a);
    int pb = Find(b);
    if(pa == pb || pb != b) {
        nocircleflag = false;
    } else {
        f[pa] = pb;
    }
}

bool isconnect() {
    int cnt = 0;
    for( int i=1 ; i<=N ; i++ )
         if(visited[i])
             cnt++;
    return (cnt == edgecount + 1);
}

int main()
{
    int src, dest, caseno=0;

    while(scanf("%d%d", &src, &dest) != EOF) {
        if(src < 0 && dest < 0)     // 非常怪啊!!!
            break;

        if(src==0 && dest==0) {
            //为空树
            printf("Case %d is a tree.\n", ++caseno);
        } else {
            UFInit(N);

            Union(src, dest);
            for(;;) {
                scanf("%d%d", &src, &dest);
                if( src==0 && dest==0 )
                    break;

                Union(src, dest);
            }

            if(nocircleflag && isconnect())
                printf("Case %d is a tree.\n", ++caseno);
            else
                printf("Case %d is not a tree.\n", ++caseno);
        }
    }

    return 0;
}



AC的C++语言程序如下:

/* HDU1325 Is It A Tree? */

#include <iostream>
#include <cstdio>
#include <cstring>

using namespace std;

const int MAXN = 1000;

// 并查集
class UF {
private:
    int v[MAXN+1];
    bool visited[MAXN+1];
    int length;
    bool nocircleflag;    // 环标记
    int edgecount;      // 边计数
public:
    UF(int n) {
        length = n;
    }

    // 压缩
    int Find(int x) {
        if(x == v[x])
            return x;
        else
            return v[x] = Find(v[x]);
    }

    bool Union(int x, int y) {
        edgecount++;
        visited[x] = true;
        visited[y] = true;

        x = Find(x);
        y = Find(y);
        if(x == y) {
            nocircleflag = false;
            return false;
        } else {
            v[x] = y;
            return true;
        }
    }

    bool Union2(int x, int y) {
        edgecount++;
        visited[x] = true;
        visited[y] = true;

        int x2 = Find(x);
        int y2 = Find(y);
        if(x2 == y2 || y2 != y) {
            nocircleflag = false;
            return false;
        } else {
            v[x2] = y2;
            return true;
        }
    }

    // 计数法判定连通性
    bool isconnect() {
        int rootcount = 0;
        for( int i=0 ; i<=MAXN ; i++ )
             if(visited[i])
                 rootcount++;
        return (rootcount == edgecount + 1);
    }

    // 唯一树根判定连通性
    bool isconnect2() {
        int root = -1;
        for( int i=0 ; i<=MAXN ; i++ )
             if(visited[i]) {
                 if(root == -1)
                    root = Find(i);
                 else
                     if(Find(i) != root)
                         return false;
             }
        return true;
    }

    inline bool nocircle() {
        return nocircleflag;
    }

    void init() {
        nocircleflag = true;
        edgecount = 0;
        for(int i=0; i<=length; i++)
            v[i] = i, visited[i] = false;
    }
};

int main()
{
    UF uf(MAXN);

    int src, dest, caseno=0;
    while(scanf("%d%d", &src, &dest)!=EOF) {
        if(src < 0 && dest < 0)     // 非常怪啊!!!
            break;

        uf.init();

        uf.Union2(src, dest);
        while(scanf("%d%d", &src, &dest)) {
            if(src==0 && dest==0)
                break;

            uf.Union2(src, dest);
        }

        if(uf.nocircle() && uf.isconnect2())
            printf("Case %d is a tree.\n", ++caseno);
        else
            printf("Case %d is not a tree.\n", ++caseno);
    }

    return 0;
}

参考链接:hdu1325Is It A Tree?(并查集)


评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值