HDU 6016 Count the Sheep【思维】

本文介绍了一个有趣的计数问题,即在给定的公羊和母羊及其好友关系中,寻找从任意一只羊开始,通过好友关系数够四只不同羊的方法数量。通过构建图论模型并使用简单的算法解决这一问题。

题目来戳呀
还不是很理解QAQ

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

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
题意:
这次题目描述真长==
一句话说,有n只公羊和m只母羊,以及k个关系,k个关系为编号为x的公羊和编号为y的母羊是好朋友。问从任意一只羊开始沿着朋友关系数够4只羊的方法有多少种。
想法:
没有想法:)
四个点,连起3条边,我们来研究中间那条边——每次枚举中间那条边,然后该边的俩个顶点的度数-1的乘积就是以该边为中间边的所有方案数。可以把给出的关系构造成一个图来看,从任意一点出发,沿着关系网找到目标。拿第一个样例来说,(为方便观察把母羊编号为3和4)从公羊出发的不同方式为1324,1423,2314,2413共四种,同理,从母羊出发也是四种(把母羊看作1,2,把公羊看作3,4),到这里,我们能看出:从公羊出发的情况乘以二就是答案。

#include <cstdio>
#include <iostream>
#include <algorithm>
#include <cstring>
using namespace std;
const int maxn=100005;
typedef long long ll;
ll b[maxn],bth[maxn],g[maxn],gth[maxn];
int main()
{
    int t;
    int boy,girl,rela;
    ll ans;
    scanf("%d",&t);
    while(t--)
    {
        scanf("%d %d %d",&n,&m,&k);
        memset(b,0,sizeof(b));
        memset(g,0,sizeof(g));
        ans=0;
        for(int i=0;i<k;i++)
        {
            scanf("%lld%lld",&bth[i],&gth[i]);//下标为i的公羊和下标为i的母羊有关系
            b[bth[i]]++;//这只公羊的度自增
            g[gth[i]]++;//这只母羊的度自增
        }
        for(int i=0;i<k;++i)
        {
            ans+=(b[bth[i]]-1)*(g[gth[i]]-1);
        }
        printf("%lld\n",ans*2);
    }
    return 0;
}
/*自己写了一组测试数据
1
2 3 5
1 1
1 3
2 1
2 2
2 3运行结果是12,但我用笔走程序算的是6,所以还是不懂啊~~~~(>_<)~~~~*/
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值