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;
    }
  • 相关阅读:
    C#结构体指针的定义及使用详解
    C# out和ref关键字
    CListCtrl控件主要事件及LVN_ITEMCHANGED消息和鼠标双击列表项事件的处理
    深圳职位:替朋友招聘biztalk的技术人员 无为而为
    微软出最新SQL Server 2005 累积修补程序包(版本 9.0.2153)(200684),这是SP1之后的更新 无为而为
    微软Visual Studio 2005 SDK version 3.0 发布了,TFS扩展的开发人员/DSL/VSTS Test都应该关注 无为而为
    好东西,将你的英文版TFS变为中文版?:Visual Studio 2005 Team Foundation Server 语言更改包 无为而为
    Analysis Service计算[期间增长率]的若干注意 无为而为
    亲身体验:更改 Team Foundation Server 部署的语言版本是一个复杂耗时的过程 无为而为
    各大门户调查:鄙视新浪,将博士伦的广告挂在热点新闻中间好几天了,把大众当傻瓜 无为而为
  • 原文地址:https://www.cnblogs.com/-citywall123/p/10717232.html
Copyright © 2011-2022 走看看