PAT_A1107#Social Clusters
Source:
Description:
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 (≤), 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 (>) is the number of hobbies, and [ 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
Keys:
Code:
/*
time: 2019-06-23 14:07:12
problem: PAT_A1107#Social Clusters
AC: 34:25 题目大意:
把一群具有相同爱好的人归为一个社交圈,找出所有的社交圈
输入:
第一行给出,总人数N<=1e3,编号从1~N
接下来N行,给出第i个人的,爱好总数K,各个爱好
输出:
第一行给出,社交圈总数
第二行给出,各个社交圈的人数,从多到少 基本思路:
基于兴趣做并查集操作,
输入每个人的兴趣,首个兴趣的Hash值+1,标记人数
统计父结点个数及其孩子的哈希值即可
*/
#include<cstdio>
#include<set>
#include<algorithm>
using namespace std;
const int M=1e3+;
int fa[M],man[M]={},ans[M]={}; int Father(int v)
{
int x=v,s;
while(fa[v] != v)
v = fa[v];
while(fa[x] != x){
s = fa[x];
fa[x] = v;
x = s;
}
return v;
} void Union(int v1, int v2)
{
int f1 = Father(v1);
int f2 = Father(v2);
fa[f2] = f1;
Father(v2);
} int main()
{
#ifdef ONLINE_JUDGE
#else
freopen("Test.txt", "r", stdin);
#endif // ONLINE_JUDGE for(int i=; i<M; i++)
fa[i]=i; int n,m,h1,h2;
set<int> hobby,clster;
scanf("%d", &n);
for(int i=; i<n; i++)
{
scanf("%d:%d", &m,&h1);
man[h1]++;
hobby.insert(h1);
for(int j=; j<m; j++)
{
scanf("%d", &h2);
hobby.insert(h2);
Union(h1,h2);
h1=h2;
}
}
for(auto it=hobby.begin(); it!=hobby.end(); it++){
ans[Father(*it)] += man[*it];
clster.insert(Father(*it));
}
printf("%d\n", clster.size());
sort(ans, ans+M, greater<int>() );
for(int i=; i<clster.size(); i++)
printf("%d%c", ans[i], i+==clster.size()?'\n':' '); return ;
}
PAT_A1107#Social Clusters的更多相关文章
- PAT1107:Social Clusters
1107. Social Clusters (30) 时间限制 1000 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue When ...
- [并查集] 1107. Social Clusters (30)
1107. Social Clusters (30) When register on a social network, you are always asked to specify your h ...
- 1107 Social Clusters[并查集][难]
1107 Social Clusters(30 分) When register on a social network, you are always asked to specify your h ...
- 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 分) 并查集模板
1107 Social Clusters (30 分) When register on a social network, you are always asked to specify your ...
- 1107 Social Clusters——PAT甲级真题
1107 Social Clusters When register on a social network, you are always asked to specify your hobbies ...
- 1107. Social Clusters (30)
When register on a social network, you are always asked to specify your hobbies in order to find som ...
- A1107. Social Clusters
When register on a social network, you are always asked to specify your hobbies in order to find som ...
随机推荐
- [转] CV Datasets on the web
转自:CVPapers This material is presented to ensure timely dissemination of scholarly and technical wor ...
- mac 浏览器解决跨域问题
Chrome:命令行执行如下命令open -a Google\ Chrome --args --disable-web-security出现如下提示,说明已经开启: Safari: open -a ' ...
- react 使用
我的有道云笔记 React 事件: 1.不能使用 return false; 来阻止元素的默认行为.需要在方法的最前面使用 e.preventDefault() 来阻止元素的默认行为(例如:a 标签的 ...
- 【Ubuntu】无法挂载磁盘
我的电脑分了三个分区,A,B,C,其中A和B是Windows盘,C是ubuntu系统盘 某日发现A ,B盘没法进入了,在文件管理器中点一下,没有反应.于是右击盘符,点击挂载,跳出错误信息: (划重点) ...
- 从新浪微博和MySQL的password保护机制谈HTTPS/SSL的必要性
尽管业界已经达成共识,在传输用户password等须要保密的信息时,尽可能採用HTTPS/SSL协议传输. 但我们还是能够看到少数没实用HTTPS/SSL加密的站点或应用. 新浪微博的登录页面和MyS ...
- 大文件Copy 大数据量复制
private void button3_Click(object sender, EventArgs e) { Thread thread ...
- P2597 [ZJOI2012]灾难 拓扑排序
这个题有点意思,正常写法肯定会T,然后需要优化.先用拓扑排序重构一遍树,然后进行一个非常神奇的操作:把每个点放在他的食物的lca上,然后计算的时候直接dfs全加上就行了.为什么呢,因为假如你的食物的l ...
- bzoj4082
贪心+倍增 首先如果这个问题在序列上,好像可以按右端点排序,然后从起点开始向能到的最远的地方走. 但是环上不可以,因为随即一个起点可能不是最小的. 然后神思路来了:我们先将环展开倍增,再将区间按右端点 ...
- JavaGraphics类的绘图方法
Graphics类提供基本绘图方法,Graphics类提供基本的几何图形绘制方法,主要有:画线段.画矩形.画圆.画带颜色的图形.画椭圆.画圆弧.画多边形.画字符串等. 1. 画线段:在窗口中画一条线段 ...
- 对RDD分区的理解
举个例子: val logFile = "file:///home/soyo/桌面/6.txt" val conf = new SparkConf().setAppName(&qu ...