并查集
#include <iostream>
#include <unordered_map>
#include <vector>
#include <algorithm>
using namespace std;
struct Family{
int numOfMembers;
int numOfHouses;
int totalArea;
int minId;
};
bool cmp(Family a,Family b){
double avera = (double)a.totalArea / a.numOfMembers,averb = (double)b.totalArea / b.numOfMembers;
if(avera != averb)
return avera > averb;
else
return a.minId < b.minId;
}
unordered_map<int,int> idInVec;
unordered_map<int,Family> rootInFamily;
vector<int> pre;
vector<Family> res;
int unionSearch(int root){
int son,tmp;
son = root;
while(root != pre[root])
root = pre[root];
while(son != root){
tmp = pre[son];
pre[son] = root;
son = tmp;
}
return root;
}
void join(int root1,int root2){
int x,y;
x = unionSearch(root1);
y = unionSearch(root2);
if(x != y){
pre[x] = y;
rootInFamily[y].minId = min(rootInFamily[y].minId,rootInFamily[x].minId);
rootInFamily[y].numOfHouses += rootInFamily[x].numOfHouses;
rootInFamily[y].numOfMembers += rootInFamily[x].numOfMembers;
rootInFamily[y].totalArea += rootInFamily[x].totalArea;
rootInFamily.erase(rootInFamily.find(x));
}
}
int main(){
int n;
scanf("%d",&n);
for(int i = 0;i < n;i++){
Family temp;
temp.numOfHouses = 0;
int id,father,mother;
scanf("%d %d %d",&id,&father,&mother);
if(idInVec.find(id) == idInVec.end()){
pre.push_back(pre.size());
idInVec[id] = pre.size() - 1;
rootInFamily[idInVec[id]] = temp;
rootInFamily[idInVec[id]].minId = id;
rootInFamily[idInVec[id]].numOfHouses = 0;
rootInFamily[idInVec[id]].numOfMembers = 1;
rootInFamily[idInVec[id]].totalArea = 0;
}
if(father != -1){
if(idInVec.find(father) == idInVec.end()){ // 父亲是第一次被发现
pre.push_back(pre.size());
idInVec[father] = pre.size() - 1;
rootInFamily[idInVec[father]] = temp;
rootInFamily[idInVec[father]].minId = father;
rootInFamily[idInVec[father]].numOfHouses = 0;
rootInFamily[idInVec[father]].numOfMembers = 1;
rootInFamily[idInVec[father]].totalArea = 0;
join(pre[idInVec[id]],pre[idInVec[father]]);
}else{
join(pre[idInVec[id]],pre[idInVec[father]]);
}
}
if(mother != -1){
if(idInVec.find(mother) == idInVec.end()){ // 父亲是第一次被发现
pre.push_back(pre.size());
idInVec[mother] = pre.size() - 1;
rootInFamily[idInVec[mother]] = temp;
rootInFamily[idInVec[mother]].minId = mother;
rootInFamily[idInVec[mother]].numOfHouses = 0;
rootInFamily[idInVec[mother]].numOfMembers = 1;
rootInFamily[idInVec[mother]].totalArea = 0;
join(pre[idInVec[id]],pre[idInVec[mother]]);
}else{
join(pre[idInVec[id]],pre[idInVec[mother]]);
}
}
int k;
scanf("%d",&k);
while(k > 0){
int child;
scanf("%d",&child);
if(idInVec.find(child) == idInVec.end()){ // 父亲是第一次被发现
pre.push_back(pre.size());
idInVec[child] = pre.size() - 1;
rootInFamily[idInVec[child]] = temp;
rootInFamily[idInVec[child]].minId = child;
rootInFamily[idInVec[child]].numOfHouses = 0;
rootInFamily[idInVec[child]].numOfMembers = 1;
rootInFamily[idInVec[child]].totalArea = 0;
join(pre[idInVec[id]],pre[idInVec[child]]);
}else{
join(pre[idInVec[id]],pre[idInVec[child]]);
}
k--;
}
int houseNum,houseArea;
scanf("%d %d",&houseNum,&houseArea);
rootInFamily[unionSearch(idInVec[id])].numOfHouses += houseNum;
rootInFamily[unionSearch(idInVec[id])].totalArea += houseArea;
}
int s = rootInFamily.size();
printf("%d\n",s);
unordered_map<int,Family>::iterator it = rootInFamily.begin();
while(it != rootInFamily.end()){
res.push_back(it->second);
it++;
}
sort(res.begin(),res.end(),cmp);
for(int i = 0;i < res.size();i++){
printf("%04d %d %.3f %.3f",res[i].minId,res[i].numOfMembers,(double)res[i].numOfHouses / res[i].numOfMembers,(double)res[i].totalArea / res[i].numOfMembers);
if(i != res.size() - 1)
printf("\n");
}
system("pause");
return 0;
}