1107 Social Clusters(30 分)
作者: CHEN, Yue
单位: 浙江大学
时间限制: 1200 ms
内存限制: 64 MB
代码长度限制: 16 KB
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
这道题大概意思是给出每个人的喜好,然后根据喜好形成不同的社会圈子,最后输出圈子的个数以及每个圈子多少人(按照从高到底排列)。
一开始的想法比较直白,就是用vector存储所有人的爱好,然后每接受第 i 个人的爱好后,对比第 i 号和 1 ~ i - 1 号有没有相同的兴趣,如果有重复的兴趣则将两人的集合union。然后遍历所有人,用一个int 型数组root来计算每个圈子有多少人。然后对圈子按照人数排序,计算有多少个圈子,然后输出排序结果。
其实在对爱好进行圈子划分的时候不需要这么麻烦,只需要一个数组来记录第j个爱好是否有人喜欢,如果没有就将第一个喜欢的人的编号放入,如果有就让当前人和记录的人划为同一个圈子即可,这样可以节省代码量和少量的时间。
原AC代码:
#include <cstdio>
#include <algorithm>
#include <vector>
using namespace std;
const int maxn = 1010;
int father[maxn] = {0}, root[maxn] = {0};
vector<int> hobby[maxn];
bool cmp(int, int);
int findFather(int);
int main() {
int n;
scanf("%d", &n);
for(int i = 1; i <= n; ++i) {
int k;
scanf("%d:", &k);
for(int j = 0; j < k; ++j) {
int h;
scanf("%d", &h);
hobby[i].push_back(h);
}
for(int j = 1; j < i; ++j) {
int faa = findFather(j);
int fab = findFather(i);
if(faa != fab) {
bool same = false;
for(int x = 0; x < hobby[i].size(); ++x) {
for(int y = 0; y < hobby[j].size(); ++y) {
if(hobby[i][x] == hobby[j][y]) {
father[fab] = faa;
same = true;
break;
}
}
if(same) {
break;
}
}
}
}
}
for(int i = 1; i <= n; ++i) {
++root[findFather(i)];
}
sort(root, root + maxn, cmp);
int noz;
for(noz = 0; noz < n; ++noz) {
if(root[noz] == 0){
break;
}
}
printf("%d\n", noz);
for(int i = 0; i < noz; ++i) {
if(i != 0) {
printf(" ");
}
printf("%d", root[i]);
}
return 0;
}
int findFather(int x) {
int a = x;
while(father[x] != 0) {
x = father[x];
}
while(father[a] != 0) {
int b = a;
a = father[a];
father[b] = x;
}
return x;
}
bool cmp(int a, int b) {
return a > b;
}
更新后AC代码:
#include <cstdio>
#include <algorithm>
using namespace std;
const int maxn = 1010;
int hobby[maxn], father[maxn], root[maxn];
void unionSet(int, int);
int findFather(int);
bool cmp(int, int);
int main() {
int n;
scanf("%d", &n);
for(int i = 0; i < maxn; ++i) {
father[i] = i;
}
for(int i = 1; i <= n; ++i) {
int k;
scanf("%d:", &k);
for(int j = 0; j < k; ++j) {
int t;
scanf("%d", &t);
if(hobby[t] == 0) {
hobby[t] = i;
} else {
unionSet(i, hobby[t]);
}
}
}
for(int i = 1; i <= n; ++i) {
++root[findFather(i)];
}
sort(root, root + maxn, cmp);
int x;
for(x = 0; x < maxn; ++x) {
if(root[x] == 0) {
break;
}
}
printf("%d\n", x);
for(int i = 0; i < x; ++i) {
if(i != 0) {
printf(" ");
}
printf("%d", root[i]);
}
return 0;
}
int findFather(int x) {
int a = x;
while(x != father[x]) {
x = father[x];
}
while(a != father[a]) {
int b = a;
a = father[a];
father[b] = x;
}
return x;
}
void unionSet(int a, int b) {
int faa = findFather(a);
int fab = findFather(b);
if(faa != fab) {
father[faa] = fab;
}
}
bool cmp(int a, int b) {
return a > b;
}
如有错误,欢迎指摘。