L2-007. 家庭房产
题目链接:https://www.patest.cn/contests/gplt/L2-007
#include<iostream> #include<cstdio> #include<cstdlib> #include<cstring> #include<cmath> #include<set> #include<algorithm> using namespace std; const int maxn=10007; const int INF=0x3f3f3f3f; int vis[maxn], p[maxn], m[maxn]; struct node { int id, num; double ans1, ans2; } a[maxn], b[maxn], mul[maxn]; void init() { memset(vis, 0, sizeof(vis)); memset(mul, 0, sizeof(mul)); for(int i=0; i<10000; i++) p[i]=i; } int cmp(node a, node b) { if(a.ans2!=b.ans2) return a.ans2>b.ans2; return a.id<b.id; } int Find(int x) { while(x!=p[x]) x=p[x]; return x; } void solve(int a, int b) { int x=Find(a); int y=Find(b); if(x!=y) { if(x>y) p[x]=y; else p[y]=x; } } int main() { int n; scanf("%d", &n); init(); for(int i=0; i<n; i++) { int p1, p2, m, d; scanf("%d %d %d", &a[i].id, &p1, &p2); vis[a[i].id]=1; if(p1!=-1) { solve(a[i].id, p1); vis[p1]=1; } if(p2!=-1) { solve(a[i].id, p2); vis[p2]=1; } scanf("%d", &m); while(m--) { scanf("%d", &d); if(d!=-1) { solve(a[i].id, d); vis[d]=1; } } scanf("%lf %lf", &a[i].ans1, &a[i].ans2); } for(int i=0; i<n; i++) { int id=Find(a[i].id); mul[id].id=id; mul[id].ans1+=a[i].ans1; mul[id].ans2+=a[i].ans2; } for(int i=0; i<10000; i++) { if(vis[i]) mul[Find(i)].num++; } int cnt=0; for(int i=0; i<10000; i++) { if(vis[i]) { int id=Find(i); if(!m[id]) { m[id]=1; double x=double(mul[id].num); b[cnt].id=id; b[cnt].ans1=mul[id].ans1*1.0/x; b[cnt].ans2=mul[id].ans2*1.0/x; b[cnt++].num=mul[id].num; } } } sort(b, b+cnt, cmp); printf("%d\n", cnt); for(int i=0; i<cnt; i++) printf("%04d %d %.3f %.3f\n", b[i].id, b[i].num, b[i].ans1, b[i].ans2); return 0; }