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
基本就是給你每個人的id,以及這個id的對應的父親id 孩子id 以及estate和area
你需要根據給的信息把所有的有關的人歸到一類之中
- 歸類使用並查集,題目要求最小的id爲一類的首領,只要在並查集合並函數時候判斷一下id大小,pre[大的id]=小的[id]
- 然後得到了記錄所有id的首領信息的pre數組,需要提取pre數組裏面存了幾個id的值,樣例的pre裏面就保存了8888 0001 5551 三個id的值,提取方法很多,可以使用一個sign【10000】數組,sign[pre[i]]=1; 然後遍歷sign數組,看看哪些值爲1,那麼就知道了有幾個家族首領了。我用的set,感覺代碼會少點
- 提取到了所有家族首領的id還需要 一個結構體來保存首領的id(排序輸出時候需要用到)對應對應家族的人數啊 estate和area啊 。我是用的map<id,node> mapp 實現的
unordered_map<int,node> mapp;
for(auto it=ShowSn.begin(); it!=ShowSn.end(); it++)
{
node p=node();
p.id=*it;
mapp[*it]= p;
}
- 然後對所有的id進行掃描(我也是用的set存儲的),看看是那個家族的,把該id有的estate和area加到家族結構體中
- 最後輸出還需要用到sort,我用了個vector存儲node。。。。感覺好麻煩。。。。
代碼實現時候對於結構體,因爲我們結構體是在map<int,node> 裏面 和vector裏面使用的,(不是之前二叉樹時候結構體是被另一個結構體指針指向的,就需要new了),直接使用node a=node() 這種臨時變量的形式就好了,不用new了,因爲存儲到map裏面人家也會自己new一個新的結構體只是值和我們臨時定義的結構體一樣,地址啥的完全不一樣了。
vector< int > 也是完全一樣的,我們生命的結構體只是給人家傳傳值罷了
#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
#include <unordered_set>
#include <unordered_map>
using namespace std;
int pre[10001];
int findP(int x)
{
if(x!=pre[x])
pre[x]=findP(pre[x]);
return pre[x];
}
void Union(int a,int b)
{
int ap=findP(a);
int bp=findP(b);
if(ap<bp)
pre[bp]=ap;
else
pre[ap]=bp;
}
struct node
{
int id;
int num;
double estate,area;
node():num(0),estate(0),area(0) {}
};
int sort2(node a,node b){
if(a.area!=b.area)
return a.area>b.area;
else{
return a.id<b.id;
}
}
int main()
{
int N;
cin>>N;
double estateSn[10000]= {0};
double areaSn[10000]= {0};
for(int i=0; i<=9999; i++)
pre[i]=i;
unordered_set<int> idSet;
for(int i=0; i<N; i++)
{
int id,idFather,idMother, childNum,estate,area;
scanf("%d %d %d %d",&id,&idFather,&idMother,&childNum);
idSet.insert(id);
if(idFather!=-1)
{
idSet.insert(idFather);
Union(id,idFather);
}
if(idMother!=-1)
{
idSet.insert(idMother);
Union(id,idMother);
}
for(int q=0; q<childNum; q++)
{
int son;
cin>>son;
Union(id,son);
idSet.insert(son);
}
cin>>estate>>area;
areaSn[id]=area;
estateSn[id]=estate;
}
unordered_set<int> ShowSn;
for(auto it=idSet.begin(); it!=idSet.end(); it++)
ShowSn.insert(findP(*it));
unordered_map<int,node> mapp;
for(auto it=ShowSn.begin(); it!=ShowSn.end(); it++)
{
node p=node();
p.id=*it;
mapp[*it]= p;
}
for(auto it=idSet.begin(); it!=idSet.end(); it++)
{
//這裏記得用指針,結構體的等於是賦值,
node *p=&mapp[pre[*it]];
p->num++;
p->area+=areaSn[*it];
p->estate+=estateSn[*it];
}
vector<node> vec;
for(auto it=mapp.begin(); it!=mapp.end(); it++){
node *p=&it->second;
p->estate=p->estate/p->num;
p->area=p->area/p->num;
vec.push_back(it->second);
}
sort(vec.begin(),vec.end(),sort2);
cout<<vec.size()<<endl;
for(int i=0;i<vec.size();i++){
printf("%04d %d %.3f %.3f\n",vec[i].id,vec[i].num,vec[i].estate,vec[i].area);
}
return 0;
}