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. c# mvc会话过期设置

    configuration 元素(常规设置架构)   system.web 元素(ASP.NET 设置架构)     sessionState 元素(ASP.NET 设置架构) <session ...

  2. http转https的各种应用

    http://www.lccee.com/content-57.html https://www.gworg.com/ssl/127.html apach: LoadModule socache_sh ...

  3. day_05 字典

    1. 字典 1.成对保存数据 ,以key:value形式保存 2.以{}表示,每项内容都是key:value,元素之间用逗号隔开 3.key是不可重复的 4.字典以hash算法来计算key的hash值 ...

  4. Sqlite和mysql的区别及优缺点

    小型嵌入式,跟mysql差不多,但更小,功能也少,属于本地数据库多用于读多写少,100W以下的数据压力不大,不支持分布式.   真正开源的小型嵌入式数据库,支持事务.触发器器  性能至少优于mysql ...

  5. java——红黑树 RBTree

    对于完全随机的数据,普通的二分搜索树就很好用,只是在极端情况下会退化成链表. 对于查询较多的情况,avl树很好用. 红黑树牺牲了平衡性,但是它的统计性能更优(综合增删改查所有的操作). 红黑树java ...

  6. vue中组件传值方式汇总

    在应用复杂时,推荐使用vue官网推荐的vuex,以下讨论简单SPA中的组件间传值. 一.路由传值 路由对象如下图所示: 在跳转页面的时候,在js代码中的操作如下,在标签中使用<router-li ...

  7. mysql 存储过程(代码块)

    大纲: 创建.删除.调用. 声明变量.变量赋值 声明游标 声明异常处理器 判断 循环 使用心得 一.创建.删除.调用 创建 DELIMITER $$ #修改分隔符 )) #括号里是入参.IN代表传入的 ...

  8. 数据结构---Java---HashSet

    1.概述 1.1 HashSet不是线程安全的: 1.2 当向HashSet存入元素时,调用该对象的hashCode()值,根据hashCode()值来决定元素的存储位置: 如果hashCode()值 ...

  9. Windows常用IDE下载(含安装教程)

    电脑办公 电脑系统(U盘安装) PE系统 WIN7 WIN8 WIN10 XP 电脑系统(直接安装) WIN7 WIN8 WIN10 Office office2003 office2007 offi ...

  10. DTCMS 网站管理系统

    dtcms适合开发一些简单小型网站 开发的过程中遇见了各种问题,下面总结下我遇见的问题 1.遇见提示未开启生成静态功能 在系统设置里面将“伪URL重写”为“生成静态”可以解决 2.在生成静态页面的时候 ...