HDU6016Count the Sheep

来源:互联网 发布:mac怎么关闭开机声音 编辑:程序博客网 时间:2024/06/10 07:56
Count the SheepTime Limit: 3000/1500 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)Total Submission(s): 692    Accepted Submission(s): 299Problem DescriptionAltough Skipping the class is happy, the new term still can drive luras anxious which is of course because of the tests! Luras became worried as she wanted to skip the class, as well as to attend the BestCoder and also to prepare for tests at the same time.However, As the result of preparing for tests, luras had no time to practice programing. She didn't want to lose her rating after attending BC. In the end, she found BCround92's writer snowy_smile for help, asking him to leak her something.Snowy_smile wanted to help while not leaking the problems. He told luras, the best thing to do is to take a good rest according to the following instructions first."Imagine you are on the endless grassland where there are a group of sheep. And n sheep of them are silent boy-sheep while m sheep are crying girl-sheep. And there are k friend-relationships between the boy-sheep and girl-sheep.Now You can start from any sheep, keep counting along the friend relationship. If you can count 4 different sheep, you will exceed 99% sheep-counters and fall asleep."Hearing of the strange instructions, luras got very shocked. Still, she kept counting. Sure enough, she fell asleep after counting 4 different sheep immediately. And, she overslept and missed the BestCoder in the next day. At a result, she made it that not losing her rating in the BCround92!!!However, you don't have the same good luck as her. Since you have seen the 2nd problem, you are possible to have submitted the 1st problem and you can't go back.So, you have got into an awkward position. If you don't AC this problem, your rating might fall down.You question is here, please, can you tell that how many different 4-sheep-counting way luras might have before her sleep?In another word, you need to print the number of the "A-B-C-D" sequence, where A-B, B-C, C-D are friends and A,B,C,D are different.InputThe first line is an integer T which indicates the case number.and as for each case, there are 3 integers in the first line which indicate boy-sheep-number, girl-sheep-number and friend-realationship-number respectively.Then there are k lines with 2 integers x and y in each line, which means the x-th boy-sheep and the y-th girl-sheep are friends.It is guaranteed that——There will not be multiple same relationships.1 <= T <= 1000for 30% cases, 1 <= n, m, k <= 100for 99% cases, 1 <= n, m, k <= 1000for 100% cases, 1 <= n, m, k <= 100000OutputAs for each case, you need to output a single line.there should be 1 integer in the line which represents the number of the counting way of 4-sheep-sequence before luras's sleep.Sample Input32 2 41 11 22 12 23 1 31 12 13 13 3 31 12 12 2Sample Output802SourceBestCoder Round #92

不难发现 4头羊连一起 肯定是这样:

A1----B1   \    \A2----B2

直接搜索显然会超时 而且从男羊A2到女羊B1 必然也存在一种方案从B1到A2
如果枚举中间的那条边
如果A1有x个朋友
B2有y个朋友
A1可以有x-1个朋友可以被选为B1
B2可以选y-1个朋友作为A2
那以这条边为中间边的方案就有2(x1)(y1)
枚举每条边作为中间边即可

#include<iostream>#include<stdlib.h>#include<stdio.h>#include<string>#include<vector>#include<deque>#include<queue>#include<algorithm>#include<set>#include<map>#include<stack>#include<time.h>#include<math.h>#include<list>#include<cstring>#include<fstream>#include<queue>#include<sstream>//#include<memory.h>using namespace std;#define ll long long#define ull unsigned long long#define pii pair<int,int>#define INF 1000000007#define pll pair<ll,ll>#define pid pair<int,double>const int inf=1e9+7;const int N =100000+5;pii fri[N];int a[N],b[N];int main(){    //freopen("/home/lu/Documents/r.txt","r",stdin);    //freopen("/home/lu/Documents/w.txt","w",stdout);    int T;    scanf("%d",&T);    while(T--){        int n,m,k;        scanf("%d%d%d",&n,&m,&k);        fill(a,a+n+1,0);        fill(b,b+m+1,0);        for(int i=0;i<k;++i){            scanf("%d%d",&fri[i].first,&fri[i].second);            ++a[fri[i].first];            ++b[fri[i].second];        }        ll ans=0;        for(int i=0;i<k;++i){            ans+=(ll)(a[fri[i].first]-1)*(b[fri[i].second]-1);        }        printf("%lld\n",ans*2);    }    return 0;}
0 0
原创粉丝点击