1107. Social Clusters (30)
2016-07-10 18:29
381 查看
1107. Social Clusters (30)
时间限制1000 ms
内存限制
65536 kB
代码长度限制
16000 B
判题程序
Standard
作者
CHEN, Yue
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
N(一共有的注册用户数)
Ki(后面有ki个爱好对应编号): h1……hki
……
求这些注册用户可能是朋友的朋友圈个数;【用户id:1~N;爱好编号1~1000】
比如
【3】2:5 3
【5】1:3
【7】4:6 8 1 5
【3】【5】有共同爱好3,存在可能,【3】【7】有共同爱好5,存在可能;座椅【3】【5】【7】一个圈
我用的bfs,有的用并查集的(想到过并查集,但是很久没做了,又失之东隅)
评测结果
时间 | 结果 | 得分 | 题目 | 语言 | 用时(ms) | 内存(kB) | 用户 |
---|---|---|---|---|---|---|---|
7月10日 16:06 | 答案正确 | 30 | 1107 | C++ (g++ 4.7.2) | 7 | 512 | datrilla |
测试点
测试点 | 结果 | 用时(ms) | 内存(kB) | 得分/满分 |
---|---|---|---|---|
0 | 答案正确 | 4 | 376 | 16/16 |
1 | 答案正确 | 4 | 384 | 4/4 |
2 | 答案正确 | 4 | 384 | 1/1 |
3 | 答案正确 | 5 | 372 | 3/3 |
4 | 答案正确 | 7 | 512 | 3/3 |
5 | 答案正确 | 6 | 512 | 3/3 |
#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; }
相关文章推荐
- 使用C++实现JNI接口需要注意的事项
- 关于指针的一些事情
- c++ primer 第五版 笔记前言
- share_ptr的几个注意点
- Lua中调用C++函数示例
- Lua教程(一):在C++中嵌入Lua脚本
- Lua教程(二):C++和Lua相互传递数据示例
- C++联合体转换成C#结构的实现方法
- C++高级程序员成长之路
- C++编写简单的打靶游戏
- C++ 自定义控件的移植问题
- C++变位词问题分析
- C/C++数据对齐详细解析
- C++基于栈实现铁轨问题
- C++中引用的使用总结
- 使用Lua来扩展C++程序的方法
- C++中调用Lua函数实例
- Lua和C++的通信流程代码实例
- C++的template模板中class与typename关键字的区别分析
- C与C++之间相互调用实例方法讲解