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

原创 2017年09月09日 14:13:21
/*
	本题收获:
	1. map<int, vector<int> > 的插入
	格式如下:
	typedef map<vector<string>,int>  mymap;  
	mymap m; 
	m.insert(pair<vector<string>,int>(vs,100)); //pair<vector<string>,int>可简写为 make_pair
	
	有关博客:
	http://blog.csdn.net/jiejinquanil/article/details/51204974
	
	2. vector的排序和去重
	去重之前需要先排序!因为unique和erase一起用的作用,仅仅是删除相邻的重复元素,所以必须先进行排序
	
	核心代码:
	vector<int>v;
	sort(v.begin(), v.end());
	v.erase(unique(v.begin(), v.end()), v.end());
	
	------------------------------------------
	同时,之前学过的坐标离散化,也用到了vector的排序和去重
	http://blog.csdn.net/mofushaohua_ln/article/details/77795409
	
*/


A. Banana


/*
	题目链接:https://www.jisuanke.com/contest/870/43626
  	2017 ACM-ICPC 亚洲区(乌鲁木齐赛区) A. Banana
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
*/

#include <iostream>
#include <map>
#include <vector>
#include <algorithm>
using namespace std;
map<int, vector<int> > banana; // key: number of monkeys, value: type of bananas;
map<int, vector<int> > location; // key: type of bananas; value: location of bananas;


int main()
{
	int t, n, m, x, y;
	cin >> t;
	while (t--)
	{
		banana.clear();
		location.clear();
		cin >> n >> m;
		for (int i = 0; i < n; i++)
		{
			cin >> x >> y;
			if (banana.count(x) == 0)
			{
				vector<int> tp;
				tp.push_back(y);
				banana.insert(make_pair(x, tp));
			}
			else
			{
				vector<int> tp = banana[x];
				tp.push_back(y);
				banana[x] = tp;
			}			
		}
		
		for (int i = 0; i < m; i++)
		{
			cin >> x >> y;
			if (location.count(x) == 0)
			{
				vector<int> tp;
				tp.push_back(y);
				location.insert(make_pair(x, tp));
			}
			else
			{
				vector<int> tp = location[x];
				tp.push_back(y);
				location[x] = tp;
			}
		}
		
	
		for (int i = 1; i <= n; i++)
		{
			vector<int>v;
			for (int j = 0; j < banana[i].size(); j++)
			{
				int tp = banana[i][j];
				if (location.count(tp) != 0)
				{
					vector<int> a = location[tp];
					for (int ii = 0; ii < a.size(); ii++)
					v.push_back(a[ii]);
										
				}
			}
			sort(v.begin(), v.end());
			v.erase(unique(v.begin(), v.end()), v.end());
			for (int k = 0; k < v.size(); k++)
					cout << i << " " << v[k] << endl;
		}
		cout << endl;
		
	}
}


版权声明:欢迎大家转载,转载请注明出处: ( http://blog.csdn.net/mofushaohua_ln )

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

Bananas are the favoured food of monkeys.In the forest, there is a Banana Company that provides bana...
  • changjiale110
  • changjiale110
  • 2017年09月09日 13:45
  • 1481

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

2017 ACM-ICPC 亚洲区(乌鲁木齐赛区)网络赛
  • axuhongbo
  • axuhongbo
  • 2017年09月09日 19:01
  • 93

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

A: Banana time limit 200ms memory limit 131072KB Discussion Bananas are the favou...
  • Dorlife
  • Dorlife
  • 2017年09月10日 10:08
  • 291

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

题目来源:Banana 题目描述和输出: Bananas are the favoured food of monkeys. In the forest, there is a Banana C...
  • Enterprise_
  • Enterprise_
  • 2017年09月09日 17:00
  • 118

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

题目:暂时看不到……有的看了再更新题目信息…… 题意:有一堆猴子,每只猴子都有自己喜欢的香蕉类型,然后香蕉会出现在指定的位置,问每只猴子是否能在每个地方吃到自己喜欢的香蕉 解题思路:暴力处理即...
  • lzc504603913
  • lzc504603913
  • 2017年09月09日 20:26
  • 282

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

A: Banana time limit 200ms memory limit 131072KB Bananas are the favoured food of monkeys. In the ...
  • sxy201658506207
  • sxy201658506207
  • 2017年09月09日 18:32
  • 108

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

#include   #include   #define MAX 55 using namespace std;   int main()   {       int t;      ...
  • jiang__shuai
  • jiang__shuai
  • 2017年09月28日 21:35
  • 84

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

每个猴子都会喜欢若干种香蕉,每种香蕉都有自己的产地,输出每个猴子能从哪些产地获取自己喜欢的香蕉 数据范围很小,所以可以暴力标记每个产地数字出现的情况,按升序输出即可。 #i...
  • threeh20
  • threeh20
  • 2017年09月19日 21:28
  • 105

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

//基础题 #include #include using namespace std; const int N = 1e3 + 5; int supply[N]; int cost_day[N]...
  • mofushaohua_ln
  • mofushaohua_ln
  • 2017年09月10日 16:56
  • 140

2017 ACM-ICPC 亚洲区(乌鲁木齐赛区) F Islands

详情请参考HDU2767 完全相同的题目 问加多少边能使这个图变为一个强连通图。缩点后判断入度和出度的个数,求最大值即可 #include #include #include ...
  • Little_boy_z
  • Little_boy_z
  • 2017年09月11日 17:55
  • 74
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:2017 ACM-ICPC 亚洲区(乌鲁木齐赛区) A. Banana
举报原因:
原因补充:

(最多只允许输入30个字)