zoukankan      html  css  js  c++  java
  • pat甲级1107

    1107 Social Clusters (30 分)

    When register on a social network, you are always asked to specify your hobbies in order to find some potential friends with the same hobbies. A social cluster is a set of people who have some of their hobbies in common. You are supposed to find all the clusters.

    Input Specification:

    Each input file contains one test case. For each test case, the first line contains a positive integer N (1000), the total number of people in a social network. Hence the people are numbered from 1 to N. Then N lines follow, each gives the hobby list of a person in the format:

    Ki​​: hi​​[1] hi​​[2] ... hi​​[Ki​​]

    where Ki​​ (>0) is the number of hobbies, and hi​​[j] is the index of the j-th hobby, which is an integer in [1, 1000].

    Output Specification:

    For each case, print in one line the total number of clusters in the network. Then in the second line, print the numbers of people in the clusters in non-increasing order. The numbers must be separated by exactly one space, and there must be no extra space at the end of the line.

    Sample Input:

    8
    3: 2 7 10
    1: 4
    2: 5 3
    1: 4
    1: 3
    1: 4
    4: 6 8 1 5
    1: 4
    

    Sample Output:

    3
    4 3 1

    这道题考察并查集。

    开始遇到了数组越界的问题,后来调试发现是最近考研复习计算机系统基础遇到的一个知识点:

    1     for (i = 1; i < 1001; i++)
    2     {
    3         for (j = 0; j < hobby[i].size() - 1; j++)
    4             _union(hobby[i][j], hobby[i][j + 1]);
    5     }

    我开始是这么写的,看似不会发生数组越界的问题,但是因为hobby[i].size()是无符号数unsigned型,当hobby[i].size()等于0时,减去1就成了无符号数2^32-1,所以发生了越界。

    看来了解底层的一些原理确实很重要,usigned型要慎用。

    #include <iostream>
    #include <vector>
    #include <algorithm>
    using namespace std;
    
    vector<vector<int>> hobby(1001);
    vector<int> father(1001, -1);
    void _union(int i, int j);
    int find(int i);
    
    int main()
    {
        int N;
        cin >> N;
        int i, j, k, t;
        char c;
        for (i = 1; i <= N; i++)
        {
            cin >> k >> c;
            for (j = 0; j < k; j++)
            {
                cin >> t;
                hobby[t].push_back(i);
            }
        }
        for (i = 1; i < 1001; i++)
        {
            for (j = 1; j < hobby[i].size(); j++)
                _union(hobby[i][j], hobby[i][j - 1]);
        }
        vector<int> cluster;
        for (i = 1; i <= N; i++)
        {
            if (father[i] < 0)
                cluster.push_back(-father[i]);
        }
        sort(cluster.begin(), cluster.end());
        cout << cluster.size() << endl << cluster[cluster.size() - 1];
        for (i = cluster.size() - 2; i >= 0; i--) cout << " " << cluster[i];
        return 0;
    }
    
    void _union(int i, int j)
    {
        int a = find(i);
        int b = find(j);
        if (a == b) return;
        if (father[a] < father[b])
        {
            father[a] += father[b];
            father[b] = a;
        }
        else
        {
            father[b] += father[a];
            father[a] = b;
        }
    }
    int find(int i)
    {
        while (father[i] > 0) i = father[i];
        return i;
    }
  • 相关阅读:
    气象数据集数据和相关源码
    农产品质量追溯系统/质量安全追溯系统
    Nutch2.x 演示抓取第一个网站
    Nutch的配置以及动态网站的抓取
    leach-matlab
    leach协议matlab仿真代码
    无线传感器网络数据融合技术
    无线传感器网络数据融合概述
    No pressure, no diamonds.
    Hibernate缓存应用的积累与总结
  • 原文地址:https://www.cnblogs.com/lxc1910/p/9690539.html
Copyright © 2011-2022 走看看