https://pintia.cn/problem-sets/994805046380707840/exam/problems/994805068539215872?type=7&page=1
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<ll, ll> PII;
typedef vector<long long> VI;
//#define int long long
#define INF 0x3f3f3f3f
#define endl '\n'
#define N 10010
const int mod = 1e9 + 7;
ll ksm(ll a, ll b) {
ll ans = 1;
for (; b; b >>= 1LL) {
if (b & 1) ans = ans * a % mod;
a = a * a % mod;
}
return ans;
}
ll lowbit(ll x) {
return -x & x;
}
//int p[N],si[N];
//int find(int x) {
// if (x == p[x])return p[x];
// p[x] = find(p[x]);
// return p[x];
//}
//size[find(b)] += size[find(a)];
//p[find(a)] = find(b);
int n;
bool vis[N];
struct no1 { //存房产数和总面积
double area, tao;
};
struct no2 { //答案的结构体
int id, num;
double area, tao;
};
bool cmp(no2 a, no2 b) {
if (a.area != b.area)return a.area > b.area; //注意有时会有精度问题则 if(abs(a.area - b.area) > 0.0001);
return a.id < b.id;
}
map<int, no2> mp;
vector<no1> a(N + 10);
int p[N + 10];
int find(int x) {
if (x != p[x])p[x] = find(p[x]);
return p[x];
}
void merge(int a, int b) {
a = find(a);
b = find(b);
if (a < b)p[b] = a;
else p[a] = b;
}
void solve() {
cin >> n;
a.clear();
for (int i = 0; i < N; i ++)p[i] = i;
for (int i = 1; i <= n; i++) {
int id, fa, mo;
cin >> id >> fa >> mo;
vis[id] = vis[fa] = vis[mo] = 1;
if (fa != -1)merge(id, fa);
if (mo != -1)merge(id, mo);
int k;
cin >> k;
for (int j = 1; j <= k; j ++) {
int son;
cin >> son;
vis[son] = 1;
merge(id, son);
}
double tao, area;
cin >> tao >> area;
a[id] = {area, tao};
// cout << a[id].area << " " << a[id].tao << endl;
}
for (int i = 0; i < N; i ++) {
if (vis[i]) {
int t = find(i);
mp[t].id = t;
mp[t].num ++;
mp[t].area += a[i].area;
mp[t].tao += a[i].tao;
}
}
vector<no2> res;
for (auto t : mp) {
t.second.tao /= 1.0 * t.second.num;
t.second.area /= 1.0 * t.second.num;
res.push_back(t.second);
}
sort(res.begin(), res.end(), cmp);
cout << res.size() << endl;
for (auto t : res) {
printf("%04d %d %.3lf %.3lf\n", t.id, t.num, t.tao, t.area);
}
}
signed main() {
// ios::sync_with_stdio(false);
// cin.tie(0), cout.tie(0);
int T = 1;
// cin >> T;
while (T --)
solve();
return 0;
}