#include<iostream>
#include<set>
#include<cstdio>
#include<vector>
#include<queue>
#include<algorithm>
using namespace std;
struct family{
int id,number;
double avg_area,avg_set;
};
bool comp(family x,family y){
if(x.avg_area!=y.avg_area) return x.avg_area>y.avg_area;
return x.id<y.id;
}
double all_number=0;
double all_area=0;
double all_set=0;
int visited[10000]={0};
family f[10000];
int ans;
vector<int>idddd;
vector<int>v[10000];//邻接表
int state[10000];//set套数
int area[10000]; //面积
queue<int>q;
void bfs(int cur){
visited[cur]=1;
all_number++;
all_set+=state[cur];
all_area+=area[cur];
for(int i=0;i<v[cur].size();i++){
int temp=v[cur][i];
if(visited[temp]==0) {
q.push(temp);
visited[temp]=1;
}
}
if(q.size()>0){
int next=q.front();
q.pop();
bfs(next);
}
}
int main(){
int n;
scanf("%d",&n);
for(int i=0;i<n;i++){
int id,fuq,muq;
scanf("%d%d%d",&id,&fuq,&muq);
idddd.push_back(id);
if(fuq!=-1){
v[id].push_back(fuq);
idddd.push_back(fuq);
v[fuq].push_back(id);
}
if(muq!=-1){
v[id].push_back(muq);
v[muq].push_back(id);
idddd.push_back(muq);
}
int child;
cin>>child;
for(int j=0;j<child;j++){
int id1;
scanf("%d",&id1);
v[id1].push_back(id);
v[id].push_back(id1);
idddd.push_back(id1);
}
int k1,k2;
scanf("%d%d",&k1,&k2);
state[id]=k1;
area[id]=k2;
}
sort(idddd.begin(),idddd.end());
// for(int i=0;i<idddd.size();i++) cout<<idddd[i]<<" ";
for(int i=0;i<idddd.size();i++){
if(i>0){
if(idddd[i]==idddd[i-1]) continue;
}
if(visited[idddd[i]]==0){
f[ans].id=idddd[i];
all_number=0;
all_area=0;
all_set=0;
bfs(idddd[i]);
f[ans].avg_area=all_area/all_number;
f[ans].number=all_number;
f[ans].avg_set=all_set/all_number;
ans++;
}
}
sort(f,f+ans,comp);
cout<<ans<<endl;
for(int i=0;i<ans;i++){
printf("%04d %d %.3lf %.3lf",f[i].id,f[i].number,f[i].avg_set,f[i].avg_area);
if(i!=ans-1) printf("\n");
}
}
PAT甲级1114 Family Property
最新推荐文章于 2021-02-11 19:21:41 发布