【HDU】6016

来源:互联网 发布:手机能安装windows xp 编辑:程序博客网 时间:2024/05/23 12:01

题目链接:点击打开链接

Count the Sheep

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


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
 

Source
BestCoder Round #92



题意:

问题描述
开学翘课固然快乐,然而也有让呃喵抓狂的事,那当然就是考试了!这可急坏了既要翘课又想要打BC还要准备考试的呃喵。呃喵为了准备考试没有时间刷题,想打BC又不想跌分,只得求助于BCround92的出题人snowy_smile,让他说点什么 ~~>_<~~。snowy_smile实在没有办法,但是又不好意思透题,只好告诉呃喵,当务之急是好好休息。"如果你按照下面这个办法睡着,那么第二天就绝对不会在BC的赛场上跌分——想象一片一望无际、广阔无边的青青草原,草原上住着一群羊,包括n只沉默的男羊和m只流泪的女羊,在男羊和女羊之间,存在k个朋友关系。现在你可以以任意一只羊为起点,顺着朋友关系数下去。如果能够连续数4只各不相同的羊,就能超过99%的数羊者,成功入睡。"呃喵听后十分震惊,但她还是听话地数下去,果然,数到第4只羊就睡着了,并一口气睡过了头,成功地错过了第二天的BestCoder,真的不会在BC的赛场上跌分啦!然而你,可就没有这么好的运气了,你既然看到了这第二题,自然一般已有提交,已经无法回头了。面对"不AC这题就可能跌分"窘境的你,需要说出,呃喵在睡前可能有多少种不同的数羊序列。即输出"A-B-C-D"这样序列的方案数,满足A-B、B-C、C-D是朋友关系且A、B、C、D各不相同。
输入描述
第一行输入数据组数T对于每组数据,第一行有三个整数n, m, k,表示n只男羊编号分别为1~n,m只女羊编号分别为1~m,并且存在k个朋友关系。接下来给出k行,每行给出两个数x y,表示第x只男羊和第y只女羊是朋友。数据保证——不会给出重复的朋友关系1 <= T <= 1000对于30%的数据,1 <= n, m, k <= 100对于99%的数据,1 <= n, m, k <= 1000对于100%的数据,1 <= n, m, k <= 100000
输出描述
对于每组数据,输出一行,该行包含一个整数,表示呃喵睡觉前可能数了哪4只羊的序列的方案数。
输入样例
(为了方便阅读,样例输入中数据组间中会额外添加一行空行)32 2 41 11 22 12 23 1 31 12 13 13 3 31 12 12 2
输出样例
802
Hint
第一组样例:(b1-g1-b2-g2) (b1-g2-b2-g1) (b2-g1-b1-g2) (b2-g2-b1-g1) (g1-b1-g2-b2) (g1-b2-g2-b1) (g2-b1-g1-b2) (g2-b2-g1-b1) 共8种合法序列



第一想法就是直接dfs爆搜,结果还是被hack了。

后来看队友的思路,在这个二分图中,我们枚举每一段男女关系,然后由男的延伸出一个女的,女的延伸出一个男的,这样就可以组成一个四人组了。

最后的结果乘二就行。


代码如下:

#include <cstdio>#include <cstring>#include <queue>#include <cmath>#include <stack>#include <vector>#include <algorithm>using namespace std;#define INF 0x3f3f3f3f#define CLR(a,b) memset(a,b,sizeof(a))#define PI acos(-1.0)#define LL long longint ax[100000+5],ay[100000+5];struct node{int x,y;}d[100000+5];int main(){int u;int n,m,k;int x,y;scanf ("%d",&u);while (u--){scanf ("%d %d %d",&n,&m,&k);CLR(ax,0);CLR(ay,0);for (int i = 1 ; i <= k ; i++){scanf ("%d%d",&x,&y);ax[x]++;ay[y]++;d[i].x = x;d[i].y = y;}LL ans = 0;for (int i = 1 ; i <= k ;i++)ans += (LL)(ax[d[i].x]-1) * (ay[d[i].y]-1);printf ("%lld\n",ans<<1);}return 0;}


0 0