PAT 1114 Family Property (并查集)

#include
#include
#include
#include
using namespace std;
#define m 10000
int road[m],n;
struct item{//先合并item,才能统计
	int id,h,area;
}d[1010];
int mem[m];//family mem
struct family{
	int minid;
	double avearea, avehouse;
}f[m];//打表
bool cmp(family a,family b){
	if(a.avearea!=b.avearea) return a.avearea>b.avearea;
	else return a.minidv){
		road[u]=v;
		mem[v]+=mem[u];
	}
}
void init(){
	for (int i = 0; i < m; i++){
		mem[i]=1;
		road[i]=i;
	}
}
int main(){
	init();
	cin>>n;
	int id,fid,mid,k;
	for (int i = 0; i < n; i++){
		scanf("%d%d%d%d",&id,&fid,&mid,&k);
		d[i].id=id;
		if(fid!=-1)
			merge(id,fid);
		if(mid!=-1)
			merge(id,mid);
		for (int j = 0; j < k; j++){
			int t;
			scanf("%d",&t);
			merge(t,id);
		}
		scanf("%d%d",&d[i].h,&d[i].area);
	}
	for (int i = 0; i < m; i++)
		getroot(i);
	set ansid;
	for (int i = 0; i < n; i++){
		int root=getroot(d[i].id);
		if(ansid.count(root)){
			f[root].avearea+=d[i].area;
			f[root].avehouse+=d[i].h;
		}else{
			f[root].minid=root;
			f[root].avearea=d[i].area;
			f[root].avehouse=d[i].h;
			ansid.insert(root);
		}
	}
	vector ans;
	for(set::iterator it=ansid.begin();it!=ansid.end();it++){
		int t=*it;
		f[t].avearea=f[t].avearea*1.0/mem[t];
		f[t].avehouse=f[t].avehouse*1.0/mem[t];
		ans.push_back(f[t]);
	}
	sort(ans.begin(),ans.end(),cmp);
	printf("%d\n",ans.size());
	for (int i = 0; i < ans.size(); i++)
		printf("%.4d %d %.3lf %.3lf\n",ans[i].minid,mem[ans[i].minid],ans[i].avehouse,ans[i].avearea);
	return 0;
}

你可能感兴趣的:(PAT 1114 Family Property (并查集))