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
这道题考察并查集。
开始遇到了数组越界的问题,后来调试发现是最近考研复习计算机系统基础遇到的一个知识点:
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的更多相关文章
- PAT甲级1107. Social Clusters
PAT甲级1107. Social Clusters 题意: 当在社交网络上注册时,您总是被要求指定您的爱好,以便找到一些具有相同兴趣的潜在朋友.一个"社会群体"是一群拥有一些共同 ...
- PAT甲级——1107 Social Clusters (并查集)
本文同步发布在CSDN:https://blog.csdn.net/weixin_44385565/article/details/90409731 1107 Social Clusters (30 ...
- pat甲级 1107. Social Clusters (30)
When register on a social network, you are always asked to specify your hobbies in order to find som ...
- PAT甲级题解(慢慢刷中)
博主欢迎转载,但请给出本文链接,我尊重你,你尊重我,谢谢~http://www.cnblogs.com/chenxiwenruo/p/6102219.html特别不喜欢那些随便转载别人的原创文章又不给 ...
- PAT甲级考前整理(2019年3月备考)之一
转载请注明出处:https://www.cnblogs.com/jlyg/p/7525244.html 终于在考前,刷完PAT甲级131道题目,不容易!!!每天沉迷在刷题之中而不能超脱,也是一种 ...
- 【转载】【PAT】PAT甲级题型分类整理
最短路径 Emergency (25)-PAT甲级真题(Dijkstra算法) Public Bike Management (30)-PAT甲级真题(Dijkstra + DFS) Travel P ...
- PAT甲级1131. Subway Map
PAT甲级1131. Subway Map 题意: 在大城市,地铁系统对访客总是看起来很复杂.给你一些感觉,下图显示了北京地铁的地图.现在你应该帮助人们掌握你的电脑技能!鉴于您的用户的起始位置,您的任 ...
- PAT甲级1127. ZigZagging on a Tree
PAT甲级1127. ZigZagging on a Tree 题意: 假设二叉树中的所有键都是不同的正整数.一个唯一的二叉树可以通过给定的一对后序和顺序遍历序列来确定.这是一个简单的标准程序,可以按 ...
- PAT甲级1123. Is It a Complete AVL Tree
PAT甲级1123. Is It a Complete AVL Tree 题意: 在AVL树中,任何节点的两个子树的高度最多有一个;如果在任何时候它们不同于一个,则重新平衡来恢复此属性.图1-4说明了 ...
随机推荐
- mc03_IntelliJ IDEA配置github
配置本地git仓库 首先配置一个本地的git仓库,熟悉一下git上传文件到github的过程,具体操作参考 mc02_配置本地git仓库并上传到github IntelliJ IDEA与github的 ...
- Nuxt 2.3.X 配置babel
1. 在package.json中修改运行脚本 添加--exec babel-node 添加之后的效果为:(修改了8/10行) { "name": "nuxt-learn ...
- grunt 安装使用(一)
grunt 依赖nodejs,所有在使用前确保你安装了nodejs,然后开始执行grunt命令. .安装node nodejs安装教程 安装完成后在命令行,执行命令: node -v 出现版本信息, ...
- 吞吐率(Requests per second),缩写RPS
计算公式: 吞吐率 = 总请求数 / 处理这些请求的总完成时间 Requests per second = Complete requests / Time taken for tests 吞 ...
- jndi理解
java中很多这些接口规范,jndi就是其中一个,而下面那些包就是jndi接口的提供商程序实现包,他们都是遵循jndi规范的. 主要接口功能是:添加命名与对象的映射到jndi树中,客户能快速查找并使用 ...
- 树形dp学习
学习博客:https://www.cnblogs.com/qq936584671/p/10274268.html 树的性质:n个点,n-1条边,任意两个点之间只存在一条路径,可以人为设置根节点,对于任 ...
- CAD安装失败怎样卸载CAD 2017?错误提示某些产品无法安装
AUTODESK系列软件着实令人头疼,安装失败之后不能完全卸载!!!(比如maya,cad,3dsmax等).有时手动删除注册表重装之后还是会出现各种问题,每个版本的C++Runtime和.NET f ...
- 性能测试工具LoadRunner10-LR之Virtual User Generator 错误处理函数
VuGen提供了错误处理函数lr_continue_on_error,用来在脚本中实时修改Vuser的出错设置.lr_continue_on_error函数语法结构如下: void lr_contin ...
- PHP会话管理
Session使用 在每个页面中使用session之前,必须使用session_start() 在每个session中都可以使用$_SESSION这个全局数组,在页面必须调用session_start ...
- DBCP数据连接池
package com.itheima.utils; import java.io.InputStream; import java.sql.Connection; import java.sql.R ...