第十八天:并查集应用

T1

畅通工程
时间限制:1 秒
内存限制:128 兆
特殊判题:否

题目描述: 某省调查城镇交通状况, 得到现有城镇道路统计表, 表中列出了每条道路直 接连通的城镇。 省政府“畅通工程”的目标是使全省任何两个城镇间都可以实现交通(但不一定有直接的道路相连,只要互相间接通过道路可达即可) 。问最少还需要建设多少条道路? 输入: 测试输入包含若干测试用例。 每个测试用例的第 1 行给出两个正整数, 分别是城镇数目 N ( < 1000 )和道路数目 M;随后的 M 行对应 M 条道路,每行给出一对正整数,分别是该条道路直接连通的两个城镇的编号。为简单起见,城镇从1 到 N 编号。当 N 为 0 时,输入结束,该用例不被处理。

输入: 测试输入包含若干测试用例。 每个测试用例的第 1 行给出两个正整数, 分别是城镇数目 N ( < 1000 )和道路数目 M;随后的 M 行对应 M 条道路,每行给出一对正整数,分别是该条道路直接连通的两个城镇的编号。为简单起见,城镇从1 到 N 编号。当 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

//
//  main.cpp
//  ConstructRoad
//
//  Created by Apple on 2019/8/22.
//  Copyright © 2019 Apple_Lance. All rights reserved.
//

#include <iostream>
#include <stdio.h>
using namespace std;
#define N 1000

int Tree[N];

int findRoot(int x){
    if(Tree[x] == -1) return x;
    else{
        int tmp = findRoot(Tree[x]);
        Tree[x] = tmp;
        return tmp;
    }
}

int main(int argc, const char * argv[]) {
    // insert code here...
    int m, n;
    while(scanf("%d", &n) != EOF && n != 0){
        scanf("%d", &m);
        for(int i = 0;i<n;i++)
            Tree[i] = -1;
        while(m-- != 0){
            int a, b;
            scanf("%d%d", &a, &b);
            a = findRoot(a);
            b = findRoot(b);
            if(a != b)
                Tree[a] =  b;
        }
        int ans = 0;
        for(int i = 0;i<n;i++)
            if(Tree[i] == -1)
                ans ++;
        printf("%d", ans - 1);
    }
    return 0;
}

T2

More is better
时间限制:1 秒
内存限制:100 兆
特殊判题:否

题目描述: 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

//
//  main.cpp
//  MoreIsBetter
//
//  Created by Apple on 2019/8/22.
//  Copyright © 2019 Apple_Lance. All rights reserved.
//

#include <iostream>
#include <stdio.h>
using namespace std;

#define N 1000001

int Tree[N];
int Sum[N];

int findRoot(int x){
    if(Tree[x] == -1)
        return x;
    else{
        int tmp = findRoot(Tree[x]);
        Tree[x] = tmp;
        return tmp;
    }
}

int main(int argc, const char * argv[]) {
    // insert code here...
    int n;
    while(scanf("%d", &n) != EOF){
        for(int i = 0;i<N;i++){
            Tree[i] = -1;
            Sum[i] = 1;
        }
        int ans = 1;
        while(n--){
            int a, b;
            scanf("%d%d", &a, &b);
            a = findRoot(a);
            b = findRoot(b);
//            printf("$ %d $ %d $", a, b);
            if(a != b){
                Tree[a] = b;
                Sum[b] += Sum[a];
            }
            
        }
        for(int i = 0;i<N;i++){
            if(Tree[i] == -1 && Sum[i] > ans)
                ans = Sum[i];
        }
        printf("%d", ans);
    }
    return 0;
}
发布了182 篇原创文章 · 获赞 101 · 访问量 20万+

猜你喜欢

转载自blog.csdn.net/lancecrazy/article/details/100020863