标签:void pac sort 关系 小数 main struct c++ can
#include<bits/stdc++.h>
using namespace std;
const int MAXV = 1010;
const int MAXN = 10000;
struct Data{
int id, fid, mid;
int num, area;
int kid[10];
}data[MAXV];
struct node{
int id, people;
double num, area;
bool flag = false;
}ans[MAXN];
bool visit[MAXN];
int father[MAXN];
int findFather(int x){
int a = x;
while(x != father[x]){
x = father[x];
}
while(a != father[a]){
int z = a;
a = father[a];
father[z] = x;
}
return x;
}
void Union(int a, int b){
int faA = findFather(a);
int faB = findFather(b);
if(faA > faB){
father[faA] = faB;
}else if(faA < faB){
father[faB] = faA;
}
}
bool cmp(node a, node b){
if(a.area != b.area){
return a.area > b.area;
}else{
return a.id < b.id;
}
}
int main(){
int n, k, cnt = 0;
scanf("%d", &n);
for(int i = 0; i < 10000; i++){
father[i] = i;
}
for(int i = 0; i < n; i++){
scanf("%d %d %d %d", &data[i].id, &data[i].fid, &data[i].mid, &k);
visit[data[i].id] = true;
if(data[i].fid != -1){
visit[data[i].fid] = true;
Union(data[i].id, data[i].fid);
}
if(data[i].mid != -1){
visit[data[i].mid] = true;
Union(data[i].id, data[i].mid);
}
for(int j = 0; j < k; j++){
scanf("%d", &data[i].kid[j]);
visit[data[i].kid[j]] = true;
Union(data[i].id, data[i].kid[j]);
}
scanf("%d %d", &data[i].num, &data[i].area);
}
for(int i = 0; i < n; i++){
int id = findFather(data[i].id);
ans[id].id = id;
ans[id].num += data[i].num;
ans[id].area += data[i].area;
ans[id].flag = true;
}
for(int i = 0; i < MAXN; i++){
if(visit[i]){
int id = findFather(i);
ans[id].people++;
}
if(ans[i].flag){
cnt++;
}
}
for(int i = 0; i < MAXN; i++){
if(ans[i].flag){
ans[i].num = (double)(ans[i].num * 1.0 / ans[i].people);
ans[i].area = (double)(ans[i].area * 1.0 / ans[i].people);
}
}
sort(ans, ans+MAXN, cmp);
printf("%d\n", cnt);
for(int i = 0; i < cnt; i++){
printf("%04d %d %.3f %.3f\n", ans[i].id, ans[i].people, ans[i].num, ans[i].area);
}
return 0;
}
标签:void pac sort 关系 小数 main struct c++ can
原文地址:https://www.cnblogs.com/tsruixi/p/12989314.html