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说明了 ...
随机推荐
- 关于c++中const的基本用法
c++中的const 有点类似于c里的宏定义#define,但是似乎是在宏定义基础上的代码优化,具体我解释不清,下面主要提到的是 const 在c++中的3中基本用法: 1.指向常量的指针 例如:co ...
- UESTC 1437
LCA模板题 随便找的倍增模板... #include<bits/stdc++.h> using namespace std; const int maxn = 1e5+11; int t ...
- pandas学习3(数据处理)
- Go语言基础之4--流程控制
一.if else语句 1.1 基本语法1 语法1: if condition { //do something } 语法2: if condition { //do something } else ...
- sqlserver 数据库阻塞和死锁
参考原文:http://blog.csdn.net/ha196200/article/details/44985597 (1) 数据库阻塞: 假设第一个连接T1占有且没有释放资源,第二个连接T2请求同 ...
- 4 练习: 使用eclipse开发
1 练习: 使用eclipse开发 1.1 练习目标 本例讲述在使用eclipse如何创建groovy程序. 1.2 创建new Groovy project 本例假设你已经安装好了g ...
- Microsoft使用技巧
1.拍摄屏幕内容的截图 按 Win + Shift + S 以打开截图栏,然后将光标拖动到要捕获的区域. 截图区域将保存到剪贴板. 2.使用键盘添加表情符号 随心随处表达自我. 按 Ctrl + Sh ...
- Mybatis学习笔记13 - 动态sql之set标签
示例代码: 接口定义: package com.mybatis.dao; import com.mybatis.bean.Employee; public interface EmployeeMapp ...
- If you are tired...
如果你累了 1. 深呼吸 放松身体,深呼吸五分钟. 2. 听音乐 静静地听几首歌放松一下就好了,比如王豪学长推荐的追梦赤子心,骄傲的少年. 3. 冥想 放松身体,处于冥想状态. 4. 干洗脸.鸣天鼓. ...
- 输入一个正整数n (1<n<=10),生成 1个 n*n 方阵 求出对角线之和
#define _CRT_SECURE_NO_WARNINGS #include <Windows.h> #include <stdio.h> #include <std ...