2017 ACM-ICPC 亚洲区(乌鲁木齐赛区)网络赛-A. Banana
2017-09-11 17:36
417 查看
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 xx should 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
简单的签到题。熟练运用stl容器会好做很多
#include <iostream>
#include <stdio.h>
#include <string.h>
#include<vector>
#include <algorithm>
using namespace std;
vector<int>houzi[110];
vector<int>chandi[110];
struct node
{
int x;
int y;
}shuchu[110];
int n,m;
int a,b;
bool cmp(node t1,node t2)
{
if(t1.x==t2.x)
{
return t1.y<t2.y;
}
return t1.x<t2.x;
}
int main()
{
int t;
cin>>t;
while(t--)
{
cin>>n>>m;
for(int i=1;i<=n;i++)
{
houzi[i].clear();
chandi[i].clear();
}
for(int i=1;i<=n;i++)
{
cin>>a>>b;
houzi[a].push_back(b);
}
for(int i=1;i<=m;i++)
{
cin>>a>>b;
chandi[a].push_back(b);
}
int num=0;
for(int i=1;i<=n;i++)
{
if(houzi[i].size()!=0)
{
for(int j=0;j<houzi[i].size();j++)
{
int u=houzi[i][j];
for(int k=0;k<chandi[u].size();k++)
{
shuchu[num].x=i;
shuchu[num].y=chandi[u][k];
num++;
}
}
}
}
sort(shuchu,shuchu+num,cmp);
node duibi=shuchu[0];
cout<<shuchu[0].x<<" "<<shuchu[0].y<<endl;
for(int i=1;i<num;i++)
{
if(shuchu[i].x==duibi.x&&shuchu[i].y==duibi.y)
{
continue;
}
else
{
cout<<shuchu[i].x<<" "<<shuchu[i].y<<endl;
duibi=shuchu[i];
}
}
}
}
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 xx should 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
简单的签到题。熟练运用stl容器会好做很多
#include <iostream>
#include <stdio.h>
#include <string.h>
#include<vector>
#include <algorithm>
using namespace std;
vector<int>houzi[110];
vector<int>chandi[110];
struct node
{
int x;
int y;
}shuchu[110];
int n,m;
int a,b;
bool cmp(node t1,node t2)
{
if(t1.x==t2.x)
{
return t1.y<t2.y;
}
return t1.x<t2.x;
}
int main()
{
int t;
cin>>t;
while(t--)
{
cin>>n>>m;
for(int i=1;i<=n;i++)
{
houzi[i].clear();
chandi[i].clear();
}
for(int i=1;i<=n;i++)
{
cin>>a>>b;
houzi[a].push_back(b);
}
for(int i=1;i<=m;i++)
{
cin>>a>>b;
chandi[a].push_back(b);
}
int num=0;
for(int i=1;i<=n;i++)
{
if(houzi[i].size()!=0)
{
for(int j=0;j<houzi[i].size();j++)
{
int u=houzi[i][j];
for(int k=0;k<chandi[u].size();k++)
{
shuchu[num].x=i;
shuchu[num].y=chandi[u][k];
num++;
}
}
}
}
sort(shuchu,shuchu+num,cmp);
node duibi=shuchu[0];
cout<<shuchu[0].x<<" "<<shuchu[0].y<<endl;
for(int i=1;i<num;i++)
{
if(shuchu[i].x==duibi.x&&shuchu[i].y==duibi.y)
{
continue;
}
else
{
cout<<shuchu[i].x<<" "<<shuchu[i].y<<endl;
duibi=shuchu[i];
}
}
}
}
相关文章推荐
- 计蒜客-2017 ACM-ICPC 亚洲区(乌鲁木齐赛区)网络赛-A-Banana
- 2017 ACM-ICPC 亚洲区(乌鲁木齐赛区)网络赛---banana
- A. Banana 2017 ACM-ICPC 亚洲区(乌鲁木齐赛区)网络赛
- A. Banana 2017 ACM-ICPC 亚洲区(乌鲁木齐赛区)网络赛
- 2017 ACM-ICPC 亚洲区(乌鲁木齐赛区)网络赛 A. Banana
- 2017 ACM-ICPC 亚洲区(乌鲁木齐赛区)网络赛 A. Banana(签到)
- 2017 ACM-ICPC 亚洲区(乌鲁木齐赛区)网络赛-A. Banana
- 2017 ACM-ICPC 亚洲区(乌鲁木齐赛区)网络赛-A. Banana
- 2017 ACM-ICPC 亚洲区(乌鲁木齐赛区)网络赛 A banana
- 2017 ACM-ICPC 亚洲区(乌鲁木齐赛区)网络赛 A. Banana(连通性水题)
- 计蒜客 16950 Banana(2017 ACM-ICPC 亚洲区(乌鲁木齐赛区)网络赛 A)
- 2017 ACM-ICPC 亚洲区(乌鲁木齐赛区)网络赛- A. Banana
- 2017 ACM-ICPC 亚洲区(乌鲁木齐赛区)网络赛 A: Banana
- 计蒜客 16954 Half-consecutive Numbers(2017 ACM-ICPC 亚洲区(乌鲁木齐赛区)网络赛 E)
- 2017 ACM-ICPC 亚洲区(乌鲁木齐赛区)网络赛 H (简单DP)
- 2017 ACM-ICPC 亚洲区(乌鲁木齐赛区)网络赛 E.Half-consecutive Numbers
- 2017 ACM-ICPC 亚洲区(乌鲁木齐赛区)网络赛
- 2017 ACM-ICPC 亚洲区(乌鲁木齐赛区)网络赛 C. Coconut
- 【2017 ACM-ICPC 亚洲区(乌鲁木齐赛区)网络赛 】H Skiing 【求DAG图的最长路】
- 2017 ACM-ICPC 亚洲区(乌鲁木齐赛区)网络赛 Coconut