算法分析和复杂性理论 / 22年算法课上机题目合集Z:The Suspects【POJ-1611】

题目 The Suspects

描述

Severe acute respiratory syndrome (SARS), an atypical pneumonia of unknown aetiology, was recognized as a global threat in mid-March 2003. To minimize transmission to others, the best strategy is to separate the suspects from others.
In the Not-Spreading-Your-Sickness University (NSYSU), there are many student groups. Students in the same group intercommunicate with each other frequently, and a student may join several groups. To prevent the possible transmissions of SARS, the NSYSU collects the member lists of all student groups, and makes the following rule in their standard operation procedure (SOP).
Once a member in a group is a suspect, all members in the group are suspects.
However, they find that it is not easy to identify all the suspects when a student is recognized as a suspect. Your job is to write a program which finds all the suspects.

输入

The input file contains several cases. Each test case begins with two integers n and m in a line, where n is the number of students, and m is the number of groups. You may assume that 0 < n <= 30000 and 0 <= m <= 500. Every student is numbered by a unique integer between 0 and n−1, and initially student 0 is recognized as a suspect in all the cases. This line is followed by m member lists of the groups, one line per group. Each line begins with an integer k by itself representing the number of members in the group. Following the number of members, there are k integers representing the students in this group. All the integers in a line are separated by at least one space.
A case with n = 0 and m = 0 indicates the end of the input, and need not be processed.

输出

For each case, output the number of suspects in one line.

样例输入

100 4

2 1 2

5 10 13 11 12 14

2 0 1

2 99 2

200 2

1 5

5 1 2 3 4 5

1 0

0 0

样例输出

4

1

1

思路分析

    题目:和疑犯在一个组内的人都是疑犯,0号学生是疑犯,需要判断学生集合中的疑犯数量。

    输入:学生数n和组数m (0 < n <= 30000 and 0 <= m <= 500),接着给出这m组的信息,每组第一个值k是组内学生数,然后跟着组内的k个学生序号。需要判断多个学生集合,以0 0结束输入。

    解决方案:简单的遍历,从0号开始将每组含0号的其他学生都标记为嫌犯,再对新出现的嫌犯依次遍历,复杂度是n的n次方显然不可行。一个组内的学生要么都是疑犯要么都不是,如果两个组中包含了相同的学生则这个两个组的学生类型相同(要么都是疑犯要么都不是),因此判断学生是不是疑犯,只需要把每组内的学生看做一个集合,后面遍历的组如果学生已经在之前的集合,就把这个组内的学生都放到之前的集合中,如果新遍历的组中有学生s1和s2,s1在集合A,s2在集合B,则遍历后集合A和集合B将会合并为一个集合。因此这个问题涉及集合、合并集合、判断是否属于同一集合,因此想到并查集的解决方案。

    算法细节:需要创建如下变量①int arr[30005]用于保存并查集leader信息,初始化为全-1②int temp[30005]用于依次储存每个访问到的组信息③int n, m代表学生数和组数④int k代表组内学生数

对每个学生集合,先判断是否满足退出条件0 0,否则初始化arr数组,输入k和组中学生序号,依次合并所有组内学生(两两合并s1,s2;s2,s3;s3,s4),由于并查集的arr数组要么是普通对象指示leader索引,要么是leader指示集合元素数量的相反数,每个集合中最小元素是leader,因此输出-arr[0]即可找出疑犯数量。

过程中的错误

有两个改进点:

    1.C++中cin和cout比较慢,因此可以通过取消流同步来进一步提高代码运行速度,具体的说,在main函数的第一句添加:ios::sync_with_stdio(false);

    2.将i++改成++i。

针对编译器的优化

    很多时候我们有的代码用C++提交通过了,但是G++却失败了呢?众所周知,不同的编译器,会对代码做出一些不同的优化。举一个最简单的例子。针对单个语句a++和++a:

    从标准C的角度去理解。a++这个语句等同于a = a + 1,也就是说,是先调用,再自增。在调用过程中,会申请一个新的数据地址,用于存放临时的变量a',然后在把a'加1,之后在把a'赋值给a。

    但是++a这个语句不需要这么麻烦。因为他是先自增,后调用,也就是省去了申请新地址的功夫所以理论上,二者的时间消耗是有差异的,如果你是使用标准C的编译方式,就可以发现这个差异。毕竟,申请临时内存这个操作耗费的时间,远远比令已知内存的数据进行一个改变要长的多。

     但是编译器的优化就体现在了这种本身结果相同却耗时有差异的地方。如果你使用gcc来编译,结果你会发现前++与后++二者基本上没有差异。这就是编译器的优化中的冰山一角了。事实上还有很多优化的地方。

AC代码

#include <iostream>
using namespace std;

void Initial(int* arr, int n) {
    for (int i = 0; i < n; ++i) arr[i] = -1;
}

int Find(int* arr, int x) {
    int root = x;
    while (arr[root] >= 0) root = arr[root];
    while (x != root) {
        int t = arr[x];
        arr[x] = root;
        x = t;
    }
    return root;
}

void Union(int* arr, int x, int y) {
    int root1 = Find(arr, x);
    int root2 = Find(arr, y);
    if (root1 != root2) {
        if (root1 < root2) {
            arr[root1] += arr[root2];
            arr[root2] = root1;
        }
        else {
            arr[root2] += arr[root1];
            arr[root1] = root2;
        }
    }
}

int main() {
    ios::sync_with_stdio(false);
    int m = 0, n = 0, t = 0;
    int arr[30000] = { 0 }, temp[30000] = { 0 };
    while (cin >> m >> n) {
        int res = 0;
        if (m == 0 && n == 0) {
            break;
        }
        else {
            Initial(arr, m);
            for (int i = 0; i < n; ++i) {
                cin >> t;
                for (int j = 0; j < t; ++j) {
                    cin >> temp[j];
                }
                for (int j = 0; j < t - 1; ++j) {
                    if (Find(arr, temp[j]) != Find(arr, temp[j + 1])) {
                        Union(arr, temp[j], temp[j + 1]);
                    }
                }
            }
            cout << -arr[0] << endl;
        }
    }
    return 0;
}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值