题意
给定不同人员的信息,包括父母、子女、房产数量、面积。求每个家庭的最小id、人员数、平均房产数量、面积。并按顺序输出。
思路
- 根据id建图。由于id过大,不能直接邻接矩阵,思考两种方法,一种是邻接表法,一种是离散化。邻接表法10000 * 5(k <= 5)的大小可以实现,对于离散化,n最大5000,1E4散列5E3 = 100MB,矩阵还是大了。
- 邻接表法构造好图同时exi数组记录对应id是否存在,st数组记录对应id是否被访问过,接下来常规BFSorDFS即可。
杂
这题开始卡在思考要不要哈希,写了一堆发现加一个exi数组记录id是否存在会简化很多。
解法1 - BFS
//09:50
#include<bits/stdc++.h>
using namespace std;
const int N = 10004;
int n, k, id, fa, ma, chi;
int m[N], a[N];
bool st[N], exi[N];
struct family{
int minone, cnt;
double aset, aarea;
family(int a, int b, double c, double d): minone(a), cnt(b), aset(c), aarea(d){};
bool operator < (const family A) const{
return aarea != A.aarea ? aarea > A.aarea : minone < A.minone;
}
};
queue<int> que;
vector<vector<int>> rec(N);
set<family> ans;
int main(){
cin>>n;
for(int i = 0; i < n; i ++){
scanf("%d %d %d %d", &id, &fa, &ma, &k);
exi[id] = true;
if(fa != -1){
rec[id].push_back(fa);
rec[fa].push_back(id);
exi[fa] = true;
}
if(ma != -1){
rec[id].push_back(ma);
rec[ma].push_back(id);
exi[ma] = true;
}
for(int j = 0; j < k; j ++){
scanf("%d", &chi);
rec[id].push_back(chi);
rec[chi].push_back(id);
exi[chi] = true;
}
scanf("%d %d", &m[id], &a[id]);
}
for(int i = 0; i < 10001; i ++){
if(exi[i] && !st[i]){
st[i] = true;
que.push(i);
int cnt = 0, sumset = 0, sumarea = 0, minid = INT_MAX;
while(que.size()){
int t = que.front(); que.pop();
cnt++;
sumset += m[t], sumarea += a[t];
minid = min(t, minid);
for(auto j : rec[t]){
if(!st[j]){
que.push(j);
st[j] = true;
}
}
}
ans.insert(family(minid, cnt, sumset / (double)cnt, sumarea / (double) cnt));
}
}
cout<<ans.size();
for(auto i : ans)
printf("\n%04d %d %.3lf %.3lf", i.minone, i.cnt, i.aset, i.aarea);
return 0;
}
解法2 - DFS
#include<bits/stdc++.h>
using namespace std;
struct estate{
int n;
int area;
}estates[10000];
struct fam{
int m_id;
int n;
double sets,areas;
};
vector<vector<int>>road(10000);
bool visited[10000]{};
void DFS(int start,int &count,int &min_id,int &sum_set,int &sum_area){
count++;
min_id=min(min_id,start);
sum_set+=estates[start].n;
sum_area+=estates[start].area;
for(int i=0;i<road[start].size();i++){
if(!visited[road[start][i]]){
visited[road[start][i]]=true;
DFS(road[start][i],count,min_id,sum_set,sum_area);
}
}
}
bool cmp(fam a,fam b){
return a.areas!=b.areas?a.areas>b.areas:a.m_id<b.m_id;
}
int main(){
int n,id,f,m,k,kid;cin>>n;
int min_id=9999,count_n=0,sum_set=0,sum_area=0,count=0;
vector<fam>ans;
vector<int>exist;
for(int i=0;i<n;i++){
cin>>id;
exist.push_back(id);
cin>>f>>m;
if(f!=-1){
road[id].push_back(f);
road[f].push_back(id);
}if(m!=-1){
road[id].push_back(m);
road[m].push_back(id);
}
cin>>k;
for(int j=0;j<k;j++){
cin>>kid;
road[id].push_back(kid);
road[kid].push_back(id);
}
cin>>estates[id].n>>estates[id].area;
}
for(int i=0;i<exist.size();i++){
if(!visited[exist[i]]){
visited[exist[i]]=true;
DFS(exist[i],count_n,min_id,sum_set,sum_area);
ans.push_back({min_id,count_n,(double)sum_set/count_n,(double)sum_area/count_n});
count++;
min_id=9999, count_n=sum_set=sum_area=0;
}
}
sort(ans.begin(),ans.end(),cmp);
cout<<count<<endl;
for(int i=0;i<ans.size();i++){
printf("%04d %d %.3lf %.3lf\n",ans[i].m_id,ans[i].n,ans[i].sets,ans[i].areas);
}
return 0;
}
题目
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
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