首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >HDU 6016 Count the Sheep

HDU 6016 Count the Sheep

作者头像
ShenduCC
发布2018-04-27 11:24:25
4890
发布2018-04-27 11:24:25
举报
文章被收录于专栏:算法修养算法修养

Count the Sheep

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

Total Submission(s): 686    Accepted Submission(s): 295

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找规律的题目先算出来开端是男孩是情况ans,最后的结果是ans*2四个关系 男-女-男-女
就像一颗只有四层的树,最后一层叶子节点的个数就是答案#include <iostream>
#include <string.h>
#include <stdlib.h>
#include <math.h>
#include <stdio.h>
#include <string>
#include <vector>

using namespace std;
typedef long long int LL;
const int maxn=1e5;
int n,m,k;
vector<int> a[maxn+5];
vector<int> b[maxn+5];
int aa[maxn+5];
int bb[maxn+5];
int main()
{
    int t;
    scanf("%d",&t);
    while(t--)
    {
        scanf("%d%d%d",&n,&m,&k);
        int x,y;
        for(int i=1;i<=n;i++)
            a[i].clear();
        for(int i=1;i<=m;i++)
            b[i].clear();
        memset(aa,0,sizeof(aa));
        memset(bb,0,sizeof(bb));
        for(int i=1;i<=k;i++)
        {
            scanf("%d%d",&x,&y);
            a[x].push_back(y);
            b[y].push_back(x);
            aa[x]++;
            bb[y]++;
        }
        LL ans=0;
        for(int i=1;i<=n;i++)
        {
            LL res=0;
            for(int j=0;j<aa[i];j++)
            {
                res+=(bb[a[i][j]]-1);
            }
            ans+=res*(aa[i]-1);
        }
        printf("%lld\n",ans*2);
    }
    return 0;
}
本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2017-02-28 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • Count the Sheep
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档