您的位置:首页 > 其它

hdu 6016 Count the Sheep

2017-02-28 23:10 441 查看

Count the Sheep

Time Limit: 3000/1500 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)

Total Submission(s): 697    Accepted Submission(s): 301


[align=left]Problem Description[/align]
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.
 

[align=left]Input[/align]
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
 

[align=left]Output[/align]
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.
 

[align=left]Sample Input[/align]

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

 

[align=left]Sample Output[/align]

8
0
2

 

题意:

  

想象一片一望无际、广阔无边的青青草原,草原上住着一群羊,包括n只沉默的男羊和m只流泪的女羊,在男羊和女羊之间,存在k个朋友关系。
现在你可以以任意一只羊为起点,顺着朋友关系数下去。如果能够连续数4只各不相同的羊,就能超过99%的数羊者,成功入睡。"
呃喵听后十分震惊,但她还是听话地数下去,果然,数到第4只羊就睡着了,并一口气睡过了头,成功地错过了第二天的BestCoder,真的不会在BC的赛场上跌分啦!
然而你,可就没有这么好的运气了,你既然看到了这第二题,自然一般已有提交,已经无法回头了。
面对"不AC这题就可能跌分"窘境的你,需要说出,呃喵在睡前可能有多少种不同的数羊序列。
即输出"A-B-C-D"这样序列的方案数,满足A-B、B-C、C-D是朋友关系且A、B、C、D各不相同。


思路:

     显然可以按照"女羊A,男羊B,女羊C,男羊D"的方式计数,在最后使得答案*2就好,注意爆int。

     一般思路是用第x个vector保存和男羊x有关系的女羊,用数组保存和女羊y有关系的男羊个数。

     然而看到一段超级优美的代码,大概如下:

代码:

#include <iostream>
#include <cstdio>
#include <cstring>

using namespace std;

const int maxn = 100005;

int a[maxn], b[maxn];
int n1[maxn], n2[maxn];

int main() {
int T;
int i;
int n, m, k;

scanf("%d", &T);
while (T--) {
memset(n1, 0, sizeof(n1));
memset(n2, 0, sizeof(n2));

scanf("%d%d%d", &n, &m, &k);
for (i = 0; i < k; i++) {
scanf("%d%d", &a[i], &b[i]);
n1[a[i]]++;
n2[b[i]]++;
}

long long int ans = 0;
for (i = 0; i < k; i++)
ans += (n1[a[i]] - 1) * (n2[b[i]] - 1);

printf("%lld\n", 2 * ans);

}
return 0;
}

用四个数组保存了对应的关系,然后按照读入的顺序再访问一次用来代替原来的vector的读取。

挺漂亮的代码,还是要学习一个。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: