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 = ;
int N, M; struct node
{
int val;
node * next;
}; node* head1[maxn];
node* head2[maxn]; bool ans[maxn][maxn]; void create()
{
memset(ans,,sizeof(ans));
for(int i = ; i < maxn ; i++)
head1[i] = NULL,head2[i] = NULL;
int st, ed;
for(int i = ; 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 = ; 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 = ; 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 = ; i < maxn ; i++)
{
for(int j = ; j < maxn ; j++)
{
if(ans[i][j] == true)
cout << i << " " << j << endl;
}
}
cout << endl;
} return ;
}
2017 ACM-ICPC 亚洲区(乌鲁木齐赛区)网络赛-A banana·的更多相关文章
- HDU 4046 Panda (ACM ICPC 2011北京赛区网络赛)
HDU 4046 Panda (ACM ICPC 2011北京赛区网络赛) Panda Time Limit: 10000/4000 MS (Java/Others) Memory Limit: ...
- Skiing 2017 ACM-ICPC 亚洲区(乌鲁木齐赛区)网络赛H题(拓扑序求有向图最长路)
参考博客(感谢博主):http://blog.csdn.net/yo_bc/article/details/77917288 题意: 给定一个有向无环图,求该图的最长路. 思路: 由于是有向无环图,所 ...
- 2017 ACM-ICPC 亚洲区(南宁赛区)网络赛 M. Frequent Subsets Problem【状态压缩】
2017 ACM-ICPC 亚洲区(南宁赛区)网络赛 M. Frequent Subsets Problem 题意:给定N和α还有M个U={1,2,3,...N}的子集,求子集X个数,X满足:X是U ...
- 2016 ACM/ICPC亚洲区青岛站现场赛(部分题解)
摘要 本文主要列举并求解了2016 ACM/ICPC亚洲区青岛站现场赛的部分真题,着重介绍了各个题目的解题思路,结合详细的AC代码,意在熟悉青岛赛区的出题策略,以备战2018青岛站现场赛. HDU 5 ...
- ICPC 2018 徐州赛区网络赛
ACM-ICPC 2018 徐州赛区网络赛 去年博客记录过这场比赛经历:该死的水题 一年过去了,不被水题卡了,但难题也没多做几道.水平微微有点长进. D. Easy Math 题意: ...
- 【2017 ACM/ICPC 乌鲁木齐赛区网络赛环境测试赛 E】蒜头君的排序
[链接]h在这里写链接 [题意] 在这里写题意 [题解] 莫队算法+树状数组. 区间增加1或减少1. 对逆序对的影响是固定的. (用冒泡排序变成升序的交换次数,就是逆序对的个数) [错的次数] 0 [ ...
- 2017 乌鲁木齐赛区网络赛 J Our Journey of Dalian Ends 费用流
题目描述: Life is a journey, and the road we travel has twists and turns, which sometimes lead us to une ...
- [刷题]ACM/ICPC 2016北京赛站网络赛 第1题 第3题
第一次玩ACM...有点小紧张小兴奋.这题目好难啊,只是网赛就这么难...只把最简单的两题做出来了. 题目1: 代码: //#define _ACM_ #include<iostream> ...
- 2016 ACM/ICPC亚洲区大连站-重现赛 解题报告
任意门:http://acm.hdu.edu.cn/showproblem.php?pid=5979 按AC顺序: I - Convex Time limit 1000 ms Memory li ...
- 2014ACM/ICPC亚洲区鞍山赛区现场赛1009Osu!
鞍山的签到题,求两点之间的距离除以时间的最大值.直接暴力过的. A - Osu! Time Limit:1000MS Memory Limit:262144KB 64bit IO Fo ...
随机推荐
- Spark源码分析 – SchedulerBackend
SchedulerBackend, 两个任务, 申请资源和task执行和管理 对于SparkDeploySchedulerBackend, 基于actor模式, 主要就是启动和管理两个actor De ...
- 设计模式之Singleton模式
当程序运行时,有时会希望在程序中,只能存在一个实例,为了达到目的,所以设计了Singleton模式,即单例模式. 单例模式的特征: 想确保任何情况下只存在一个实例 想在程序上表现出只存在一个实例 示例 ...
- 针对Quant的Python快速入门指南
作者:用Python的交易员 (原创文章,转载请注明出处) 最近有越来越多的朋友在知乎或者QQ上问我如何学习入门Python,就目前需求来看,我需要写这么一篇指南. 针对整个vn.py框架的学习,整体 ...
- 为什么说”人生苦短,我用python“?
本文不扯什么大道理,只是先介绍Python的背景,然后从实用的角度出发举一两个真实栗子. 首先要想了解要一门语言的好坏,或者为什么招程序员喜欢(卧槽,原来程序员喜欢不是女朋友?)我们的先从语言的产 ...
- HTML容易遗忘内容(一)
HTML基础语法: <html> <head> <tiltle>Hello</tiltle> </head> <body bgcolo ...
- JSON.parse和JSON.stringify
var json_arr = []; //parse用于从一个字符串中解析出json对象;stringify()用于从一个对象解析出字符串 ...
- 安全篇:弱密码python检测工具
安全篇:弱密码python检测工具 https://github.com/penoxcn/PyWeakPwdAudit
- Elasticsearch.js 发布 —— 在Node.js和浏览器中调用Elasticsearch
继PHP.Ruby.Python和Perl之后,Elasticsearch最近发布了Elasticsearch.js,Elasticsearch的JavaScript客户端库.可以在Node.js和浏 ...
- visual studio 2015开发nodejs教程1搭建环境
http://sailsdoc.swift.ren/ 这里有 sails中文文档 1 安装nodejsv6.10.3 下载地址 https://nodejs.org/dist/v6.10.3/nod ...
- 19. Remove Nth Node From End of List(移除倒数第N的结点, 快慢指针)
Given a linked list, remove the nth node from the end of list and return its head. For example, Give ...