您的位置:首页 > 其它

pat 1107. Social Clusters (30)

2017-02-17 21:56 459 查看
https://www.patest.cn/contests/pat-a-practise/1107

参考了:

http://www.itdadao.com/articles/c15a875956p0.html

这道题我觉得有点歧异

如果

3个人

分别喜欢

1 2

2 4

4 6

算几个圈子,我觉得是两个(1,2),和(2,3),因为1和3没有共同的爱好。

但是pat的过的例子任何是一个圈子(1,2,3) , 1和3可以通过2联系起来。

#include <stdio.h>
#include <cstring>
#include <algorithm>
int cmp(int a, int b) {
return a > b;
}

int f[1001];
int like[1001];
int find(int x) {
return x == f[x] ? x : f[x] = find(f[x]);
}

int main() {
int n,k,a;

memset(like,0,sizeof(like));
scanf("%d",&n);
for (size_t i = 1; i <=n; i++)
{
f[i] = i;
}

for (size_t i = 1; i <= n; i++)
{
scanf("%d:", &k);
for (size_t j = 0; j < k; j++)
{
scanf("%d", &a);
if (like[a] == 0) like[a] = i;
if (find(i) != find(like[a])) f[find(i)] = find(like[a]);
}
}
int count = 0, ans[1001];
memset(ans,0,sizeof(ans));
for (size_t i = 1; i <= n; i++)
{

int father = find(i);
if (i == father) count++;
ans[father]++;
}
std::sort(ans , ans+n+1, cmp);
printf("%d\n",count);
for (size_t i = 0; i <count; i++)
{
printf("%d%c", ans[i], i == count-1 ? '\n' : ' ');
}

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: