Codeforces 420 C. Bug in Code

在一家名为F公司的组织中,发现了一项严重错误。公司领导决定在会议上找出问题的源头,即谁是造成代码错误的人。共有n名开发者参与了讨论,每个人声称他们知道错误是由另外两个人中的一个造成的。为了确保至少p个人同意选择的嫌疑人,公司领导需要找出所有可能的嫌疑人组合。本文将详细介绍如何计算所有可能的嫌疑人组合数量。

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



C. Bug in Code
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output

Recently a serious bug has been found in the FOS code. The head of the F company wants to find the culprit and punish him. For that, he set up an organizational meeting, the issue is: who's bugged the code? Each of the n coders on the meeting said: 'I know for sure that either x or y did it!'

The head of the company decided to choose two suspects and invite them to his office. Naturally, he should consider the coders' opinions. That's why the head wants to make such a choice that at least p of n coders agreed with it. A coder agrees with the choice of two suspects if at least one of the two people that he named at the meeting was chosen as a suspect. In how many ways can the head of F choose two suspects?

Note that even if some coder was chosen as a suspect, he can agree with the head's choice if he named the other chosen coder at the meeting.

Input

The first line contains integers n and p (3 ≤ n ≤ 3·105; 0 ≤ p ≤ n) — the number of coders in the F company and the minimum number of agreed people.

Each of the next n lines contains two integers xiyi (1 ≤ xi, yi ≤ n) — the numbers of coders named by the i-th coder. It is guaranteed that xi ≠ i,  yi ≠ i,  xi ≠ yi.

Output

Print a single integer –– the number of possible two-suspect sets. Note that the order of the suspects doesn't matter, that is, sets (1, 2)и (2, 1) are considered identical.

Sample test(s)
input
4 2
2 3
1 4
1 4
2 1
output
6
input
8 6
5 6
5 7
5 8
6 2
2 1
7 3
1 3
1 4
output
1

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <map>

using namespace std;

const int maxn=330000;
typedef long long int LL;

map<pair<int,int>, int> tu;
int agree[maxn];

int main()
{
    int n,p;
    scanf("%d%d",&n,&p);
    for(int i=1;i<=n;i++)
    {
        int u,v;
        scanf("%d%d",&u,&v);
        if(u>v) swap(u,v);
        agree[u]++; agree[v]++;
        tu[make_pair(u,v)]++;
    }
    LL ans=0;
    map<pair<int,int> ,int>::iterator it;
    for(it=tu.begin();it!=tu.end();it++)
    {
        int u=it->first.first,v=it->first.second;
        int res=agree[u]+agree[v];
        if(res>=p&&res-it->second<p)
            ans--;
    }
    int now=n;
    sort(agree+1,agree+1+n);
    for(int i=1;i<=n;i++)
    {
        now=max(i,now);
        while(i<now&&agree[i]+agree[now]>=p) now--;
        ans+=n-now;
    }
    printf("%I64d\n",ans);
    return 0;
}





评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值