1114. Family Property (25)
时间限制 150 ms 内存限制 65536 kB 代码长度限制 16000 B
判题程序 Standard 作者 CHEN, Yue
This time, you are supposed to help us collect the data for family-owned property. Given each person’s family members, and the estate(房产)info under his/her own name, we need to know the size of each family, and the average area and number of sets of their real estate.
Input Specification:
Each input file contains one test case. For each case, the first line gives a positive integer N (<=1000). Then N lines follow, each gives the infomation of a person who owns estate in the format:
ID Father Mother k Child1 … Childk M_estate Area
where ID is a unique 4-digit identification number for each person; Father and Mother are the ID’s of this person’s parents (if a parent has passed away, -1 will be given instead); k (0<=k<=5) is the number of children of this person; Childi’s are the ID’s of his/her children; M_estate is the total number of sets of the real estate under his/her name; and Area is the total area of his/her estate.
Output Specification:
For each case, first print in a line the number of families (all the people that are related directly or indirectly are considered in the same family). Then output the family info in the format:
ID M AVG_sets AVG_area
where ID is the smallest ID in the family; M is the total number of family members; AVG_sets is the average number of sets of their real estate; and AVG_area is the average area. The average numbers must be accurate up to 3 decimal places. The families must be given in descending order of their average areas, and in ascending order of the ID’s if there is a tie.
Sample Input:
10
6666 5551 5552 1 7777 1 100
1234 5678 9012 1 0002 2 300
8888 -1 -1 0 1 1000
2468 0001 0004 1 2222 1 500
7777 6666 -1 0 2 300
3721 -1 -1 1 2333 2 150
9012 -1 -1 3 1236 1235 1234 1 100
1235 5678 9012 0 1 50
2222 1236 2468 2 6661 6662 1 300
2333 -1 3721 3 6661 6662 6663 1 100
Sample Output:
3
8888 1 1.000 1000.000
0001 15 0.600 100.000
5551 4 0.750 100.000
思路:利用并查集来得到最小根节点,然后利用压缩路径,使所有其他节点的根节点为此节点的,直接指向此节点,再遍历出现的节点,如果节点不是根节点,则将所有的信息叠加到根节点上,如果是根节点,则一定是需要输出的节点
#define _CRT_SECURE_NO_WARNINGS
#include <algorithm>
#include <iostream>
#include <vector>
#include <iomanip>
#include <set>
using namespace std;
const int MaxN = 10010;
bool isRoot[MaxN] = { 0 };
struct info{
int id, f, m, k, estate, area, n;
int child[6];
double a_es, a_area;
info() { n = 1; estate = area = 0; }
}person[MaxN]; int N;
int Root[MaxN];
int findRoot(int a) {
int tmp = a;
while (a != Root[a]) a = Root[a];
while (tmp != Root[tmp]) {
int f = Root[tmp];
Root[tmp] = a;
tmp = f;
}
return a;
}
void Union(int &a, int b) {
int fa = findRoot(a);
int fb = findRoot(b);
if (fa < fb) Root[fb] = fa;
else if (fa > fb) {
Root[fa] = fb;
a = fb;
}
}
bool cmp(struct info a, struct info b) {
if (a.a_area != b.a_area) return a.a_area > b.a_area;
return a.id < b.id;
}
set<int> member;
vector<struct info> res;
int main() {
#ifdef _DEBUG
freopen("data.txt", "r+", stdin);
#endif // _DEBUG
std::ios::sync_with_stdio(false);
cin >> N;
for (int i = 0; i < MaxN; ++i)Root[i] = i;
for (int i = 0; i < N; ++i) {
int id; cin >> id;person[id].id = id;
int tmp = id;
cin >> person[id].f >> person[id].m >> person[id].k; member.insert(id);
if (person[id].f != -1) {
Union(tmp, person[id].f);
member.insert(person[id].f);
}
if (person[id].m != -1) {
Union(tmp, person[id].m);
member.insert(person[id].m);
}
for (int k = 0; k < person[id].k; ++k) {
cin >> person[id].child[k];
Union(tmp, person[id].child[k]);
member.insert(person[id].child[k]);
}
cin >> person[id].estate >> person[id].area;
}
vector<int> isRoot;
for (auto it = member.begin(); it != member.end(); ++it) {
int r = findRoot(*it);
if(*it != r){
person[r].area += person[*it].area;
person[r].estate += person[*it].estate;
++person[r].n;
}
else isRoot.push_back(*it);
}
for (int i = 0; i < isRoot.size(); ++i) {
int id = isRoot[i]; person[id].id = id;
person[id].a_es = (double)person[id].estate / person[id].n;
person[id].a_area = (double)person[id].area / person[id].n;
res.push_back(person[id]);
}
sort(res.begin(), res.end(), cmp);
cout << res.size(); cout << endl;
for (int i = 0; i < res.size(); ++i) {
cout <<setw(4) << setfill('0') << res[i].id;
cout <<" " << res[i].n << " ";
cout << setiosflags(ios::fixed) << setprecision(3) << res[i].a_es << " " << res[i].a_area << endl;
}
return 0;
}