您的位置:首页 > 其它

HDU 1272 小希的迷宫 + 1325 Is It A Tree? , 并查集

2012-07-13 09:15 537 查看
题目链接:

小希的迷宫:http://acm.hdu.edu.cn/showproblem.php?pid=1272

Is It A Tree:http://acm.hdu.edu.cn/showproblem.php?pid=1325

题目类型: 并查集

题目:

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.




题目大意:
HDU的小希的迷宫是Is It A Tree?这一题的汉化版。题目翻译可看小希的迷宫。
大概意思是给定一些结点的关系, 然后要判断这些结点是否组成一棵树。

分析与总结:
判断是否是一棵树的几个要点:
(1)判断是否有回路,有回路的就不是树。这个可用并查集来做。一旦给定的两个结点a,b是属于同一个集合的,那么便可判定有回路。

(2)看是否只有一个连通分支, 如果有多个,那么就成了森林。 这个可以用并查集来判断。最终并查集只有一个根结点,

(3)判断顶点数是否等于边数加1,如果不等,则说明不是树。

(4)判断节点的入度是否<=1,如果大于1,则说明不是树;

(5)判断两个节点的父节点是否相等,如果相等,则不能构成树;

并查集的学习: 并查集--学习详解
/article/1632596.html

Is It A Tree代码:



#include<iostream>
#include<cstdio>
#include<cstring>
#define N 120005
using namespace std;
int father
, rank
,in
,out
;
bool vis
, isCircle;

void initSet(){
    for(int i=1; i<N; ++i){
        father[i]=i, rank[i]=0;
        in[i] = 0, out[i] = 0;
    }
}

int find(int x){
    int i, j = x;
    while(j!=father[j]) j=father[j];
    while(x!=j){
        i = father[x];
        father[x] = j;
        x = i;
    }
    return j;
}

void Union(int x, int y){
    int a = find(x);
    int b = find(y);
    if(a == b){
        isCircle = true;
        return;
    }
    if(rank[a] > rank)
        father[b] = a;
    else{
        if(rank[a] == rank[b])
            rank[b]++;
        father[a] = b;
    } 
}

int main(){
#ifdef LOCAL
    freopen("input.txt","r",stdin);
#endif
    int a,b,cas=1;
    bool flag=true;
    
    while(~scanf("%d %d",&a,&b) && a>=0 && b>=0){
        if(!a && !b){
            printf("Case %d is a tree.\n",cas++);
            continue;
        }

        int edge = 1;
        memset(vis, 0, sizeof(vis));
        flag = true;
        isCircle = false;
        initSet();

        int Max = a>b?a:b;
        int Min = a<b?a:b;

        if(a==b) flag=false;
        vis[a]=vis[b]=true;
        ++in[b];
        ++out[a];
        Union(a, b);

        while(~scanf("%d %d",&a,&b) && a && b){
            vis[a] = vis[b] = true;
            if(a==b) flag=false;
            if(Min > a) Min = a;
            if(Min > b) Min = b;
            if(Max < a) Max = a;
            if(Max < b) Max = b;

            ++in[b];
            if(in[b]>1) flag=false;

            ++edge;

            Union(a,b);
        }
        int numConnect=0, node=0;
        for(int i=Min; i<=Max; ++i) if(vis[i] ){
            ++node;
            if(father[i]==i)
                ++numConnect;
        }
        
        if(node!=edge+1) flag = false;

        if(flag && !isCircle && numConnect==1) printf("Case %d is a tree.\n", cas++);
        else printf("Case %d is not a tree.\n",cas++);
    }
    return 0;
}





[b]

—— 生命的意义,在于赋予它意义。


原创 http://blog.csdn.net/shuangde800 , By
D_Double
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: