1107. Social Clusters (30)
来源:互联网 发布:网络直播平台流量计算 编辑:程序博客网 时间:2024/06/15 17:48
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:83: 2 7 101: 42: 5 31: 41: 31: 44: 6 8 1 51: 4Sample Output:
34 3 1
N(一共有的注册用户数)
Ki(后面有ki个爱好对应编号): h1……hki
……
求这些注册用户可能是朋友的朋友圈个数;【用户id:1~N;爱好编号1~1000】
比如
评测结果
测试点
#include<iostream>#include<vector> #include<queue>#include<algorithm>using namespace std;void readln(int *N, vector<vector<int > >*people,vector<vector<int > >*hobby){ int maxHobby=0,i; cin>>(*N); (*people).resize((*N)); for(i=0;i<(*N);i++) { int num,_hobby; cin>>num; getchar(); getchar(); while(num--) { cin>>_hobby; (*people)[i].push_back(_hobby-1); maxHobby=maxHobby>_hobby?maxHobby:_hobby; } } (*hobby).resize(maxHobby); for(i=0;i<(*N);i++) { for(vector<int> ::iterator it=(*people)[i].begin();it!=(*people)[i].end();it++) { (*hobby)[(*it)].push_back(i); } }}void bfs_set(int *N, vector<vector<int > >*people,vector<vector<int > >*hobby,vector<int>*ansNum){ vector<bool >flag((*people).size(),true); vector<bool >flag_hobby((*hobby).size(),true); for(int id=0;id<(*N);id++) { if(flag[id]) { flag[id]=false; int count=1; queue<int>qt; qt.push(id); while(!qt.empty()) { queue<int>que; for(vector<int >::iterator pt=(*people)[qt.front()].begin();pt!=(*people)[qt.front()].end();pt++) { if(flag_hobby[(*pt)]) { flag_hobby[(*pt)]=false; que.push((*pt)); } } while(!que.empty()) { for(vector<int >::iterator ht=(*hobby)[que.front()].begin();ht!=(*hobby)[que.front()].end();ht++) { if(flag[(*ht)]) { flag[(*ht)]=false; count++; qt.push((*ht)); } } que.pop(); } qt.pop(); } (*ansNum).push_back(count); } }}void writeln(vector<int >*ansNum){ cout<<(*ansNum).size()<<endl; for(vector<int >::iterator iter=(*ansNum).begin();iter!=(*ansNum).end();iter++) { if(iter!=(*ansNum).begin()) cout<<" "<<(*iter); else cout<<(*iter); }}bool cmpAns(const int &a,const int &b){ return a>b;}int main(){ int N; vector<vector<int > >people; vector<vector<int > >hobby; readln(&N, &people,&hobby); vector<int>ansNum; bfs_set(&N, &people,&hobby,&ansNum); sort(ansNum.begin(),ansNum.end(),cmpAns); writeln(&ansNum); return 0;}
- 1107. Social Clusters (30)
- 1107. Social Clusters (30)
- 1107. Social Clusters (30)
- 1107. Social Clusters (30)
- 1107. Social Clusters (30)
- 1107. Social Clusters (30)
- 1107. Social Clusters (30)
- 1107. Social Clusters (30)
- 1107. Social Clusters (30)
- 1107. Social Clusters (30)
- 1107. Social Clusters (30)
- 1107. Social Clusters (30)
- 1107. Social Clusters (30)
- 1107. Social Clusters (30)
- 1107. Social Clusters (30)
- 1107. Social Clusters (30)
- 1107. Social Clusters (30)
- 【PAT】1107. Social Clusters (30)
- 简易网站流量统计工具
- tomcat用户登录问题
- NYOJ-另一种阶乘问题
- bq24250/1/3 无法实现大电流充电[2A]的几点检查注意事项
- BOSH 学习笔记
- 1107. Social Clusters (30)
- JAVA并发编程(三)设计线程安全的类
- Restful
- 字符串占位符的三种解决方案及其时间性能对比
- 自己动手实现一个守护进程,当控制台窗口关闭时还可以在后台运行。每隔一秒钟向my.log文件中插入一条记录
- Linux用户抢占和内核抢占详解(概念, 实现和触发时机)--Linux进程的管理与调度(二十)
- iOS中自定义输入文本框的cell(UITextFieldCell)的使用技巧
- Fresco图片加载(三)
- 关于android stuidio的基本设置