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联系起来。
参考了:
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' : ' '); } }
相关文章推荐
- PAT (Advanced Level) Practise 1107 Social Clusters (30)
- PAT (Advanced Level) 1107. Social Clusters (30)
- 【PAT】【Advanced Level】1107. Social Clusters (30)
- PAT (Advanced Level)1107. Social Clusters (30)并查集
- PAT(A) - 1107. Social Clusters (30)
- PAT甲题题解-1107. Social Clusters (30)-PAT甲级真题(并查集)
- PAT (Advanced Level) Practise 1107 Social Clusters (30)
- PAT 1107. Social Clusters (30)
- PAT - 甲级 - 1107. Social Clusters (30) (并查集)
- Pat(A) 1107. Social Clusters (30)
- PAT (Advanced Level) Practise 1107. Social Clusters (30) 并查集
- PAT - 1107. Social Clusters (30) 并查集
- PAT甲组 1107. Social Clusters (30)
- PAT 1107. Social Clusters (30) 并查集
- PAT 甲级 1107. Social Clusters (30)
- 【PAT】1107. Social Clusters (30)
- PAT 1107. Social Clusters (30) 特殊问题+并查集+数量信息并查集
- PAT_A 1107. Social Clusters (30)
- pat-a 1107. Social Clusters (30)
- PAT 1107. Social Clusters (30)