Codeforces Round #428 (Div. 2)C

本文探讨了一道涉及贪心算法的题目,并特别讨论了容易忽视的特殊情况:当剩下三组每组两人的情况,可以使用两个四人座来容纳。通过具体的代码实现,展示了如何通过深度优先搜索(DFS)来解决该问题。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

这题就是一道简单的贪心,但是少考虑了一种特例,当还剩下3组人,每组2个人,可以用2个4排的座位放下这些人。

1102

3302

#include <iostream>
#include <stdio.h>
#include <cstring>
#include <algorithm>
#include <vector>
#include <queue>
#include <vector>
using namespace std;
#define Max_N 100000+20
int n;
vector<int> G[Max_N];
int used[Max_N];
long long int num;
double ans;
int dfs(int x, int fa, int deep, double g)
{
	if (G[x].size() == 1) {
		ans += g * deep;
	}
	for (int i = 0; i < G[x].size(); i++) {
		if (G[x][i] == fa) continue;
		int s = G[x].size();
		if (x != 1) s--;
		dfs(G[x][i], x, deep + 1, g/s) ;
		
		//cout << x << G[x][i] << endl;
	}
	return 0;
}
int main()
{
	cin >> n;
	int x, y;
	num = 0;
	ans = 0;
	memset(used, 0, sizeof(used));
	for (int i = 1; i < n; i++) {
		scanf("%d%d", &x, &y);
		G[y].push_back(x);
		G[x].push_back(y);
	}
	used[1] = 1;
	//cout << "ss" << endl;
	dfs(1, -1,  0, 1);
	//cout << ans << num;
	printf("%.15f\n", ans);

	return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值