#include <iostream>
#include <algorithm>
#include <vector>
#include <queue>
using namespace std;
const int maxn=10010;
struct people{
int numset,area;
vector<int> relation;
people(){
numset=area=0;
relation.clear();
}
}pe[maxn];
struct family{
int smallestid,nummember,sumset,sumarea;
family(){
smallestid=100000;
nummember=sumset=sumarea=0;
}
};
int N,root[1010],rootindex=0;
bool vis[maxn]={0};
vector<family> ans;
vector<int> exist;
void BFS(int r){
queue<int> q;
q.push(r);
vis[r]=true;
family temp;
while(!q.empty()){
int t=q.front();
q.pop();
temp.sumset+=pe[t].numset;
temp.nummember++;
temp.sumarea+=pe[t].area;
if(t<temp.smallestid) temp.smallestid=t;
for(int i=0;i<pe[t].relation.size();i++){
if(!vis[pe[t].relation[i]]){
vis[pe[t].relation[i]]=true;
q.push(pe[t].relation[i]);
}
}
}
ans.push_back(temp);
}
bool cmp(family a,family b){
double avg1=a.sumarea*1.0/(a.nummember*1.0),avg2=b.sumarea*1.0/(b.nummember*1.0);
if(avg1!=avg2) return avg1>avg2;
else return a.smallestid<b.smallestid;
}
int main(){
//freopen("g:\\in.txt","r",stdin);
scanf("%d",&N);
for(int i=0;i<N;i++){
int nowp,m,f,numch,ch,tnumset,tarea;
scanf("%d%d%d%d",&nowp,&m,&f,&numch);
exist.push_back(nowp);
if(m==-1&&f==-1) root[rootindex++]=nowp;
else{
if(m!=-1){
pe[nowp].relation.push_back(m);
pe[m].relation.push_back(nowp);
exist.push_back(m);
}
if(f!=-1){
pe[nowp].relation.push_back(f);
pe[f].relation.push_back(nowp);
exist.push_back(f);
}
}
for(int j=0;j<numch;j++){
scanf("%d",&ch);
pe[nowp].relation.push_back(ch);
pe[ch].relation.push_back(nowp);
exist.push_back(ch);
}
scanf("%d%d",&pe[nowp].numset,&pe[nowp].area);
}
for(int i=0;i<exist.size();i++){
if(!vis[exist[i]]){
BFS(exist[i]);
}
}
sort(ans.begin(),ans.end(),cmp);
printf("%d\n",ans.size());
for(int i=0;i<ans.size();i++)
printf("%04d %d %.3f %.3f\n",ans[i].smallestid,ans[i].nummember,ans[i].sumset*1.0/ans[i].nummember,ans[i].sumarea*1.0/ans[i].nummember);
return 0;
}
PAT A1114
最新推荐文章于 2021-04-27 12:13:31 发布