【强连通+背包】CF1763E

Problem - E - Codeforces

题意

思路

首先,先考虑第一个条件,要保证是p个节点互相到达且节点数最少,一定是个强连通,图的形态一定就是和强连通相关的。

然后,因为在这个前提上,要让单向节点数尽可能多,那就考虑将这些强连通分量用有向边连接

那么用哪些多大的强连通连接在一起就用背包处理一下就好了,因为要让节点数尽可能少,代价就是节点数,价值就是每个团的点对数,即x * (x - 1) / 2

然后背包完之后考虑第二问,求单向点数

把背包的方案求出来之后直接计算贡献即可,具体看代码

#include <bits/stdc++.h>

#define int long long

constexpr int N = 2e5 + 10;
constexpr int mod = 998244353;
constexpr int Inf = 0x3f3f3f3f;

int n;
int f[N];
int dp[N];

int calc(int x) {
	return x * (x - 1) / 2;
}
void solve() {
	std::cin >> n;
	memset(dp, 0x3f, sizeof(dp));
	dp[0] = 0;
	for (int i = 2; i <= 633; i ++) {
		int w = calc(i);
		for (int j = w; j <= n; j ++) {
			if (dp[j] > dp[j - w] + i) {
				dp[j] = dp[j - w] + i;
				f[j] = i;
			}
		}
	}
	std::vector<int> b;
	for (int i = n; i; i -= calc(f[i])) b.push_back(f[i]);
	int sum = 0, ans = 0;
	for (auto x : b) {
		ans += sum * x;
		sum += x;
	}
	std::cout << dp[n] << " " << ans << "\n";
}
signed main() {
	std::ios::sync_with_stdio(false);
	std::cin.tie(nullptr);

	int t = 1;
	while (t--) {
		solve();
	}
	return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值