Count the Sheep

来源:互联网 发布:茶叶网络连锁 编辑:程序博客网 时间:2024/05/22 06:15

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
80

2

这道题我得唠唠,刚开始我看到这个题,就以为是图的广搜,但我太粗心了,这极大地反映出我对于广搜的不理解,不理解啊,然后我又想到了深搜,但深搜肯定是超时的,再想想,那么这道题肯定是有简单解法的,在这里我们再分析题目中关键的部分,就能看到四个人,为什么是四个人呢?这里边就有问题了,好了,具体解释在代码里。

#include<bits/stdc++.h>using namespace std;const int maxn = 1e5+5;int man[maxn], girl[maxn];long long c1[maxn], c2[maxn];//c1用来统计和编号为i的男生有朋友关系的女生数量,c2同理int main(){    int T, n, m, k;    scanf("%d", &T);    while(T--)    {        scanf("%d%d%d", &n, &m, &k);        memset(c1, 0, sizeof(c1));        memset(c2, 0, sizeof(c2));        for(int i = 1; i <= k; i++)        {            scanf("%d%d", &man[i], &girl[i]);//man[i]-girl[i]相当于记录了一队男女朋友关系            c1[man[i]]++;//和这个男的相连的点+1            c2[girl[i]]++;//和这个女的相连的点+1        }        long long ans = 0;        for(int i = 1; i <= k; i++)            ans += (c1[man[i]]-1) * (c2[girl[i]]-1);//把这对关系拿出来,相当于一根筷子,筷子的两边一边是男一边是女,再在筷子的两个端点去统计男的相连的女生,和女生相连的男生,因为一根筷子就已经是一根筷子,所以-1        ans *= 2;//4个人的关系可以正着,也可以反着        printf("%I64d\n", ans);    }    return 0;}

0 0
原创粉丝点击