1107. Social Clusters (30)
来源:互联网 发布:德军总部剧情知乎 编辑:程序博客网 时间:2024/06/05 18:13
题目详情:https://www.patest.cn/contests/pat-a-practise/1107
提交:
提交代码:
#include <iostream>#include <vector>#include <algorithm>using namespace std;#define N 1010int hobby[N],likePerson[N];int n,isRoot[N];int findFather( int key ){ while( key != hobby[key] ) key = hobby[key]; return key;}void Union( int a,int b ){ a = findFather(a); b = findFather(b); if( a != b ) hobby[a] = b;}void Init(){ for( int i=1;i<=n;++i ) { hobby[i] = i; isRoot[i] = 0; }}int main (){ cin>>n; Init(); for( int i=1;i<=n;++i ) { int h,num; scanf("%d:",&num); for( int j=1;j<=num;++j ) { cin>>h; if( likePerson[h] == 0 ) likePerson[h] = i; Union(i,likePerson[h]); } } for( int i=1;i<=n;++i ) { ++isRoot[findFather(i)]; } int ans = 0; for( int i=1;i<=n;++i ) { if( isRoot[i] != 0 ) ++ans; } cout<<ans<<endl; sort(isRoot+1,isRoot+n+1); //从小到大 for( int i=n;i>n-ans;--i ) { if( i == n-ans+1 ) cout<<isRoot[i]<<endl; else cout<<isRoot[i]<<" "; } return 0;}
我没想出来,看的算法笔记,功力还是太浅了。加油吧!
0 0
- 1107. Social Clusters (30)
- 1107. Social Clusters (30)
- 1107. Social Clusters (30)
- 1107. Social Clusters (30)
- 1107. Social Clusters (30)
- 1107. Social Clusters (30)
- 1107. Social Clusters (30)
- 1107. Social Clusters (30)
- 1107. Social Clusters (30)
- 1107. Social Clusters (30)
- 1107. Social Clusters (30)
- 1107. Social Clusters (30)
- 1107. Social Clusters (30)
- 1107. Social Clusters (30)
- 1107. Social Clusters (30)
- 1107. Social Clusters (30)
- 1107. Social Clusters (30)
- 【PAT】1107. Social Clusters (30)
- 用程序员思维理解西游记团队,哪个角色最容易被淘汰?
- 嵌入式Linux如何加快物联网方案的开发速度
- Mysql 插入 Emoji表情报错 Incorrect string value: '\xF0\x9F...' for column 'XXX' at row 1
- 如何搭建appium+Python的自动化测试环境
- VMware Workstation安装CentOS7,解决联网问题
- 1107. Social Clusters (30)
- shell命令学习网站
- iOS瘦身-无用Pod依赖检查脚本
- ubuntu下libusb安装与测试
- dojo初解和dojo.connect用法(转)
- android 视屏手势 VideoGestureView
- 查看网络流量
- AFNetworking(v3.0+)框架学习总结(二内部逻辑处理过程)
- 【类反射】反射示例(模拟Instanceof的功能)