codeforces 420C. Bug in Code

来源:互联网 发布:晋城监狱网络改造 编辑:程序博客网 时间:2024/05/02 02:42

http://codeforces.com/problemset/problem/420/C

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 then coders on the meeting said: 'I know for sure that eitherx 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 leastp 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 integersxi,yi(1 ≤ xi, yi ≤ n) — the numbers of coders named by thei-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 22 31 41 42 1
Output
6
Input
8 65 65 75 86 22 17 31 31 4
Output
1

贴一张图辅助说明:

设A和B被指认为嫌疑人的频度是agree[a],agree[b],被人同时认定是嫌疑人的频度是map(a,b),那么A和B影响的对数是P=agree[a]+agree[b]-map(a,b)。假如P>=k就算入结果中。为了快速统计结果,可以先放两个指针p, pp在排好序(从小到大)的agree数组左右两边,p每向右移动一位,pp不断向左移动,直到agree[p]+agree[pp]<k,这时结果加上n-pp(因为比pp大的dex满足agree[p]+agree[dex]>=k),迭代下去。。。通过此法算出总的个数(agree[a]+agree[b]>=k),然后减去P=agree[a]+agree[b]-map(a,b)<k的总数。

#include <iostream>#include <cstdio>#include <map>#include <cstring>#include <algorithm>using namespace std;const int N=3e5+10;typedef long long LL;map<pair<int,int>,int> mp;map<pair<int,int>,int>::iterator ix;int ag[N];int main(){    //freopen("cin.txt","r",stdin);    int n,p;    int a,b;    while(cin>>n>>p){        mp.clear();        memset(ag,0,sizeof(ag));        for(int i=0;i<n;i++){            scanf("%d%d",&a,&b);            if(a>b) swap(a,b);            mp[make_pair(a,b)]++;            ag[a]++;            ag[b]++;        }        //ag[a]+ag[b]-mp[a,b]>=p: YES        LL ans=0;        for(ix=mp.begin();ix!=mp.end();ix++){            int t1=ix->first.first,t2=ix->first.second;            if(ag[t1]+ag[t2]>=p&&ag[t1]+ag[t2]-ix->second<p) ans--;        }        sort(ag+1,ag+1+n);        int pp=n;        for(int i=1;i<=n;i++){            pp=max(pp,i);            while(i<pp&&ag[pp]+ag[i]>=p) pp--;            ans+=(n-pp)*1LL;        }        printf("%I64d\n",ans);    }    return 0;}



0 0
原创粉丝点击