#include<iostream>
#include<algorithm>
#include<vector>
#include<unordered_set>
using namespace std;
struct node{
int totalnum;
double totoalsets;
double totoalarea;
double avgarea;
double avgset;
}family[10000];
bool visited[10000];
bool rootvisited[10000]={false};
bool appear[10000];
vector<int> father(10000);
vector<int> Root;
unordered_set<int> myset;
int findfather(int x){
int a=x;
while(x!=father[x])
x=father[x];
while(a!=father[a]){
int z=a;
a=father[a];
father[z]=x;
}
return x;
}
void Union(int x,int y){
int faA=findfather(x);
int faB=findfather(y);
if(faA>faB)
father[faA]=faB;
else if(faA<faB)
father[faB]=faA;
}
bool cmp(int root1,int root2){
if(family[root1].avgarea!=family[root2].avgarea)
return family[root1].avgarea>family[root2].avgarea;
else
return root1<root2;
}
int main(){
int N,Person,Father,Mother,child,k;
double set,area;
for(int i=0;i<father.size();i++)
father[i]=i;
cin>>N;
for(int i=0;i<N;i++){
cin>>Person>>Father>>Mother;
visited[Person]=true;
if(Father!=-1){
visited[Father]=true;
Union(Person,Father);
}
if(Mother!=-1){
visited[Mother]=true;
Union(Person,Mother);
}
cin>>k;
for(int j=0;j<k;j++){
cin>>child;
visited[child]=true;
Union(Person,child);
}
cin>>set>>area;
family[Person].totoalsets=set;
family[Person].totoalarea=area;
}
for(int i=0;i<10000;i++){
if(visited[i]==true){
int root=findfather(i);
if(root==i){
myset.insert(root);
family[i].totalnum=1;
continue;
}
family[root].totoalarea+=family[i].totoalarea;
family[root].totoalsets+=family[i].totoalsets;
family[root].totalnum++;
myset.insert(root);
}
}
for(auto st=myset.begin();st!=myset.end();st++){
Root.push_back(*st);
int root=*st;
family[root].avgarea=family[root].totoalarea/family[root].totalnum;
family[root].avgset=family[root].totoalsets/family[root].totalnum;
}
sort(Root.begin(),Root.end(),cmp);
cout<<myset.size()<<endl;
for(int i=0;i<Root.size();i++){
int k=Root[i];
printf("%04d %d %.3lf %.3lf",k,family[k].totalnum,family[k].avgset,family[k].avgarea);
if(i!=Root.size()-1)
cout<<endl;
}
return 0;
}
“相关推荐”对你有帮助么?
-
非常没帮助
-
没帮助
-
一般
-
有帮助
-
非常有帮助
提交