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:

K​i​​: h​i​​[1] h​i​​[2] ... h​i​​[K​i​​]

where K​i​​ (>0) is the number of hobbies, and h​i​​[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

这道题考察并查集。

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

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

我开始是这么写的,看似不会发生数组越界的问题,但是因为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();
vector<int> father(, -);
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 = ; i <= N; i++)
{
cin >> k >> c;
for (j = ; j < k; j++)
{
cin >> t;
hobby[t].push_back(i);
}
}
for (i = ; i < ; i++)
{
for (j = ; j < hobby[i].size(); j++)
_union(hobby[i][j], hobby[i][j - ]);
}
vector<int> cluster;
for (i = ; i <= N; i++)
{
if (father[i] < )
cluster.push_back(-father[i]);
}
sort(cluster.begin(), cluster.end());
cout << cluster.size() << endl << cluster[cluster.size() - ];
for (i = cluster.size() - ; i >= ; i--) cout << " " << cluster[i];
return ;
} 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] > ) i = father[i];
return i;
}

pat甲级1107的更多相关文章

  1. PAT甲级1107. Social Clusters

    PAT甲级1107. Social Clusters 题意: 当在社交网络上注册时,您总是被要求指定您的爱好,以便找到一些具有相同兴趣的潜在朋友.一个"社会群体"是一群拥有一些共同 ...

  2. PAT甲级——1107 Social Clusters (并查集)

    本文同步发布在CSDN:https://blog.csdn.net/weixin_44385565/article/details/90409731 1107 Social Clusters (30  ...

  3. pat甲级 1107. Social Clusters (30)

    When register on a social network, you are always asked to specify your hobbies in order to find som ...

  4. PAT甲级题解(慢慢刷中)

    博主欢迎转载,但请给出本文链接,我尊重你,你尊重我,谢谢~http://www.cnblogs.com/chenxiwenruo/p/6102219.html特别不喜欢那些随便转载别人的原创文章又不给 ...

  5. PAT甲级考前整理(2019年3月备考)之一

       转载请注明出处:https://www.cnblogs.com/jlyg/p/7525244.html 终于在考前,刷完PAT甲级131道题目,不容易!!!每天沉迷在刷题之中而不能超脱,也是一种 ...

  6. 【转载】【PAT】PAT甲级题型分类整理

    最短路径 Emergency (25)-PAT甲级真题(Dijkstra算法) Public Bike Management (30)-PAT甲级真题(Dijkstra + DFS) Travel P ...

  7. PAT甲级1131. Subway Map

    PAT甲级1131. Subway Map 题意: 在大城市,地铁系统对访客总是看起来很复杂.给你一些感觉,下图显示了北京地铁的地图.现在你应该帮助人们掌握你的电脑技能!鉴于您的用户的起始位置,您的任 ...

  8. PAT甲级1127. ZigZagging on a Tree

    PAT甲级1127. ZigZagging on a Tree 题意: 假设二叉树中的所有键都是不同的正整数.一个唯一的二叉树可以通过给定的一对后序和顺序遍历序列来确定.这是一个简单的标准程序,可以按 ...

  9. PAT甲级1123. Is It a Complete AVL Tree

    PAT甲级1123. Is It a Complete AVL Tree 题意: 在AVL树中,任何节点的两个子树的高度最多有一个;如果在任何时候它们不同于一个,则重新平衡来恢复此属性.图1-4说明了 ...

随机推荐

  1. Codeforces Round #482 (Div. 2) B、Treasure Hunt(模拟+贪心)979B

    题目 大致题意 n表示要进行n次操作,接着给出三个字符串,表示三个人初始拥有的串.每次操作要替换字符串中的字母,询问最后在游戏中曾出现过的相同的子串谁最多. 思路 (1)  讨论最多的子串,肯定是全部 ...

  2. python进制转换或数据格式转换

    以下是部分进制转换的,都是python内置函数 int(x [,base ])         将x转换为一个整数    long(x [,base ])        将x转换为一个长整数    f ...

  3. GM TECH2 Scanner Clone

    Professional Diagnostic Tools gm tech 2 scanner china with multi-languages, TIS2000 Programming CD, ...

  4. python 时间序列resample参数

  5. #1413 : Rikka with String 后缀自动机 + 二级差分

    http://hihocoder.com/problemset/problem/1413?sid=1199641 这题断断续续做了2个多星期吧,一直不会 设总答案为sum,替换后新加的子串数量为x,失 ...

  6. python3+Appium自动化12-H5元素定位环境搭建

    前言 在混合开发的App中,经常会有内嵌的H5页面.那么这些H5页面元素该如何进行定位操作呢? 针对这种场景直接使用前面所讲的方法来进行定位是行不通的,因为前面的都是基于Andriod原生控件进行元素 ...

  7. 转:99%的人都理解错了HTTP中GET与POST的区别

    原贴来自:http://www.techweb.com.cn/network/system/2016-10-11/2407736.shtml GET和POST是HTTP请求的两种基本方法,要说它们的区 ...

  8. tinkphp3.2.3 关于事务处理。

    自己做一个测试,关于事务处理的. 在对多表进行操作的时候 基本上都离不开事务. 有的操作,是要由上一操作后,产的值(如主表里插入后,要获取插入的主键ID值,返回给下面处理表用.)带到后面的表处理当中去 ...

  9. pta5-9 Huffman Codes (30分)

    5-9 Huffman Codes   (30分) In 1953, David A. Huffman published his paper "A Method for the Const ...

  10. c# 读取图片文件

    /// <summary> /// 通过FileStream 来打开文件,这样就可以实现不锁定Image文件,到时可以让多用户同时访问Image文件 /// </summary> ...