zoukankan      html  css  js  c++  java
  • ZOJ 3601 Unrequited Love 浙江省第九届省赛

    Unrequited Love

    Time Limit: 16 Seconds      Memory Limit: 131072 KB

    There are n single boys and m single girls. Each of them may love none, one or several of other people unrequitedly and one-sidedly. For the coming q days, each night some of them will come together to hold a single party. In the party, if someone loves all the others, but is not loved by anyone, then he/she is called king/queen of unrequited love.

    Input

    There are multiple test cases. The first line of the input is an integer T ≈ 50 indicating the number of test cases.

    Each test case starts with three positive integers no more than 30000 -- n m q. Then each of the next n lines describes a boy, and each of the next m lines describes a girl. Each line consists of the name, the number of unrequitedly loved people, and the list of these people's names. Each of the last q lines describes a single party. It consists of the number of people who attend this party and their names. All people have different names whose lengths are no more than 20. But there are no restrictions that all of them are heterosexuals.

    Output

    For each query, print the number of kings/queens of unrequited love, followed by their names in lexicographical order, separated by a space. Print an empty line after each test case. See sample for more details.

    Sample Input

    2
    2 1 4
    BoyA 1 GirlC
    BoyB 1 GirlC
    GirlC 1 BoyA
    2 BoyA BoyB
    2 BoyA GirlC
    2 BoyB GirlC
    3 BoyA BoyB GirlC
    2 2 2
    H 2 O S
    He 0
    O 1 H
    S 1 H
    3 H O S
    4 H He O S
    

    Sample Output

    0
    0
    1 BoyB
    0
    
    0
    0
    
    看的人家的代码算是理解了,我是直接比较的明显要超时(n^2),换个思路后时间复杂度变为(2*n)代码:
     1 #include<iostream>
     2 #include<algorithm>
     3 #include<cstdio>
     4 #include<map>
     5 #include<set>
     6 using namespace std;
     7 char s1[30],s2[30],s3[30];
     8 int main()
     9 {
    10    int T;
    11    int n,m,q,n0;
    12    scanf("%d",&T);
    13    while(T--)
    14    {
    15        string lovy,lovyed;
    16        map< pair<string,string> ,int >love;
    17        love.clear();
    18        scanf("%d %d %d",&n,&m,&q);
    19        lovy = lovyed = "";
    20        while(n--)
    21        {
    22         scanf("%s %d",s1,&n0);
    23            lovy = s1;
    24            while(n0--)
    25            {
    26                scanf("%s",s2);
    27                lovyed = s2;
    28                love[make_pair(lovy,lovyed)] = 1;
    29            }
    30            lovy = lovyed = "";
    31        }
    32 
    33        while(m--)
    34        {
    35           scanf("%s %d",s1,&n0);
    36            lovy = s1;
    37            while(n0--)
    38            {
    39                scanf("%s",s2);
    40                lovyed = s2;
    41                love[make_pair(lovy,lovyed)] = 1;
    42            }
    43         lovy = lovyed = "";
    44        }
    45        while(q--)
    46        {   bool flag = true;
    47            int k =0;
    48            string mm[30010];
    49            scanf("%d",&n0);
    50            scanf("%s",s1);
    51            mm[0]=lovy = s1;
    52            for(int i=1;i<n0;i++)
    53            {
    54              scanf("%s",s1);
    55              lovyed = mm[i] = s1;
    56              //关键在于下面的判断
    57              if(love[make_pair(lovy,lovyed)] == 0|| love[make_pair(lovyed,lovy)] == 1)
    58              {
    59                     lovy = mm[i];
    60                     k = i;
    61              }
    62            }
    63            for(int j=0;j<k;j++)
    64            {
    65              if(lovy != mm[j])
    66              {
    67                if(love[make_pair(lovy,mm[j])] == 0 || love[make_pair(mm[j],lovy)] == 1)
    68                   {
    69                                     flag =  false;
    70                   }
    71              }
    72            }
    73            if(flag)
    74            printf("1 %s
    ",lovy.c_str());
    75                else printf("0
    ");
    76        }
    77      printf("
    ");
    78    }
    79   return 0;
    80 }
  • 相关阅读:
    十个能让你成为牛逼前端程序猿的特征
    一道Javascript面试题引发的血案
    程序员实现财务自由的9个阶段,你达到了哪一段?
    程序员进阶路上不能错过的史上最全技术知识图谱秘籍
    清华大学研发神技能:用意念回复微信
    机器学习原来如此有趣:用深度学习识别人脸
    【代码片段】如何使用CSS来快速定义多彩光标
    Android自定义一款带进度条的精美按键
    现在的人工智能逆天到什么地步了?
    分享几套生成iMac相关高逼格免费mockup的素材和在线工具
  • 原文地址:https://www.cnblogs.com/lovychen/p/4472494.html
Copyright © 2011-2022 走看看