[dp][图论]Node Pairs Codeforces1763E

48 篇文章 0 订阅
26 篇文章 0 订阅

Let's call an ordered pair of nodes (u,v)(u,v) in a directed graph unidirectional if u≠vu≠v, there exists a path from uu to vv, and there are no paths from vv to uu.

A directed graph is called pp-reachable if it contains exactly pp ordered pairs of nodes (u,v)(u,v) such that u<vu<v and uu and vv are reachable from each other. Find the minimum number of nodes required to create a pp-reachable directed graph.

Also, among all such pp-reachable directed graphs with the minimum number of nodes, let GG denote a graph which maximizes the number of unidirectional pairs of nodes. Find this number.

Input

The first and only line contains a single integer pp (0≤p≤2⋅1050≤p≤2⋅105) — the number of ordered pairs of nodes.

Output

Print a single line containing two integers — the minimum number of nodes required to create a pp-reachable directed graph, and the maximum number of unidirectional pairs of nodes among all such pp-reachable directed graphs with the minimum number of nodes.

Examples

input

3

output

3 0

input

4

output

5 6

input

0

output

0 0

题意: 给出一个参数p,要求能构成p可达图的最少点数,以及在这个点数下单向连通点对的最大值。其中p可达即为u < v且从u能到v从v也能到u的(u, v)个数,单向连通即u < v且从u能到v或v能到u的(u, v)个数。

分析: 这道题的关键在于考虑整幅图是由什么构成的,其实任何一个有向图都可以看作由若干强连通分量连接而成,这里的连接指的是一些单向边,也就是连接以后还是这些个强连通分量,它们不会增大也不会减小。其实知道了p可达图最少点数就知道了最多的单向连通数了,因为(u, v)之间的关系就三种,要么是双向连通,要么是单向连通,要么是不连通,而(u, v)的总数一共是n*(n-1)/2个,现在已知其中有p个是双向连通,那么单向连通+不连通个数就是n*(n-1)/2-p个,由于要单向连通数尽量多,那么自然就是n*(n-1)/2-p个了。

每个强连通分量对于答案的贡献是size*(size-1)/2,size是该强连通分量中点的个数,最后p就是由若干强连通分量的贡献加和得到,求解过程可以用dp处理,设dp[i]表示i可达图的最少点数,dp[1] = 2,然后枚举其中某个连通分量的点个数来进行更新,也就是dp[i] = min(dp[i], dp[i-j*(j-1)/2]+j)。

具体代码如下:

#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cstring>
#include <cmath>
#include <string>
#define inf 0x3f3f3f3f3f3f3f3f
#define int long long
using namespace std;

int dp[200005];

signed main(){
	int p;
	cin >> p;
	dp[1] = 2;
	for(int i = 2; i <= p; i++){
		dp[i] = inf;
		for(int j = 1; j*(j-1)/2 <= i; j++)
			dp[i] = min(dp[i], dp[i-j*(j-1)/2]+j);
	}
	printf("%lld %lld", dp[p], dp[p]*(dp[p]-1)/2-p);
	
    return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值