并查集

九度1012:畅通工程 题目地址:http://ac.jobdu.com/problem.php?pid=1012

题目描述:

    某省调查城镇交通状况,得到现有城镇道路统计表,表中列出了每条道路直接连通的城镇。省政府“畅通工程”的目标是使全省任何两个城镇间都可以实现交通(但不一定有直接的道路相连,只要互相间接通过道路可达即可)。问最少还需要建设多少条道路?

输入:

    测试输入包含若干测试用例。每个测试用例的第1行给出两个正整数,分别是城镇数目N ( < 1000 )和道路数目M;随后的M行对应M条道路,每行给出一对正整数,分别是该条道路直接连通的两个城镇的编号。为简单起见,城镇从1到N编号。 
    注意:两个城市之间可以有多条道路相通,也就是说
    3 3
    1 2
    1 2
    2 1
    这种输入也是合法的
    当N为0时,输入结束,该用例不被处理。

输出:

    对每个测试用例,在1行里输出最少还需要建设的道路数目。

样例输入:
4 2
1 3
4 3
3 3
1 2
1 3
2 3
5 2
1 2
3 5
999 0
0
样例输出:
1
0
2
998

  解题思路:根据题意,可以看出是求连通分量的个数,答案是连通分量个数-1。并查集解决这类问题很方便

#include<iostream>
using namespace std;
 
#define MAX 1001
int root[MAX];
 
int findroot(int x){
    if(root[x] == -1) return x;
    else return findroot(root[x]);
}
 
int main(){
    int n,m,i,c1,c2,ans;
    while(cin >> n){
        if(n == 0) break;
         
        for(i = 1;i <= n;i++) root[i] = -1;
         
        cin >> m;
        for(i = 0;i < m;i++){
            cin >> c1 >> c2;
            int a = findroot(c1);
            int b = findroot(c2);
            //如果两个城市不在同一个连通分量,则合并它们所在的两个连通分量
            if(a != b) root[a] = b; 
        }
         
        ans = 0;
        for(i = 1;i <= n;i++)//找联通分量个数 
            if(root[i] == -1) ans++;
         
        cout << ans - 1 << endl;
    }
     
    return 0;
}
 
/**************************************************************
    Problem: 1012
    User: cherish
    Language: C++
    Result: Accepted
    Time:10 ms
    Memory:1524 kb
****************************************************************/

九度1444:More is better 题目地址:http://ac.jobdu.com/problem.php?pid=1444 题目描述:

Mr Wang wants some boys to help him with a project. Because the project is rather complex, the more boys come, the better it will be. Of course there are certain requirements.Mr Wang selected a room big enough to hold the boys. The boy who are not been chosen has to leave the room immediately. There are 10000000 boys in the room numbered from 1 to 10000000 at the very beginning. After Mr Wang's selection any two of them who are still in this room should be friends (direct or indirect), or there is only one boy left. Given all the direct friend-pairs, you should decide the best way.

输入:

The first line of the input contains an integer n (0 ≤ n ≤ 100 000) - the number of direct friend-pairs. The following n lines each contains a pair of numbers A and B separated by a single space that suggests A and B are direct friends. (A ≠ B, 1 ≤ A, B ≤ 10000000)

输出:

The output in one line contains exactly one integer equals to the maximum number of boys Mr Wang may keep.

样例输入:
4
1 2
3 4
5 6
1 6
4
1 2
3 4
5 6
7 8
样例输出:
4
2
解题思路:这题也是一个与连通分量相关的题目。不过该题要求的答案是具有最多节点个数的连通分量所含的节点个数。也可以轻松用并查集解决。

#include<iostream>
using namespace std;
 
#define N 10000001
 
int root[N];
 
//sum[i]用来记录以i为根节点的连通分量中的节点个数
int sum[N];
 
int findroot(int x){
    if(root[x] == -1) return x;
    else return findroot(root[x]);
} 
 
int main(){
    int n,i,b1,b2,ans;
    while(cin >> n){
         
        for(i = 1;i < N;i++){
            root[i] = -1;
            sum[i] = 1;
        }
        for(i = 0;i < n;i++){
            cin >> b1 >> b2;
            int a = findroot(b1);
            int b = findroot(b2);
            if(a != b){
                root[a] = b;
                 
                //每次合并两个连通分量,更新连通分量的节点数目 
                sum[b] += sum[a];
            }
        }
         
        ans = 1;//至少有一个节点
        for(i = 1;i < N;i++){
            if(root[i] = -1 && sum[i] > ans) ans = sum[i];
        } 
         
        cout << ans << endl;
    }
     
    return 0;
}
 
