1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124
| #include<iostream> #include<vector> #include<algorithm> #include<iomanip> using namespace std; const double INF = 1e-7; int father[10001]; bool vis[10001];
int find(int x) { while(x != father[x]) x = father[x];
return x; }
void Union(int a, int b){ int fa = find(a); int fb = find(b); if(fa==fb) return; if(fa<fb){ father[fb] = fa; }else{ father[fa]=fb; } }
struct Node{ int id, fid,mid,cn,estate,area; int cid[10]; Node(int i, int f, int m,int cn):id(i),fid(f),mid(m),cn(cn),estate(0),area(0){ for(int i=0;i<10;i++){ cid[10]=0; } }; };
struct Data{ int people; int id; double estate; double area; bool flag; Data():people(0),estate(0),area(0),flag(false),id(0){}; };
bool cmp(Data a, Data b){ return abs(a.area - b.area) > INF ? a.area > b.area : a.id < b.id; } Data roots[10000]; int main(){ int N; cin>>N; vector<Node> nodes; for(int i=0;i<10001;i++){ father[i]=i; } for(int i=0;i<N;i++){ int id, fid,mid,cn; cin >> id >> fid>> mid>>cn; Node temp = Node(id,fid,mid,cn); for(int j=0;j<cn;j++){ cin>>temp.cid[j]; } cin>> temp.estate >> temp.area; nodes.push_back(temp); } for(int i=0;i<N;i++){ Node temp = nodes[i]; int k = nodes[i].cn; if(temp.fid!=-1){ Union(temp.id, temp.fid); vis[temp.fid] = true; } if(temp.mid!=-1){ Union(temp.id,temp.mid); vis[temp.mid] = true; } for(int j=0;j<k;j++){ Union(temp.id,temp.cid[j]); vis[temp.cid[j]] = true; } vis[temp.id] = true; }
int count = 0; for(int i=0;i<N;i++){ Node temp = nodes[i]; int x = find(temp.id); roots[x].id = x ; if(!roots[x].flag){ count++; roots[x].flag = true; } roots[x].estate +=temp.estate; roots[x].area += temp.area; } for(int i=0;i<10000;i++){ if(vis[i]){ roots[find(i)].people++; } } for(int i=0;i<10000;i++){ if(roots[i].flag){
roots[i].estate = roots[i].estate /(1.0*roots[i].people); roots[i].area= roots[i].area / (1.0*roots[i].people); } } sort(roots,roots+10000,cmp); cout<<setiosflags(ios::fixed)<< setprecision(3); for(int i=0;i<count;i++){ char ss[5]; sprintf(ss,"%04d",roots[i].id); cout<<ss<<" "<<roots[i].people<<" "<<roots[i].estate<<" "<<roots[i].area<<endl; }
}
|