团体程序设计天梯赛 L2-007 家庭房产 (25分)

题目链接:

L2-007 家庭房产 (25分)

思路:

对于每个人,将和自己有关系的人都保存下来,将每个人的房产保存下来,然后对所有人dfs即可;

代码:

#include<bits/stdc++.h>

using namespace std;

const int maxn = 12345;
vector<int> G[maxn];
int n, vst[maxn], a[maxn], cnt[maxn], s[maxn];
int no, ans, tcnt, ts;
void dfs(int u) {           //cerr << u << '\n';
	++ans;
	no = min(no, u);
	vst[u] = true;
	tcnt += cnt[u];
	ts += s[u];
	for(int & x : G[u]) {
		if(vst[x] == false) dfs(x);
	}
}
struct fam {
	int no,	ans;
	double cnt, s;
	bool operator < (const fam & x) const {
		return s == x.s ? no < x.no : s > x.s;
	}
};
int main() {
#ifdef MyTest
	freopen("Sakura.txt", "r", stdin);
#endif	
	scanf("%d", &n);
	for(int i = 0; i < n; i++) {
		scanf("%d", a + i);
		vector<int> v(2);
		int k, x;
		scanf("%d %d %d", &v[0], &v[1], &k);
		while(k--) { scanf("%d", &x); v.push_back(x); }
		for(int & x : v) { 
		    if(~x)	G[x].push_back(a[i]), G[a[i]].push_back(x);
		}
		scanf("%d %d", cnt + a[i], s + a[i]);
	}
	vector<fam> v; 
	for(int i = 0; i < n; i++) {
		if(vst[a[i]] == false) {
			no = a[i];
			ans = tcnt = ts = 0;
			dfs(no);              //cerr << "--------------\n";
			v.push_back(fam {no, ans, tcnt * 1.0 / ans, ts * 1.0 / ans});
		}
	}
	sort(v.begin(), v.end());
	printf("%d\n", v.size());
	for(fam & x : v) {
		printf("%04d %d %.3f %.3f\n", x.no, x.ans, x.cnt, x.s);
	}
	return 0;
}
发布了280 篇原创文章 · 获赞 7 · 访问量 6691

猜你喜欢

转载自blog.csdn.net/qq_45228537/article/details/104050858