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. 及时从数据库中取得数据填放进Form表单的多选框中

    #写上以下代码就不用担心数据库添加了数据而不能及时获取了 def __init__(self, *args, **kwargs): #每次创建Form1对象时执行init方法 super(Form1, ...

  2. hxml总结

    段落<p> br 换行 hr 分割线 &nbsp 空格 &gt  > &lt < &  &amp h        7级标题 <i ...

  3. SpringBoot2.0.3整合Quartz2.3.0实现定时任务

    转载:https://www.cnblogs.com/ealenxie/p/9134602.html 关于别人写的quartz学习的地址:https://blog.csdn.net/lkl_csdn/ ...

  4. hive取等分数据

    %sql select t3.* from ( select t2.* ,row_number() over(partition by t2.pt order by t2.pv) as rn2 fro ...

  5. 23-----BBS论坛

    BBS论坛(二十三) 23.添加板块 (1)apps/models class BoardModel(db.Model): __tablename__ = 'board' id = db.Column ...

  6. mysql初始化

    注意:--install前,必须用mysql启动命令的绝对路径 # 制作MySQL的Windows服务,在终端执行此命令: mysqld --install # 移除MySQL的Windows服务,在 ...

  7. VBS 学习

    VBS其他功能 获取系统用户名 DimWshNetwork Set WshNetwork =CreateObject("WScript.Network") strTaccount ...

  8. auto uninstaller 简体中文版 更新下载地址

    地址一(腾讯微云) 地址二(百度网盘)    提取码:3nx7 地址三(直接下载)

  9. [转]25个HTML5和JavaScript游戏引擎库

    本文转自:http://www.open-open.com/news/view/27c6ed 1. The GMP JavaScript Game Engine GMP是一个基于精灵2-D游戏,它可以 ...

  10. 服务间调用--feign跟ribbon

    微服务一般来说当然是多服务多实例的,那么这些服务之间如何相互调用呢?spring cloud之前我们用dubbo,把服务开放出来,在xml配好后就可以像调用本地service一样调用其它模块的服务了: ...