7-2 家庭房产 (25 分)
原题地址
模拟就完事了
#include <vector>
#include<stdio.h>
#include<string.h>
#include <cstring>
#include <list>
#include <map>
#include <set>
#include <deque>
#include <stack>
#include <bitset>
#include <algorithm>
#include <functional>
#include <iostream>
#include <iomanip>
#include <cstdio>
#include <cmath>
#include <set>
#include<cstring>
#define ll long long
#define MODD 1000000007
#define pii pair<int,int>
#include<stdio.h>
#include<string.h>
using namespace std;
struct node{
double square,house;
int numer;
}pre[1000000];//all number
struct Cmp{
int numer;
double square , house;
}cmp[100000];//save smallest number
int cmd(Cmp a ,Cmp b){//compare
if(a.square==b.square)
return a.numer<b.numer;
return a.square>b.square;
}
int unionsearch(int root) //查找根结点
{
int son, tmp;
son = root;
while(root != pre[root].numer) //我的上级不是掌门
root = pre[root].numer;
while(pre[son].numer != root) //我就找他的上级,直到掌门出现
{
tmp = pre[son].numer;
pre[son].numer = root;
son = tmp;
}
return root; //掌门驾到~~
}
void join(int root1, int root2)
{
int x, y;
x = unionsearch(root1);//我老大是玄慈
y = unionsearch(root2);//我老大是灭绝
if(x < y)
pre[y].numer = x; //打一仗,谁输就当对方老大
else if(x>y)pre[x].numer = y;
}
int existed[100000]={0};//存在的人
int main(){
int n;cin>>n;
for(int i=0;i<10000;i++){
pre[i].numer = i;
pre[i].house = 0;
pre[i].square= 0;
}
for(int i=0;i<n;i++){
int name;cin >>name ;
existed[name]=1;
int name1=0;
cin >>name1;if(name1!=-1)join(name1,name),existed[name1]=1;//save father
cin >>name1;if(name1!=-1)join(name1,name),existed[name1]=1;//save mother
int num=0;cin >>num;
for(int j=0;j<num;j++){
cin >>name1;
join(name,name1),existed[name1]=1;
}
cin >>num;pre[name].house = num;
cin >>num;pre[name].square=num;
}
int cnt[10000]={0};
for(int i=0;i<10000;i++){
if(existed[i]==1){
cnt[unionsearch(i)]++;
if(unionsearch(i)!=i){
pre[unionsearch(i)].house+=pre[i].house;pre[i].house=0;
pre[unionsearch(i)].square+=pre[i].square;pre[i].square=0;
}
}
}
int sum=0,mm=0;
for(int i=0;i<10000;i++){
if(cnt[i]!=0){
sum++;
cmp[mm].house=pre[unionsearch(i)].house/cnt[i];
cmp[mm].square=pre[unionsearch(i)].square/cnt[i];
cmp[mm++].numer = unionsearch(i);
}
}
cout<<sum<<endl;
sort(cmp,cmp+n,cmd);
for(int i=0;i<sum;i++){
printf("%04d %d %.3f %.3f\n",cmp[i].numer,cnt[cmp[i].numer],cmp[i].house,cmp[i].square);
}
}