1114. Family Property (25)
2017-02-20 15:50
253 查看
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:
Sample Output:
#include <iostream>
#include <vector>
#include <algorithm>
#include <queue>
#include <unordered_set>
using namespace std;
struct res{
int minid,tot;
double aveset,avearea;
res(int m=0,int t = 0,double s = 0,double a = 0):minid(m),tot(t),aveset(s),avearea(a){}
bool operator<(const res & r)const{
if(avearea!=r.avearea)return avearea>r.avearea;
return minid<r.minid;
}
};
const int maxn = 10005;
int n;
unordered_set<int> G[maxn];
int vis[maxn],sets[maxn],area[maxn],used[maxn];
void bfs(int root,int &tot,double &s,double &a,int &minid){
queue<int> que;
que.push(root);
vis[root] = 1;
tot = 0;s = 0;a=0;minid = 9999999;
while(que.empty() == false){
int q = que.front();
que.pop();
tot++;
s += sets[q];
a += area[q];
if(minid>q)minid =q;
for(unordered_set<int>::iterator itr = G[q].begin();itr != G[q].end();++itr){
int v = *itr;
if(vis[v] == 0){vis[v] = 1;
que.push(v);}
}
}
}
int main()
{
cin>>n;
int id,f,m,k,c;
double es,ea;
for(int i=0;i<n;i++){
cin>>id>>f>>m>>k;
if(f != -1){G[f].insert(id);G[id].insert(f);}
if(m != -1){G[m].insert(id);G[id].insert(m);}
used[id]=used[f]=used[m]=1;
for(int j=0;j<k;j++){
cin>>c;
G[c].insert(id);
G[id].insert(c);
used[c] =1;
}
cin>>sets[id]>>area[id];
}
vector<res> ans;
for(int i=0;i<maxn;i++){
if(used[i] == 1){
int tot,mind;
double s,a;
if(vis[i] == 0){
bfs(i,tot,s,a,mind);
ans.push_back(res(mind,tot,s/tot,a/tot));
}
}
}
sort(ans.begin(),ans.end());
cout<<ans.size()<<endl;
for(int i=0;i<ans.size();i++){
printf("%04d %d %.3lf %.3lf\n",ans[i].minid,ans[i].tot,ans[i].aveset,ans[i].avearea);
}
return 0;
}
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.0005551 4 0.750 100.000
#include <iostream>
#include <vector>
#include <algorithm>
#include <queue>
#include <unordered_set>
using namespace std;
struct res{
int minid,tot;
double aveset,avearea;
res(int m=0,int t = 0,double s = 0,double a = 0):minid(m),tot(t),aveset(s),avearea(a){}
bool operator<(const res & r)const{
if(avearea!=r.avearea)return avearea>r.avearea;
return minid<r.minid;
}
};
const int maxn = 10005;
int n;
unordered_set<int> G[maxn];
int vis[maxn],sets[maxn],area[maxn],used[maxn];
void bfs(int root,int &tot,double &s,double &a,int &minid){
queue<int> que;
que.push(root);
vis[root] = 1;
tot = 0;s = 0;a=0;minid = 9999999;
while(que.empty() == false){
int q = que.front();
que.pop();
tot++;
s += sets[q];
a += area[q];
if(minid>q)minid =q;
for(unordered_set<int>::iterator itr = G[q].begin();itr != G[q].end();++itr){
int v = *itr;
if(vis[v] == 0){vis[v] = 1;
que.push(v);}
}
}
}
int main()
{
cin>>n;
int id,f,m,k,c;
double es,ea;
for(int i=0;i<n;i++){
cin>>id>>f>>m>>k;
if(f != -1){G[f].insert(id);G[id].insert(f);}
if(m != -1){G[m].insert(id);G[id].insert(m);}
used[id]=used[f]=used[m]=1;
for(int j=0;j<k;j++){
cin>>c;
G[c].insert(id);
G[id].insert(c);
used[c] =1;
}
cin>>sets[id]>>area[id];
}
vector<res> ans;
for(int i=0;i<maxn;i++){
if(used[i] == 1){
int tot,mind;
double s,a;
if(vis[i] == 0){
bfs(i,tot,s,a,mind);
ans.push_back(res(mind,tot,s/tot,a/tot));
}
}
}
sort(ans.begin(),ans.end());
cout<<ans.size()<<endl;
for(int i=0;i<ans.size();i++){
printf("%04d %d %.3lf %.3lf\n",ans[i].minid,ans[i].tot,ans[i].aveset,ans[i].avearea);
}
return 0;
}
相关文章推荐
- PAT (Advanced Level) Practise 1114 Family Property (25) 并查集orDFS
- 1114. Family Property (25)
- PAT (Advanced Level) Practise 1114 Family Property (25)
- PAT (Advanced Level) Practise 1114 Family Property (25)
- PAT A1114 family property (25)
- PAT 1114 Family Property
- Family Property (25)解题报告
- PAT 1114 Family Property (并查集)
- pat 甲1114. Family Property (dfs)
- 1114. Family Property
- JAVA基础编程50题(25-27题)详解
- PAT甲级 1059. Prime Factors (25)
- Java网络编程从入门到精通(25):创建ServerSocket对象
- 1045. 快速排序(25)
- Android---25---Chronometer计时器
- 邮件服务端口 port 25、109、110、143、465、995、993
- 04-树4. Root of AVL Tree (25)
- Emergency (25)
- F - Piggy-Bank HDU 1114 (完全背包的变形+初始化细节)
- 德才论 (25)python3