zoukankan      html  css  js  c++  java
  • Count the Sheep 思维题

    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.

    InputThe 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 <= 100000OutputAs 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

    这道题让我想到了二分图,离散刚学的,就是两边元素之间有边连接,各边元素之间无连接,要找出4个元素满足,A-B,B-C,C-D,注意一下,ABCD满足的话,DCBA也满足,可以考虑分析每一对男女,看看男的有几个女朋友,女的有几个男朋友,各自减一相乘就好了,有点像排列组合,最后再乘2,就ok。


    代码:

    #include <iostream>
    #include <cstdlib>
    #include <cstring>
    #include <cstdio>
    using namespace std;
    
    #define init(a)  memset(a,0,sizeof(a))
    struct rela
    {
        int x,y;
    }re[100001];
    int main()
    {
        int t,n,m,k,x,y;
        long long ans;
        int b[100001],g[100001];
        cin>>t;
        while(t--)
        {
            scanf("%d%d%d",&n,&m,&k);
            init(b);
            init(g);
            ans=0;
            for(int i=0;i<k;i++)
            {
                scanf("%d%d",&x,&y);
                b[x]++;
                g[y]++;
                re[i].x=x,re[i].y=y;
            }
            for(int i=0;i<k;i++)
            {
                ans+=(b[re[i].x]-1)*(g[re[i].y]-1);
            }
            cout<<ans*2<<endl;
        }
    }
  • 相关阅读:
    sqlserver 2000备份文件还原到sqlserver 2005(2008)
    .dll文件有什么用?
    汇编片段
    以POST方式请求数据的Ajax实现方式
    有两个数据据服务器上有两个一样结构的数据库,现想将一服务器上的一数据库里的一个表的一部份记录插入到另一服务器上的一数据库的一表中.
    揭开ASP.NET中Cookie编程的奥秘(2)
    商城网店初步完成了,很多不足
    ajax上传(xmlhttp上传文件突破大小限制)
    查询优化
    金山词霸”屏幕取词技术揭密(讨论稿)
  • 原文地址:https://www.cnblogs.com/8023spz/p/7210480.html
Copyright © 2011-2022 走看看