当前位置:网站首页>1107 social clusters (30 points)
1107 social clusters (30 points)
2022-07-03 04:55:00 【vs5】
The main idea of the topic : Everyone likes some hobbies , Those who have the same hobbies are considered as a group , Number of output groups , And the number of people in each group , From large to small output .
Transform the meaning of the question to find the number of connected blocks , And the number of points in the block . Don't mix the hobby number with the person number here , We mark our hobbies with human labels , Then there is the merge set template .
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
const int N = 1000;
int p[N],st[N];
int find(int x)
{
if(p[x] != x) p[x] = find(p[x]);
return p[x];
}
int main()
{
int n;
scanf("%d",&n);
for(int i = 1; i <= n; i ++) p[i] = i;
for(int i = 1; i <= n; i ++)
{
int k;
scanf("%d:",&k);
for(int j = 0 ; j < k; j ++)
{
int x;
cin >> x;
if(!st[x]) st[x] = i;// The number is i People like it j This activity
int a = find(i),b = find(st[x]);
if(a != b) p[a] = b;
}
}
vector<int>ans(n + 1,0);int cnt = 0;
for(int i = 1; i <= n; i ++) ans[find(i)] ++;
for(auto it : ans) if(it != 0) cnt ++;
sort(ans.begin(),ans.end(),[](int a,int b)
{
return a > b;
});
cout << cnt << '\n';
for(int i = 0; i < cnt; i ++)
{
if(i != 0) cout << ' ';
cout << ans[i];
}
return 0;
}
边栏推荐
- Games101 Lesson 9 shading 3 Notes
- Kept hot standby and haproxy
- Mobile terminal - uniapp development record (public request encapsulation)
- Leetcode simple question: check whether the array is sorted and rotated
- Review the old and know the new: Notes on Data Science
- sql语句模糊查询遇到的问题
- Preparation for school and professional cognition
- Retirement plan fails, 64 year old programmer starts work again
- Actual combat 8051 drives 8-bit nixie tube
- Market status and development prospect prediction of the global fire alarm sensor industry in 2022
猜你喜欢
Keepalived热备与HAProxy
Thesis reading_ ICD code_ MSMN
The consumption of Internet of things users is only 76 cents, and the price has become the biggest obstacle to the promotion of 5g industrial interconnection
Leetcode simple question: the key with the longest key duration
MPM model and ab pressure test
[luatos sensor] 2 air pressure bmp180
JDBC database operation
The reason why the entity class in the database is changed into hump naming
Use Sqlalchemy module to obtain the table name and field name of the existing table in the database
Leetcode simple question: check whether the array is sorted and rotated
随机推荐
Leetcode simple question: check whether the string is an array prefix
Interface frequency limit access
[SQL injection point] location and judgment of the injection point
Without 50W bride price, my girlfriend was forcibly dragged away. What should I do
Leetcode simple question: check whether the array is sorted and rotated
Notes | numpy-09 Broadcast
关于开学的准备与专业认知
Why does I start with =1? How does this code work?
Do you know UVs in modeling?
第十九届浙江省 I. Barbecue
The process of browser accessing the website
50 practical applications of R language (36) - data visualization from basic to advanced
[set theory] binary relation (example of binary relation operation | example of inverse operation | example of composite operation | example of limiting operation | example of image operation)
Number of 1 in binary (simple difficulty)
[luatos sensor] 2 air pressure bmp180
Shell script -- condition judgment
[PCL self study: filtering] introduction and use of various filters in PCL (continuously updated)
Handler understands the record
[Yu Yue education] basic reference materials of interchangeability and measurement technology of Zhongyuan Institute of Technology
Market status and development prospect prediction of global neutral silicone sealant industry in 2022