zoukankan      html  css  js  c++  java
  • UVALive5461 UVA615 POJ1308 Is It A Tree?(解法二)

    Regionals 1997 >> North America - North Central NA

    问题链接:UVALive5461 UVA615 POJ1308 Is It A Tree?

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

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

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

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

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

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

    /* UVALive5461 UVA615 POJ1308 Is It A Tree? */
    
    #include <iostream>
    #include <cstdio>
    
    using namespace std;
    
    const int MAXN = 100000;
    
    // 并查集
    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 isconnect() {
            int rootcount = 0;
            for( int i=0 ; i<=MAXN ; i++ )
                 if(visited[i])
                     rootcount++;
            return (rootcount == edgecount + 1);
        }
    
        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()
    {
        int src, dest, caseno=0;
        UF uf(MAXN);
    
        while(scanf("%d%d", &src, &dest) != EOF) {
            if(src == -1 && dest == -1)
                break;
    
            uf.init();
    
            if(src==0 && dest==0) {
                //为空树
                printf("Case %d is a tree.
    ", ++caseno);
            } else {
                uf.Union(src, dest);
                for(;;) {
                    scanf("%d%d", &src, &dest);
                    if( src==0 && dest==0 )
                        break;
    
                    uf.Union(src, dest);
                }
    
                if(uf.nocircle() && uf.isconnect())
                    printf("Case %d is a tree.
    ", ++caseno);
                else
                    printf("Case %d is not a tree.
    ", ++caseno);
            }
        }
    
        return 0;
    }


  • 相关阅读:
    Dom修改元素样式
    URL百分号编码
    accesskey附上一些实例
    dom实例
    dom 创建时间
    关系运算符
    赋值运算符
    js图片随机切换
    js自增图片切换
    transform-origin盒子旋转位置
  • 原文地址:https://www.cnblogs.com/tigerisland/p/7564095.html
Copyright © 2011-2022 走看看