当前位置:网站首页>1107 social clusters (30 points)
1107 social clusters (30 points)
2022-06-30 14:54:00 【Xue Dongjing】
maxmin
1107 Social Clusters (30 branch )
The question
give n Personal interests , People with the same interests become a collection .
Number of output groups , Output the number of people in each group in descending order .
Ideas
Check your hobbies and , Open an array to record the number of people in each hobby . Finally, the number of all hobbies of a group is concentrated on the hobbies of the root node .
Code
// Begin to use map Storage , But found map It's not easy to use here , Something's wrong bug, On that basis .
#include<stdio.h>
#include<map>
#include<iostream>
#include<algorithm>
using namespace std;
map<int,int>gp,pr;
map<int,int>::iterator itor;
map<int,int>::reverse_iterator to;
int pre[100007],ans[100007],ang[100007];
int find(int x)
{
int y=x,z;
while(x!=pre[x]){
x=pre[x];
}
while(y!=x){
z=pre[y];
pre[y]=x;
y=z;
}
return x;
}
void link(int a,int b)
{
pre[a]=b;
}
bool cmp(int a,int b)
{
return a>b;
}
int main()
{
int n,k,x,temp,p,q,cnt=0;
string str;
scanf("%d",&n);
for(int i=0;i<100007;i++){
pre[i]=i;
ans[i]=0;
}
for(int i=0;i<n;i++){
scanf("%d",&k);
cin>>str;
if(k!=0){
scanf("%d",&x);
p=find(x);
ans[p]+=1;
gp[p]=ans[p];
for(int j=1;j<k;j++){
scanf("%d",&x);
q=find(x);
if(p!=q){
link(q,p);
ans[p]+=ans[q];
gp[p]=ans[p];
ans[q]=0;
gp[q]=0;
}
}
}
}
for(itor=gp.begin();itor!=gp.end();itor++){
// Want to use map Automatic sorting of
// printf("!!!!%d %d\n",itor->first,itor->second);
if(itor->second!=0){
// printf("@@%d \n",itor->first);
// pr[itor->second]=itor->first;// When the two groups have the same number of people, there is a mistake here
ang[cnt++]=itor->second;
}
}
// printf("%d\n",pr.size());
sort(ang,ang+cnt,cmp);
printf("%d\n",cnt);
for(int i=0;i<cnt;i++){
if(i!=0){
printf(" ");
}
printf("%d",ang[i]);
}
// for(to=pr.rbegin();to!=pr.rend();to++){
// if(to!=pr.rbegin()){
// printf(" ");
// }
// cout<<to->first<<" "<<to->second<<endl;;
// }
return 0;
}
边栏推荐
- CCF image rotation (Full Score code + problem solving idea) 201503-01
- val_ Loss decreases first and then increases or does not decrease but only increases
- JS array
- CCF sequence segmentation (Full Score code + problem solving idea) 201509 -1
- JS array sorting method summary
- Experiment 2: stack
- V3 01_ Welcome
- Three ways and differences of defining functions in JS
- Querywrapper in mybaits plus
- How to program and process such parts?
猜你喜欢

day02

CCF image rotation (Full Score code + problem solving idea) 201503-01

Laravel upload error
![[buuctf] [geek challenge 2019] secret file](/img/00/23bebd013eb4035555c0057725e3c4.jpg)
[buuctf] [geek challenge 2019] secret file

CCF access control system (Full Score code + problem solving idea) 201412-1

2021-05-12

Lihongyi machine learning 2020 homework summary

JS to realize simple lottery function

Determine the number of digits of an integer in MATLAB (one line of code)

2021-07-14 mybaitsplus
随机推荐
Learn about data kinship JSON format design from sqlflow JSON format
catkin_ Make reports an error, transfers the location of the workspace, and uses other people's workspace files to cause compilation errors
Programming exercises: whole point and circle (solution ideas and code implementation)
Finding the root of an integer by dichotomy
2021 geek challenge Web
CCF string matching (Full Score code + problem solving ideas + skill summary) March 3, 2014
V3 01_ Welcome
Is it troublesome for CITIC futures to open an account? Is it safe? How much is the handling charge for opening an account for futures? Can you offer a discount
Laravel upload error
Greedy two-dimensional array sorting
Color classification of sorting
[buuctf] [actf2020 freshman competition]include
1133: output family and friends string
Distributed -- openresty+lua+redis
CCF window (Full Score code + problem solving idea) March 2, 2014
One dimensional and two dimensional array addresses
For loop and promise to solve the problem of concurrent callback
Detailed explanation of settimeout() and setinterval()
Binary rotation array (2)
The kth largest element in the sorted array