zoukankan      html  css  js  c++  java
  • 畅通工程---并查集

    畅通工程

    Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
    Total Submission(s): 77565    Accepted Submission(s): 41180


    Problem Description
    某省调查城镇交通状况,得到现有城镇道路统计表,表中列出了每条道路直接连通的城镇。省政府“畅通工程”的目标是使全省任何两个城镇间都可以实现交通(但不一定有直接的道路相连,只要互相间接通过道路可达即可)。问最少还需要建设多少条道路?
     
    Input
    测试输入包含若干测试用例。每个测试用例的第1行给出两个正整数,分别是城镇数目N ( < 1000 )和道路数目M;随后的M行对应M条道路,每行给出一对正整数,分别是该条道路直接连通的两个城镇的编号。为简单起见,城镇从1到N编号。
    注意:两个城市之间可以有多条道路相通,也就是说
    3 3
    1 2
    1 2
    2 1
    这种输入也是合法的
    当N为0时,输入结束,该用例不被处理。
     
    Output
    对每个测试用例,在1行里输出最少还需要建设的道路数目。
     
    Sample Input
    4 2
    1 3
    4 3
    3 3
    1 2
    1 3
    2 3
    5 2
    1 2
    3 5
    999 0
    0
     
    Sample Output
    1
    0
    2
    998
     下面两个代码不同的地方在于求子集的方法不同
     
    第一种方法是利用根节点的根节点还是自己求(for循环判断p[i]==i)
     
    #include<iostream>
    using namespace std;
    int p[1000005], r[1000005];
    int n,t;
    void init()//初始化集合,每个元素的老板都是自己
    {
        for (int i = 1; i <= n; i++)
        {
            p[i] = i;
        }
    }
    
    int find(int x)//查找元素x的老板是谁
    {
        if (x == p[x])
            return x;
        else
            return p[x] = find(p[x]);
    }
    
    void join(int x, int y)//合并两个集合
    {
        int xRoot = find(x);
        int yRoot = find(y);
    
        if (xRoot == yRoot) //老板相同,不合并
            return;
        if (r[xRoot] < r[yRoot]) //r[i]是元素i所在树的高度,矮树的根节点认高树的根节点做老板
            p[xRoot] = yRoot;
        else if (r[xRoot] > r[yRoot])
            p[yRoot] = xRoot;
        else
        {
            p[yRoot] = xRoot;//树高相同,做老板的树高度要加一
            r[xRoot]++;
        }
    }
    
    bool sameRoot(int x, int y)//查询两个元素的老板是否相同
    {
        return find(x) == find(y);
    }
    int main()
    {
        int m, x, y;
        while (cin >> n && n != 0)
        {
            t = 0;
            cin >> m;
            init();
            for (int i = 0; i < m; i++)//把已知联通的元素加入集合
            {
                cin >> x >> y;
                if (sameRoot(x, y))
                    continue;
                else
                    join(x, y);
            }
            for (int i = 1; i <= n; i++)//有多少个p[i]==i,就有多少个子集
            {
                if (p[i] == i)
                    t++;
            }
            cout << t-1 << endl;
        }
        return 0;
    }

    第二种方法:假设刚开始所有元素各不相连,此时子集个数有cnt=n个,每加入一条边,子集个数减少一个

    #include<iostream>
    using namespace std;
    int p[1000005], r[1000005];
    int n,t,cnt;
    void init()//初始化集合,每个元素的老板都是自己
    {
        for (int i = 1; i <= n; i++)
        {
            p[i] = i;
        }
    }
    
    int find(int x)//查找元素x的老板是谁
    {
        if (x == p[x])
            return x;
        else
            return p[x] = find(p[x]);
    }
    
    void join(int x, int y)//合并两个集合
    {
        int xRoot = find(x);
        int yRoot = find(y);
    
        if (xRoot == yRoot) //老板相同,不合并
            return;
        cnt=cnt-1;
        if (r[xRoot] < r[yRoot]) //r[i]是元素i所在树的高度,矮树的根节点认高树的根节点做老板
            p[xRoot] = yRoot;
        else if (r[xRoot] > r[yRoot])
            p[yRoot] = xRoot;
        else
        {
            p[yRoot] = xRoot;//树高相同,做老板的树高度要加一
            r[xRoot]++;
        }
    }
    
    bool sameRoot(int x, int y)//查询两个元素的老板是否相同
    {
        return find(x) == find(y);
    }
    int main()
    {
        int m, x, y;
        while (cin >> n && n != 0)
        {
            cnt=n;//当元素各不相连的时候,有n个集合
            t = 0;
            cin >> m;
            init();
            for (int i = 0; i < m; i++)//把已知联通的元素加入集合
            {
                cin >> x >> y;
                if (sameRoot(x, y))
                    continue;
                else
                    join(x, y);
            }
            // for (int i = 1; i <= n; i++)//有多少个p[i]==i,就有多少个子集
            // {
            //     if (p[i] == i)
            //         t++;
            // }
            cout << cnt-1 << endl;
        }
        return 0;
    }
  • 相关阅读:
    Web服务器安全设置
    java大文件读写操作,java nio 之MappedByteBuffer,高效文件/内存映射
    Java IO和Java NIO在文件拷贝上的性能差异分析
    Java高效读取大文件
    NIO入门之轻松读取大文件
    我来说说java的NIO
    java读取大文件 超大文件的几种方法
    @RequestBody 的正确使用办法
    友鱼项目知识点
    怎样查看Tomcat动态控制台信息
  • 原文地址:https://www.cnblogs.com/-citywall123/p/10717232.html
Copyright © 2011-2022 走看看