PAT甲級--1114 Family Property(25 分)【並查集】
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 Mestate Area
where ID
is a unique 4-digit identification number for each person; Father
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
解題思路:這題用了並查集,瞭解並查集可以看看https://blog.csdn.net/Imagirl1/article/details/82353349,瞭解這裡用2個結構,一個儲存答案,一個用來處理輸入資料,並查集就是來找有多少個家族,然後統計每個家族有多少人多少sets多少area,然後注意下輸出來就好了,這裡-1的人不要處理。
#include<bits/stdc++.h>
using namespace std;
struct Data{//處理輸入資料
int id,fid,mid;
int c[6];
int num,area;
};
struct Ans{//答案的元素
int id,people;
double num,area;
bool flag;//flag是最終的父親節點為true,多少個true,多少個家庭
};
Ans ans[10000];
Data data[10000];
bool cmp(Ans a,Ans b)//答案輸出的順序
{
if(a.area!=b.area) return a.area>b.area;
else return a.id<b.id;
}
int father[10000];
bool visit[10000];//判斷是不是活著的
//並查集
int find(int x)
{
while(x!=father[x])
x=father[x];
return x;
}
void union1(int x,int y)
{
int fa1=find(x);
int fa2=find(y);
if(fa1>fa2) father[fa1]=fa2;
else if(fa1<fa2) father[fa2]=fa1;
}
int main(void)
{
int n,k;
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;
union1(data[i].fid,data[i].id);
}
if(data[i].mid!=-1)//只處理在的人
{
visit[data[i].mid]=true;
union1(data[i].mid,data[i].id);
}
for(int j=0;j<k;j++)
{
scanf("%d",&data[i].c[j]);
visit[data[i].c[j]]=true;
union1(data[i].c[j],data[i].id);
}
scanf("%d %d",&data[i].num,&data[i].area);
}
for(int i = 0; i < n; i++) {
int id = find(data[i].id);//找出最父親的節點,把data[i]的所有東西都加到這個節點
ans[id].id = id;
ans[id].num += data[i].num;
ans[id].area += data[i].area;
ans[id].flag = true;
}
int cnt=0;
for(int i=0;i<10000;i++)
{
if(visit[i])//還在的人
{
ans[find(i)].people++;//最父親的節點的人數加1
}
if(ans[i].flag==true) cnt++;//多少個true多少個家族
}
for(int i=0;i<10000;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 + 10000, 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;
}
相關推薦
PAT甲級--1114 Family Property(25 分)【並查集】
1114 Family Property(25 分) This time, you are supposed to help us collect the data for family-owned property. Given each person's family
1118 Birds in Forest (25 分)【並查集的應用】
1118 Birds in Forest (25 分) Some scientists took pictures of thousands of birds in a forest. Assume that all the birds appear in the same
1118 Birds in Forest (25 分)【並查集的應用】
1118 Birds in Forest (25 分) Some scientists took pictures of thousands of birds in a forest. Assume that all the birds appear in the same
1126 Eulerian Path(25 分)【並查集/dfs】
題意:如果一個連通圖的所有結點的度都是偶數,那麼它就是Eulerian,如果除了兩個結點的度是奇數其他都是偶數,那麼它就是Semi-Eulerian,否則就是Non-Eulerian (歐拉回路:圖G的一個迴路,如果恰通過圖G的每一條邊,則該回路稱為歐拉回路,具有歐拉回
PAT (Advanced Level) Practice 1114 Family Property (25 分)
程式設計題 1114 Family Property (25 分) This time, you are supposed to help us collect the data for family-owned property. Given each per
1114 Family Property (25 分)
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, a
PAT甲級--1154 Vertex Coloring (25 分)【hash】
A proper vertex coloring is a labeling of the graph's vertices with colors such that no two vertices sharing the same edge have the same col
PAT甲級 1101 Quick Sort (25 分) 快排
1101 Quick Sort (25 分) There is a classical process named partition in the famous quick sort algorithm. In this process we typic
PAT甲級 1063 Set Similarity (25 分)Set集合
1063 Set Similarity (25 分) Given two sets of integers, the similarity of the sets is defined to be Nc/Nt×100%, where Nc is the n
PAT甲級 1083 List Grades (25 分)排序水題
1083 List Grades (25 分) Given a list of N student records with name, ID and grade. You are supposed to sort the records with respect to t
PAT甲級 1101 Quick Sort (25 分) 快排
1101 Quick Sort (25 分) There is a classical process named partition in the famous quick sort algorithm. In this process we typically choo
PAT甲級 1105 Spiral Matrix (25 分)模擬
1105 Spiral Matrix (25 分) This time your job is to fill a sequence of N positive integers into a spiral matrix in non-increasing order. A
PAT甲級 1122 Hamiltonian Cycle (25 分)圖論
1122 Hamiltonian Cycle (25 分) The "Hamilton cycle problem" is to find a simple cycle that contains every vertex in a graph. Such a cycle i
PAT甲級 1121 Damn Single (25 分)set
1121 Damn Single (25 分) "Damn Single (單身狗)" is the Chinese nickname for someone who is being single. You are supposed to find those who ar
PAT 甲級 1083 List Grades (25 分)
1083 List Grades (25 分) Given a list of N student records with name, ID and grade. You are supposed to sort the records wi
PAT甲級 1037 Magic Coupon (25 分)(貪心)
1037 Magic Coupon (25 分) The magic shop in Mars is offering some magic coupons. Each coupon has an integer N printed on it, meaning that
PAT甲級--1004 Counting Leaves(30 分)【層次遍歷+dfs】
1004 Counting Leaves(30 分) A family hierarchy is usually presented by a pedigree tree. Your job is to count those family members who have
1048 Find Coins (25 分)【Hash雜湊】
1048 Find Coins (25 分) Eva loves to collect coins from all over the universe, including some other planets like Mars. One day she visited a universal
1071 Speech Patterns (25 分)【map的使用】
1071 Speech Patterns (25 分) People often have a preference among synonyms of the same word. For example, some may prefer "the police", whi
PTA——03-樹1 樹的同構(25 分)【java語言實現】
題目 給定兩棵樹T1和T2。如果T1可以通過若干次左右孩子互換就變成T2,則我們稱兩棵樹是“同構”的。例如圖1給出的兩棵樹就是同構的,因為我們把其中一棵樹的結點A、B、G的左右孩子互換後,就得到另外一棵樹。而圖2就不是同構的。 現給定兩棵樹,請你判斷