PTA Family Property (disjoint-set)

Given everyone's family members and their property in their name, you count the number of people per household, per capita area of ​​real estate and real estate cycle of songs.

Input formats:

Input of the first row is given a positive integer N (≤1000), followed by N rows, each row gives a person the following format Property:

No children Parents k 1 ... k real estate cycle of songs with a total area children

Wherein the code is unique to each person in a 4-digit number; father and mother, respectively corresponding to the number of this person's parents number (if already passed away, the display -1); k (0≤k≤5) is the number of children of that person; children i is the number of their children.

Output formats:

First, the number of the output of the first row of the family (all who have relatives belong to the same family). Then output information for each family in the following format:

The minimum number of family members per household population number per capita housing area real estate cycle of songs

Wherein the average value required to retain three decimal places. Family First Information Size high per capita output, if tied, the output member numbers in ascending order.

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


The disjoint-set is too complicated, adjusting a night, and finally found debugging, and anxious old spit blood .... Whenever a person is out of question to see Chen grandmother, to consciously gasp ...

#include<iostream>
#include<cstring>
#include<vector>
#include<algorithm>
#include<stack>
#include<queue>
#include<map>
#include<list>
#include<set>
#include<cmath>
using namespace std;
typedef long long ll;
#define N 100005

int fa[N],flag[N];

int find(int x){
	if(fa[x]==x) return x;
	return fa[x]=find(fa[x]);
}

void Union(int x,int y){
	if(x!=-1) flag[x] = 1;
	if(y!=-1) flag[y] = 1;
	if(x==-1||y==-1) return ;
	int rx = find(x),ry = find(y);
	if(rx!=ry) {
		if(rx<ry) fa[ry] = rx;
		else fa[rx] = ry;
	}
}

void init(){
	for(int i=0;i<N;i++) fa[i] = i;
}

struct family{
	int id;
	int num;
	int cnt;
	int area;
}arr[N],res[N];

bool cmp(family a,family b){
	double ave1 = a.area*1.0/a.num ,ave2 =  b.area*1.0/b.num;
	if(ave1!=ave2) 	return ave1 > ave2;
	else return a.id < b.id; 
}
int main(){
	
	int n,x,f,m,k,cnt,s,kid;
	cin >> n;
	init();
	fill(flag,flag+N,0);
	memset(arr,0,sizeof(arr));
	memset(res,0,sizeof(res)); 
	for(int i=0;i<n;i++){
		scanf("%d%d%d%d",&arr[i].id,&f,&m,&k);
		Union(f,arr[i].id); Union(arr[i].id,m);
		for(int j=0;j<k;j++){
			cin >> kid;
			Union(arr[i].id,kid);
		}	
		scanf("%d%d",&arr[i].cnt,&arr[i].area);
	}
	for(int i=0;i<n;i++){
		int idt = find(arr[i].id);
		res[idt].id = idt;
		res[idt].cnt += arr[i].cnt;
		res[idt].area += arr[i].area; 
	} 
	vector<int> v;
	for(int i=0;i<N;i++){
		if(flag[i]){
			int id = find(i);
			if(id==i) v.push_back(id);
			res[id].num ++;
			flag[id] = 1;
		} 
	}
	cout << v.size() << endl;
	family res1[v.size()];
	for(int i=0;i<v.size();i++){
		res1[i].id = v[i];
		res1[i].num = res[v[i]].num;
		res1[i].cnt = res[v[i]].cnt;
		res1[i].area = res[v[i]].area;
	}
	sort(res1,res1+v.size(),cmp);
	for(int i=0;i<v.size();i++){
		printf("%04d %d %.3lf %.3lf\n",res1[i].id,res1[i].num,res1[i].cnt*1.0/res1[i].num,res1[i].area*1.0/res1[i].num);
	}
	
	return 0;
}

Published 79 original articles · won praise 37 · views 8886

Guess you like

Origin blog.csdn.net/SinclairWang/article/details/104095971