1. 程式人生 > >2017 ACM-ICPC 亞洲區(烏魯木齊賽區)網絡賽-A banana·

2017 ACM-ICPC 亞洲區(烏魯木齊賽區)網絡賽-A banana·

more ems ans iostream sent -- following ant uid

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·