牛客小白月赛 23 C. 完全图

链接

https://ac.nowcoder.com/acm/contest/4784/C

题意

给定一个包含 n n n 个顶点的完全图,可以删掉图中的一些边,但是删掉的边不能超过 m m m 条,问删去边之后的图最多能有几个连通分量

思路

反过来想,要添加 C n 2 − m C_n^2−m Cn2m条边,让连通块尽量多

形成 n − 1 , n − 2 , n − 3 , . . . , n − i n-1,n-2,n-3,...,n-i n1,n2,n3,...,ni 个连通块能用完的最大边数依次为: 1 , 3 , 6 , . . . , i ( i + 1 ) 2 1,3,6,...,\frac{i(i+1)}{2} 1,3,6,...,2i(i+1)

二分即可

代码

#include<bits/stdc++.h>
using namespace std;
int main() {
    ios::sync_with_stdio(false);
    cin.tie(0);
    int T;cin>>T;
    while(T--) {
        long long n,m;
        cin>>n>>m;
        __int128 x=(__int128)n*(n-1)/2-m;
        long long l=0,r=n,ans;
        while(l<r) {
            long long mid=l+r+1>>1;
            if(x<=(__int128)(n-mid)*(n-mid+1)/2) l=mid;
            else r=mid-1;
        }
        cout<<l<<endl;
    }
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值