hdu 6016

来源:互联网 发布:血源诅咒帅哥捏脸数据 编辑:程序博客网 时间:2024/05/22 16:55

Count the Sheep

Time Limit: 3000/1500 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 1101    Accepted Submission(s): 491


Problem Description
Altough 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.
 

Input
The 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 <= 1000

for 30% cases, 1 <= n, m, k <= 100

for 99% cases, 1 <= n, m, k <= 1000

for 100% cases, 1 <= n, m, k <= 100000
 

Output
As 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 Input
32 2 41 11 22 12 23 1 31 12 13 13 3 31 12 12 2
 

Sample Output
802
 
题目要求,寻找a-b,b-c,c-d且a,b,c,d不相同的总个数,首先,起初因为需要用到并查集的知识,然后还把并查集的模板打上去后,发现四个变量不好做。
后看别人的代码,发现有一更简单的方法,首先,必须是男女相见,所以只可能是男女男女或女男女男,这样,只需要求出其中的一种后将其乘二即可,然后,根据中间的一组,男女,定义两个数组a,b;分别记录每一个变量的出现次数,即每一个男羊女羊的朋友数目,这样中间的一组男女,男的羊还有(n-1)个女 羊朋友,然后,女羊还有(m-1)个男羊朋友,这样,(n-1)*(m-1)种,让后相加。最后乘二。
注意,用cin容易超时,scanf不会超时。

#include <iostream>#include <cstring>#include <cstdio>using namespace std;const int maxn=100010;struct node{    int x,y;}s[maxn];int a[maxn]; int b[maxn];int main(){    int t;    cin>>t;    while(t--){        int n,m,k;        cin>>n>>m>>k;        memset(a,0,sizeof(a));        memset(b,0,sizeof(b));        for(int i=0;i<k;i++){           scanf("%d%d",&s[i].x,&s[i].y);           a[s[i].x]++; b[s[i].y]++;        }        long long ans=0;        for(int i=0;i<k;i++){            ans+=(long long)(a[s[i].x]-1)*(b[s[i].y]-1);        }        cout << ans*2<<endl;    }    return 0;}


0 0
原创粉丝点击