1114. Family Property (25)
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 100Sample Output:
3 8888 1 1.000 1000.000 0001 15 0.600 100.000 5551 4 0.750 100.000分析:用图(并查集)结构存储输入;用bfs遍历计算。
细节:图的边为无向边;
(以下代码比较丑,还是完整定义图结构比较迅速方便)
#include <iostream>
#include <string>
#include <cstdio>
#include <cmath>
#include <queue>
#include <vector>
#include <functional>
#define rep(i,j,k) for(int i=j;i<=k;++i)
const int Max=10000;
using namespace std;
class node
{
public:
int id;
int sets;
int area;
vector<int> family;
node(){
id=sets=area=0;
};
};
struct res
{
int minres;
float set,are;
int sum;
friend bool operator < (const res& a, const res& b){
return a.are<b.are || (a.are==b.are && a.minres>b.minres);
}
};
vector<int> root;
node estate[Max];
int visited[Max];
priority_queue<res> result;
void create(int n)
{
while (n--){
node x;
int id,f,m,k;
scanf("%d %d %d %d",&id, &f, &m, &k);
root.push_back(id);
x.id=id;
x.family.push_back(f);
x.family.push_back(m);
estate[f].family.push_back(id); // f==-1时仍然可以执行
estate[m].family.push_back(id); //
while (k--){
int c;
scanf("%d",&c);
x.family.push_back(c);
estate[c].family.push_back(id);
}
int s,a;
scanf("%d %d",&s, &a);
x.sets=s;
x.area=a;
estate[id]=x;
}
}
void bfs(int root)
{
queue<int> q;
int minres=10000;
int sum=0;
float set=0,are=0;
q.push(root);
visited[root]=1;
int v;
while(!q.empty()){
v=q.front();
if (v<minres) minres=v;
++sum;
set+=estate[v].sets;
are+=estate[v].area;
int size=estate[v].family.size();
rep(i,0,size-1){
if (estate[v].family[i]==-1) continue;
if (visited[estate[v].family[i]]!=1){
visited[estate[v].family[i]]=1;
q.push(estate[v].family[i]);
}
}
q.pop();
}
res t;
t.minres=minres;
t.set=set/sum;
t.are=are/sum;
t.sum=sum;
result.push(t);
}
void major()
{
rep(i,1,root.size()){
if (visited[root[i-1]]!=1){
bfs(root[i-1]);
}
}
cout<<result.size()<<endl;
while (!result.empty()){
res t=result.top();
printf("%04d %d %.3f %.3f\n", t.minres, t.sum, t.set, t.are);
result.pop();
}
}
int main()
{
// freopen("test.txt","r",stdin);
int n;
cin>>n;
create(n);
major();
return 0;
}