Count the Sheep
Time Limit: 3000/1500 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)Total Submission(s): 1338 Accepted Submission(s): 612
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.
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
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.
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
3 2 2 4 1 1 1 2 2 1 2 2 3 1 3 1 1 2 1 3 1 3 3 3 1 1 2 1 2 2
Sample Output
8 0 2
这道题蛮有意思,就是问能有多少个四元线。
细想一下,如果输入完关系之后,是不是就形成了一个完整的关系图。然后如果我们知道任意两个点,
还知道公牛只跟母牛做朋友,母牛只跟公牛做朋友,只需要知道这两个点分别相连了几个点就好了嘛!
假设a公牛练了4头母牛,b母牛连了5头公牛,问你ab在中间的4元线有多少个,3*4 = 12.
这时只需要再去处理一遍输入就好了。
上代码:
#include<cstdio>
#include<iostream>
#include<algorithm>
#include<queue>
#include<stack>
#include<cstring>
using namespace std;
typedef long long ll;
int numb[100005],numg[100005];
int inb[100005],ing[100005];
int main()
{
int t;
int n,m,k;
scanf("%d",&t);
while(t--)
{
memset(numb,0,sizeof(numb));
memset(numg,0,sizeof(numg));
scanf("%d %d %d",&n,&m,&k);
for(int i = 1;i<= k;i++)
{
scanf("%d%d",&inb[i],&ing[i]);
numb[inb[i]]++;numg[ing[i]]++;
}
ll ans = 0;//不用long long 可能不够
for(int i = 1;i<= k;i++)
{
ans+= (numb[inb[i]]-1)*(numg[ing[i]]-1);//记得减1哦!
}
ans*= 2;//倒过来亦可以,乘以2就行
printf("%lld\n",ans);
}
return 0;
}
如有不明白或错误,欢迎评论指正.