zoukankan      html  css  js  c++  java
  • 2017 ACM-ICPC 亚洲区(乌鲁木齐赛区)网络赛-A banana·

    2017-09-09 16:41:28

    writer:pprp

    题意很好理解就不说了,实现比较清晰,选择邻接表来做

    但是我用的是链表来实现的,所以导致出现了很多问题,最后卡的最长时间的一个问题是

    应该从1开始而不是从0开始,读题应该自习一点;

    题目如下:

    Bananas are the favoured food of monkeys.

    In the forest, there is a Banana Company that provides bananas from different places.

    The company has two lists.

    The first list records the types of bananas preferred by different monkeys, and the second one records the types of bananas from different places.

    Now, the supplier wants to know, whether a monkey can accept at least one type of bananas from a place.

    Remenber that, there could be more than one types of bananas from a place, and there also could be more than one types of bananas of a monkey's preference.

    Input Format

    The first line contains an integer TT, indicating that there are TT test cases.

    For each test case, the first line contains two integers NN and MM, representing the length of the first and the second lists respectively.

    In the each line of following NN lines, two positive integers i, ji,j indicate that the ii-th monkey favours the jj-th type of banana.

    In the each line of following MM lines, two positive integers j, kj,k indicate that the jj-th type of banana could be find in the kk-th place.

    All integers of the input are less than or equal to 5050.

    Output Format

    For each test case, output all the pairs x, yx,y that the xx-the monkey can accept at least one type of bananas from the yy-th place.

    These pairs should be outputted as ascending order. That is say that a pair of x, yx,y which owns a smaller xxshould be output first.

    If two pairs own the same xx, output the one who has a smaller yy first.

    And there should be an empty line after each test case.

    样例输入

    1
    6 4
    1 1
    1 2
    2 1
    2 3
    3 3
    4 1
    1 1
    1 3
    2 2
    3 3

    样例输出

    1 1
    1 2
    1 3
    2 1
    2 3
    3 3
    4 1
    4 3

    代码如下;

    #include <iostream>
    #include <cstring>
    #include <algorithm>
    #include <cstdio>
    
    using namespace std;
    const int maxn = 55;
    int N, M;
    
    struct node
    {
        int val;
        node * next;
    };
    
    node* head1[maxn];
    node* head2[maxn];
    
    bool ans[maxn][maxn];
    
    void create()
    {
        memset(ans,0,sizeof(ans));
        for(int i = 1 ; i < maxn ; i++)
            head1[i] = NULL,head2[i] = NULL;
        int st, ed;
        for(int i = 1 ; i <= N ; i++)
        {
            cin >> st >> ed;
            node *tmp = new node();
            tmp->next = NULL;
            if(head1[st] == NULL)
            {
                head1[st] = tmp;
                tmp->val = ed;
            }
            else
            {
                node * p = head1[st];
                while(p->next != NULL)
                    p = p->next;
                p->next = tmp;
                tmp->val = ed;
            }
        }
        for(int i = 1 ; i <= M ; i++)
        {
            cin >> st >> ed;
            node * tmp = new node();
            tmp->next = NULL;
            if(head2[st] == NULL)
            {
                head2[st] = tmp;
                tmp->val = ed;
            }
            else
            {
                node * p = head2[st];
                while(p->next != NULL)
                {
                    p = p->next;
                }
                p->next = tmp;
                tmp->val = ed;
            }
        }
    }
    
    void fun()
    {
        node * p, * q;
        int rec_st, rec_ed;
        for(int i = 1 ; i <= N ; i++)
        {
            p = head1[i];
            while(p != NULL)
            {
                rec_st = p->val;
                q = head2[rec_st];
                while(q != NULL)
                {
                    rec_ed = q->val;
                    ans[i][rec_ed] = true;
                    q = q->next;
                }
                p = p->next;
            }
        }
    }
    
    int main()
    {
        //freopen("in.txt","r",stdin);
        int cas;
        cin >> cas;
        while(cas--)
        {
            cin >> N >> M;
            create();
            fun();
            for(int i = 1; i < maxn ; i++)
            {
                for(int j = 1 ; j < maxn ; j++)
                {
                    if(ans[i][j] == true)
                        cout << i << " " << j << endl;
                }
            }
            cout << endl;
        }
    
        return 0;
    }

     

  • 相关阅读:
    将配置文件自动复制到vs的测试项目中
    用索引器简化的C#类型信息访问
    Requested Clipboard operation did not succeed的解决办法
    在win2003上IIS部署可能出现的问题的解决方案
    Login failed for user 'IIS APPPOOL\ASP.NET v4.0'.
    在Ubuntu虚拟机中配置bridge共享上网
    Ajax学习日志
    Workflow architecture in Windows SharePoint Services (version 3):WWF和WSS V3 的关系 无为而为
    使用BizTalk的必须关注:HWS已经死了,微软已经放弃HWS了,估计替代产品就WWF。(外加其它的宣告死亡的工具和API列表) 无为而为
    我想要求主管给我们升级电脑,但是主管不肯,大家报一报公司电脑的配置,我看我的电脑是不是最差的,顺便谈谈你们公司电脑硬件升级策略 无为而为
  • 原文地址:https://www.cnblogs.com/pprp/p/7498641.html
Copyright © 2011-2022 走看看