/**************************************************************
    Problem: 1444
    User: cherish
    Language: C++
    Result: Accepted
    Time:1370 ms
    Memory:79644 kb
****************************************************************/

九度1109:连通图 题目地址:http://ac.jobdu.com/problem.php?pid=1109 题目描述:

    给定一个无向图和其中的所有边,判断这个图是否所有顶点都是连通的。

输入:

    每组数据的第一行是两个整数 n 和 m(0<=n<=1000)。n 表示图的顶点数目,m 表示图中边的数目。如果 n 为 0 表示输入结束。随后有 m 行数据,每行有两个值 x 和 y(0<x, y <=n),表示顶点 x 和 y 相连,顶点的编号从 1 开始计算。输入不保证这些边是否重复。

输出:

    对于每组输入数据,如果所有顶点都是连通的,输出"YES",否则输出"NO"。

样例输入:
4 3
1 2
2 3
3 2
3 2
1 2
2 3
0 0
样例输出:
NO
YES
#include<iostream>
using namespace std;
 
#define N 1000
int root[N];
 
int findroot(int x){
    if(root[x] == -1) return x;
    else return findroot(root[x]);
}
 
int main(){
    int n,m,i,ans,v1,v2;
    while(cin >> n){
        if(n == 0) break;
         
        for(i = 1;i <= n;i++) root[i] = -1;
         
        cin >> m;
        for(i = 0;i < m;i++){
            cin >> v1 >> v2;
            int a = findroot(v1);
            int b = findroot(v2);
            if(a != b) root[a] = b;
        }
         
        ans = 0;
        for(i = 1;i <= n;i++){
            if(root[i] == -1) ans++;
            if(ans > 1){
                cout << "NO" << endl;
                break;
            }
        }
        if(ans == 1) cout << "YES" << endl; 
             
    }
     
    return 0;
}
 
/**************************************************************
    Problem: 1109
    User: cherish
    Language: C++
    Result: Accepted
    Time:30 ms
    Memory:1524 kb

九度1445:How many tables? 题目地址:http://ac.jobdu.com/problem.php?pid=1445 题目描述:

Today is Ignatius' birthday. He invites a lot of friends. Now it's dinner time. Ignatius wants to know how many tables he needs at least. You have to notice that not all the friends know each other, and all the friends do not want to stay with strangers.

One important rule for this problem is that if I tell you A knows B, and B knows C, that means A, B, C know each other, so they can stay in one table.

For example: If I tell you A knows B, B knows C, and D knows E, so A, B, C can stay in one table, and D, E have to stay in the other one. So Ignatius needs 2 tables at least.

输入:

The input starts with an integer T(1<=T<=25) which indicate the number of test cases. Then T test cases follow. Each test case starts with two integers N and M(1<=N,M<=1000). N indicates the number of friends, the friends are marked from 1 to N. Then M lines follow. Each line consists of two integers A and B(A!=B), that means friend A and friend B know each other. There will be a blank line between two cases.

输出:

For each test case, just output how many tables Ignatius needs at least. Do NOT print any blanks.

样例输入:
2
5 3
1 2
2 3
4 5

5 1
2 5
样例输出:
2
4
解题思路:该题同样是求连通分量的个数的题。但是在路径未压缩的情况下使用并查集会出现超时。所以要进行一下路径压缩。
#include<iostream>
using namespace std;
 
#define N 1001
int root[N];
 
//未进行路径压缩优化的情况下,运行超时 
int findroot(int x){
    if(root[x] == -1) return x;
    else{
        int tmp = findroot(root[x]);
        root[x] = tmp;//路径压缩
        return tmp;
         
    } 
}
 
int main(){
    int t,n,m,i,j;
    cin >> t;
    for(i = 0;i < t;i++){
         
        cin >> n >> m;
        for(j = 1;j <= n;j++) root[j] = -1;
          
        for(j = 0;j < m;j++){
             
            int f1,f2;
            cin >> f1 >> f2;
            int a = findroot(f1);
            int b = findroot(f2);
            if(a != b) root[a] = b;
        }
         
        int ans = 0;
        for(j = 1;j <= n;j++)
            if(root[j] == -1) ans++;
         
        cout << ans << endl; 
    }
     
    return 0;
}
 
/**************************************************************
    Problem: 1445
    User: cherish
    Language: C++
    Result: Accepted
    Time:10 ms
    Memory:1524 kb
****************************************************************/

总结:

求连通分量的个数和连通分量的节点个数的题使用并查集方法很简单,在使用并查集的过程中最好进行一下路径压缩,会大大减少查找根节点的时间。





  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值