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·的更多相关文章

  1. HDU 4046 Panda (ACM ICPC 2011北京赛区网络赛)

    HDU 4046 Panda (ACM ICPC 2011北京赛区网络赛) Panda Time Limit: 10000/4000 MS (Java/Others)    Memory Limit: ...

  2. Skiing 2017 ACM-ICPC 亚洲区(乌鲁木齐赛区)网络赛H题(拓扑序求有向图最长路)

    参考博客(感谢博主):http://blog.csdn.net/yo_bc/article/details/77917288 题意: 给定一个有向无环图,求该图的最长路. 思路: 由于是有向无环图,所 ...

  3. 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 ...

  4. 2016 ACM/ICPC亚洲区青岛站现场赛(部分题解)

    摘要 本文主要列举并求解了2016 ACM/ICPC亚洲区青岛站现场赛的部分真题,着重介绍了各个题目的解题思路,结合详细的AC代码,意在熟悉青岛赛区的出题策略,以备战2018青岛站现场赛. HDU 5 ...

  5. ICPC 2018 徐州赛区网络赛

    ACM-ICPC 2018 徐州赛区网络赛  去年博客记录过这场比赛经历:该死的水题  一年过去了,不被水题卡了,但难题也没多做几道.水平微微有点长进.     D. Easy Math 题意:   ...

  6. 【2017 ACM/ICPC 乌鲁木齐赛区网络赛环境测试赛 E】蒜头君的排序

    [链接]h在这里写链接 [题意] 在这里写题意 [题解] 莫队算法+树状数组. 区间增加1或减少1. 对逆序对的影响是固定的. (用冒泡排序变成升序的交换次数,就是逆序对的个数) [错的次数] 0 [ ...

  7. 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 ...

  8. [刷题]ACM/ICPC 2016北京赛站网络赛 第1题 第3题

    第一次玩ACM...有点小紧张小兴奋.这题目好难啊,只是网赛就这么难...只把最简单的两题做出来了. 题目1: 代码: //#define _ACM_ #include<iostream> ...

  9. 2016 ACM/ICPC亚洲区大连站-重现赛 解题报告

    任意门:http://acm.hdu.edu.cn/showproblem.php?pid=5979 按AC顺序: I - Convex Time limit    1000 ms Memory li ...

  10. 2014ACM/ICPC亚洲区鞍山赛区现场赛1009Osu!

    鞍山的签到题,求两点之间的距离除以时间的最大值.直接暴力过的. A - Osu! Time Limit:1000MS     Memory Limit:262144KB     64bit IO Fo ...

随机推荐

  1. spring data jpa 遇到的问题

    org.springframework.core.convert.ConversionFailedException: Failed to convert from type [java.lang.O ...

  2. java中 synchronized 的使用,确保异步执行某一段代码。

    最近看了个有关访问网络url和下载的例子,里面有几个synchronized的地方,系统学习下,以下内容很重要,记下来. Java语言的关键字,当它用来修饰一个方法或者一个代码块的时候,能够保证在同一 ...

  3. django将数据库中数据直接传到html

    1.当然,前提是建立和配置好django数据库啦~ 2.在python后台函数中引入需要的表 #要读取home这个APP下的models.py文件,引入其中的Student_message_unedi ...

  4. PHP归档phar性能測试

    PHP自从5.3后新增PHAR归档,Phar 归档的概念来自 Java™ 技术的 JAR 归档,它同意使用单个文件打包应用程序.这个文件里包括运行应用程序所需的全部东西.该文件不同于单个可运行文件,后 ...

  5. js-jquery-002-条形码-一维码

    一.使用 官方地址:http://barcode-coder.com/en/barcode-jquery-plugin-201.html 1.js引用 <script type="te ...

  6. java-mybaits-00401-Mapper-输入输出

    Mapper.xml映射文件中定义了操作数据库的sql,每个sql是一个statement,映射文件是mybatis的核心. 1.parameterType(输入类型) 1.1.#{}与${} #{} ...

  7. abap编辑器中代码不可修改

    当出现abap编辑器中代码不能修改的情况,可以按如下设置: edit——> 修改操作——>关闭助手

  8. win下如何解决在chrome的同源访问问题

    引子:本来是想验证如果在网页中包含多个框架,那么就会存在两个以上的不同全局环境,如果从一个框架引用另一个框架的数据比如数组a,那么用 instanceof 判断这个数组a是不是另个框架Array的实例 ...

  9. C# DataTable Column DataType 对应 数据库

      public DataTable MakeDataTable(){ DataTable myTable; DataRow myNewRow; // Create a new DataTable. ...

  10. 003-spring boot项目的项目属性配置

    一.application.properties文件. 1.项目的配置文件内容.配置了端口,超时连接时间, 2.控制器. 3.访问. 二.application.yml文件 1.application ...