[并查集] hdu1325 Is it a tree ?

来源:互联网 发布:淘宝 苹果证书 编辑:程序博客网 时间:2024/06/02 13:28

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.

并查集,只需要合并和查询两个基本的操作。
要让连成的图称为一棵树,必须满足以下几个条件:

1、所有的节点都是连通的,即不能为森林。
2、节点自身不能和自己连接。
3、连接成的图中不能存在环路。

对于第一条,只要统计所有连通以后集合的个数即可。

对于第二条和第三条,只要用并查集中Find函数查询一下,在集合合并前是否连通,如果联通,则新加入的边就会造成环路,否则就可以联通。

根据这几点来操作,代码很快就来了。

#include<iostream>#include<cstdio>using namespace std;const int Max = 100005;int parent[Max];bool flag[Max];void make_set(){    for(int x = 1; x < Max; x ++)    {        parent[x] = x;        flag[x] = false;    }}int Find(int x){    if(x != parent[x])        parent[x] = Find(parent[x]);    return parent[x];}void mix(int x, int y){    x = Find(x);    y = Find(y);    if(x == y) return;    parent[y] = x;}int main(){    int x, y, t = 1, fir;    while(scanf("%d %d", &x, &y) != EOF)    {        if(x == -1 && y == -1) break;        if(x == 0 && y == 0)         //  第1类判断: 空树是一棵树。        {            printf("Case %d is a tree.\n", t ++);            continue;        }        make_set();        flag[x] = flag[y] = true;        fir = x;        bool tree = true;        if(x == y) tree = false;    //  第2类判断,同if(Find(x) == Find(y))。        else mix(x, y);        while(scanf("%d %d", &x, &y) && x != 0)        {            flag[x] = flag[y] = true;            if(Find(x) == Find(y)) tree = false;   //  第2类判断,不能存在环路。            mix(x, y);        }        for(x = 1; x < 100; x ++)   //  第3类判断:不能为森林。            if(flag[x] && Find(x) != Find(fir))                tree = false;        if(tree) printf("Case %d is a tree.\n", t ++);        else printf("Case %d is not a tree.\n", t ++);    }    return 0;}
0 0
原创粉丝点击