2017 ACM-ICPC 亚洲区(乌鲁木齐赛区)网络赛-A banana·

Posted pprp

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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;
}

 

以上是关于2017 ACM-ICPC 亚洲区(乌鲁木齐赛区)网络赛-A banana·的主要内容,如果未能解决你的问题,请参考以下文章

C. Coconut(2017 ACM-ICPC 亚洲区(乌鲁木齐赛区)网络赛)

2017 ACM-ICPC 亚洲区(乌鲁木齐赛区)网络赛-A banana·

2017 ACM-ICPC 亚洲区(乌鲁木齐赛区)网络赛 H. Skiing (拓扑排序+假dp)

2017 ACM-ICPC 亚洲区(乌鲁木齐赛区)网络赛 H. Skiing

2017 ACM-ICPC 亚洲区(南宁赛区)网络赛 B题

2017 ACM-ICPC 亚洲区(青岛赛区)网络赛 1001