2017 ACM-ICPC 亚洲区(乌鲁木齐赛区)网络赛 A. Banana(签到)

                                A: Banana
                time limit 200ms  memory limit 131072KB

Discussion

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 T, indicating that there are T test cases.
For each test case, the first line contains two integers N and M, representing the
length of the first and the second lists respectively.
In the each line of following N lines, two positive integers i, j indicate that the i-th
monkey favours the j-th type of banana.
In the each line of following M lines, two positive integers j, k indicate that the j-th
type of banana could be find in the k-th place.
All integers of the input are less than 50.

Output Format

For each test case, output all the pairs x, y that the x-the monkey can accept at least
one type of bananas from the y-th place.
These pairs should be outputted as ascending order. That is say that a pair of x, y
which owns a smaller x should be output first.
If two pairs own the same x, output the one who has a smaller y first.
And there should be an empty line after each test case.

Sample Input

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

Sample Output

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

题意

一个猴子喜欢吃一种或多种香蕉,一个香蕉可以生长在多个地方,问一个猴子可以在那些地方吃到喜欢的香蕉
输出按照升序

思路

因为一个地方可以有多种香蕉,一个猴子可以喜欢多种香蕉
所以以香蕉来记录。具体看代码。

代码

#include 
#include 
#include 
#include 
#include 
using namespace std;
const int maxn = 55;
vector<int >G[maxn];
int map[maxn][maxn];
int n, m, x, y, z;
int main()
{
    int t;
    scanf("%d",&t);
    while(t--)
    {
        for(int i=0;i<=50;i++)
            G[i].clear();
        memset(map,0,sizeof(map));
        scanf("%d %d",&n, &m);
        for(int i = 0; i < n; i ++)
        {
            scanf("%d %d",&x, &y);
            G[y].push_back(x);
        }
        for(int i = 0; i < m; i ++)
        {
            scanf("%d %d",&y, &z);
            for(unsigned int j = 0; j < G[y].size(); j ++)
                map[G[y][j]][z] = 1;
        }
        for(int i = 1; i <= 50; i ++)
        {
            for(int j = 1; j <= 50; j ++)
                if(map[i][j])
                    printf("%d %d\n",i,j);
        }
        printf("\n");
    }
}

你可能感兴趣的:(比赛)