1114 Family Property (25 point(s))
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 Nlines follow, each gives the infomation of a person who owns estate in the format:
ID
Father
Mother
k Child1⋯Childk Mestate 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; Mestate 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
AVGsets AVGarea
where ID
is the smallest ID in the family; M
is the total number of family members; AVGsets is the average number of sets of their real estate; and AVGarea 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
非常爽!30分钟AC了本题。
题意:
给出一些人的家庭关系图,要求计算每个家庭的人口、平均房产、平均面积、最小编号的成员。并且按照一定顺序输出。
考点:
图的存储、深度优先搜索、运算符重载、STL
思路:
1. 首先使用vector<int> graph[]存储图的结构,采用无向图的存储方式。visit[]表示存在的结点。
2. 受题目中要输出每个家庭中最小编号成员,而成员编号都是4位数字的启发,我们按照编号从小到大遍历每个存在的成员(visit[i]==true),并以这个成员作为入口,使用DFS遍历,每一个家庭就是图中的一个分支。遍历的同时记录结点数(人口)、房产总数、面积总数,同时将访问过的结点都取消标记。将每一次的记录存储在set<family> F中。
3. 编码实现结构体family的重载,并输出结果。
注意点:
1. 特别小心本题的图的存储,特别是关于父母亲的关系;
2. 注意输出格式。
#include<iostream>
#include<cstring>
#include<vector>
#include<set>
using namespace std;
const int MAX = 1e5+7;
vector<int> graph[MAX];
bool visit[MAX]={false};
double sets[MAX];
double area[MAX];
struct family{
int id;int m;double avgSets,avgArea;
family(int i,int n,double as,double aa):id(i),m(n),avgSets(as),avgArea(aa){}
bool operator < (const family &other) const{
if(avgArea!=other.avgArea) return avgArea>other.avgArea;
return id<other.id;
}
void output() const{
printf("%04d %d %.3f %.3f\n",id,m,avgSets,avgArea);
}
};
int cnt;double sumSets,sumArea;
void dfs(int v){
cnt++;
sumSets+=sets[v];
sumArea+=area[v];
visit[v]=false;
for(int i=0;i<graph[v].size();i++){
int cur = graph[v][i];
if(visit[cur]){
dfs(cur);
}
}
}
set<family> F;
int main(void){
int N;cin>>N;
int id,father,mother,k,a;
for(int i=0;i<N;i++){
cin>>id>>father>>mother>>k;
visit[id]=true;
if(father!=-1){
visit[father]=true;
graph[id].push_back(father);
graph[father].push_back(id);
}
if(mother!=-1){
visit[mother]=true;
graph[id].push_back(mother);
graph[mother].push_back(id);
}
while(k--){
cin>>a;
visit[a]=true;
graph[id].push_back(a);
graph[a].push_back(id);
}
cin>>sets[id]>>area[id];
}
for(int i=0;i<10000;i++){
if(visit[i]){
cnt = 0;sumSets=sumArea=0.0;
dfs(i);
F.insert(family(i,cnt,sumSets/double(cnt),sumArea/double(cnt)));
}
}
set<family>::iterator it;
printf("%d\n",(int)F.size());
for(it=F.begin();it!=F.end();it++){
(*it).output();
}
return 0;
}