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;
    }

     

  • 相关阅读:
    linux 静态库和动态库(共享库)的制作与使用
    实现linux mkdir命令
    行间距和文本样式
    单位和字体
    html标签2
    css层叠样式表
    html标签
    html简介
    数据数组
    Redis的使用
  • 原文地址:https://www.cnblogs.com/pprp/p/7498641.html
Copyright © 2011-2022 走看看