您的位置:首页 > 编程语言 > C语言/C++

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答案正确301107C++
(g++ 4.7.2)
7512datrilla


测试点

测试点结果用时(ms)内存(kB)得分/满分
0答案正确437616/16
1答案正确43844/4
2答案正确43841/1
3答案正确53723/3
4答案正确75123/3
5答案正确65123/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++