CodeForces - 320B Ping-Pong (Easy Version)
题目最开始 完全不懂 配合案例也看不懂-_-
总之就是用传递性 问能否从a区间到b区间
dfs(x,y) 走遍与第x区间所有的 联通区间 最后检验 第y区是否被访问过
是一道搜索好题 搜索还需加强
#include <iostream>
#include <stdio.h>
#include <string.h> using namespace std; typedef long long ll;
typedef pair<ll,ll> P;
int N,n = ;//record how many intervals
bool vis[];
P p[]; bool check(int x, int y)
{
return ( (p[x].first < p[y].second && p[x].first > p[y].first) || (p[x].second < p[y].second && p[x].second > p[y].first) );
} void dfs(int x, int y)//x-->>start interval; y-->>end interval 把所有与x联通的区间都走一遍
{
//if(x == n) return false;//
if ( vis[x] || vis[y] ) return ;
vis[x] = true;//经过了x
for (int i = ; i < n; i++)
{
if (vis[i]) continue;//hes been visited
if ( check(x, i) )//之前版本 if (dfs(x,i) && dfs(i,Y) return true;//每次进函数都会找 x连通的
{
dfs(i, y);
} //不需要dfs(x,i) check即可 因为 跳转到dfs(i, y ) 小看了dfs()想多了
}
return ;//最终只需要检查 vis[b]即可 ->b是否被走过
}
int main()
{
int query, a, b;
freopen("in.txt", "r", stdin);
scanf("%d", &N);
for (int i = ; i < N; i++)
{
scanf("%d%d%d", &query, &a, &b);
if (query == )
{
p[n].first = a;
p[n].second = b;
n++;
}
else if (query == )
{
memset(vis, , sizeof(vis));
dfs(a-, b-);
if (vis[b-]) printf("YES\n");
else printf("NO\n");
}
}
return ;
}
同样的 这道题 用bfs也可以做
理解一下 dfs和bfs的区别
dfs-->> 一条路走到黑 知道无法走 然后再走另外一条路
如果 正确结果在最后一个分支 那么相当于就是把所有可能都遍历了一遍
bfs-->> 每条路都先走第一步 到下一个点后又走下一层的 所有第一步 这样每一次都想外扩展
因为每次都是走第一步 每一个节点都向下一层 扩展一层所以可以找到 最短路径
bfs :
#include <iostream>
#include <stdio.h>
#include <string.h>
#include <queue>
//bfs解F题
using namespace std; typedef pair<long long, long long> P;
P interval[];
int T, num = ;
bool vis[]; bool check(int x, int y)
{
if ( interval[x].first > interval[y].first && interval[x].first < interval[y].second || interval[x].second > interval[y].first && interval[x].second < interval[y].second)
return true;
return false;
} bool bfs(int x, int y)
{
queue<int> que;
que.push(x);
while(!que.empty())
{
int tx = que.front();
P crt = interval[tx];
que.pop();
if (vis[tx]) continue;
vis[tx] = true;
if ( check(tx, y) ) return true;
for (int i = ; i < num; i++)
{
if (check(tx, i) && !vis[i])
{
que.push(i);
}
}
}
return false; } int main()
{
freopen("in.txt", "r", stdin);
scanf("%d", &T);
int cas, a, b;
while (T--)
{
memset(vis, , sizeof(vis));
scanf("%d%d%d", &cas, &a, &b);
if(cas == )
{
interval[num].first = a;
interval[num].second = b;
num++;
}
else
{
if (bfs(a-, b-)) printf("YES\n");
else printf("NO\n");
}
}
return ;
}
CodeForces - 320B Ping-Pong (Easy Version)的更多相关文章
- codeforces Equalizing by Division (easy version)
output standard output The only difference between easy and hard versions is the number of elements ...
- Codeforces 1118F1 Tree Cutting (Easy Version) (简单树形DP)
<题目链接> 题目大意: 给定一棵树,树上的点有0,1,2三中情况,0代表该点无色.现在需要你将这棵树割掉一些边,使得割掉每条边分割成的两部分均最多只含有一种颜色的点,即分割后的两部分不能 ...
- Codeforces 1296E1 - String Coloring (easy version)
题目大意: 给定一段长度为n的字符串s 你需要给每个字符进行涂色,然后相邻的不同色的字符可以进行交换 需要保证涂色后能通过相邻交换把这个字符串按照字典序排序(a~z) 你只有两种颜色可以用来涂 问是否 ...
- Codeforces Round #540 (Div. 3) F1. Tree Cutting (Easy Version) 【DFS】
任意门:http://codeforces.com/contest/1118/problem/F1 F1. Tree Cutting (Easy Version) time limit per tes ...
- Codeforces Round #540 (Div. 3) D1. Coffee and Coursework (Easy version) 【贪心】
任意门:http://codeforces.com/contest/1118/problem/D1 D1. Coffee and Coursework (Easy version) time limi ...
- Codeforces Round #521 (Div. 3) F1. Pictures with Kittens (easy version)
F1. Pictures with Kittens (easy version) 题目链接:https://codeforces.com/contest/1077/problem/F1 题意: 给出n ...
- Codeforces 1077F1 Pictures with Kittens (easy version)(DP)
题目链接:Pictures with Kittens (easy version) 题意:给定n长度的数字序列ai,求从中选出x个满足任意k长度区间都至少有一个被选到的最大和. 题解:$dp[i][j ...
- Codeforces Round #599 (Div. 2) B1. Character Swap (Easy Version) 水题
B1. Character Swap (Easy Version) This problem is different from the hard version. In this version U ...
- Codeforces Round #575 (Div. 3) D1+D2. RGB Substring (easy version) D2. RGB Substring (hard version) (思维,枚举,前缀和)
D1. RGB Substring (easy version) time limit per test2 seconds memory limit per test256 megabytes inp ...
- Codeforces Round #622(Div 2) C1. Skyscrapers (easy version)
题目链接: C1. Skyscrapers (easy version) 题目描述: 有一行数,使得整个序列满足 先递增在递减(或者只递增,或者只递减) ,每个位置上的数可以改变,但是最大不能超过原来 ...
随机推荐
- AJPFX关于StringBuffer,StringBuilder类 总结(一)
StringBuffer,StringBuilder类 StringBuffer是同步的,数据安全,效率低;StringBuilder是不同步的,数据不安全,效率高 StringBuffer:概述1) ...
- Spring数据访问1 - 数据源配置及数据库连接池的概念
无论你要选择哪种数据访问方式,首先你都需要配置好数据源引用. Spring中配置数据源的几种方式 通过在JDBC驱动程序定义的数据源: 通过JNDI查找的数据源: 连接池的数据源: 对于即将发布到生产 ...
- iOS应用版本更新(自动提醒用户更新代码)
在#import "AppDelegate.h" 文件中的application:(UIApplication *)application didFinishLaunchingWi ...
- Eclipse被卡死了或者失去响应了后分析根源的一个小技巧
提升程序员工作效率的工具/技巧推荐系列 推荐一个功能强大的文件搜索工具SearchMyFiles 介绍一个好用的免费流程图和UML绘制软件-Diagram Designer 介绍Windows任务管理 ...
- php-PHP Warning: PHP Startup: Invalid library (maybe not a PHP library) 'xxx.so' in Unknown on line 0
关于xxx.so,今天在安装php的模块时候老是报,xxx.so的问题,虽然不影响使用,但作为一名当年的程序员强迫症患者,誓死要把 他搞清楚,后面发现是删除了也没有影响,因为在安装php的时候已经将他 ...
- uva1153 Keep the Customer Satisfied
贪心加优先队列 (默认是小的在前,正好) //这里又很套路,设队列里的都是符合条件的考虑新加入的即可.再处理一下空队列的情况.很完美// 截止时间短的在前面,干的就多先根据截止日期排序优先队列根据完成 ...
- 【前端】pid2children iview tree json
<script> import inBody from '../inBody' export default { components:{ inBody } ,data () { retu ...
- Chrome安装助手踩坑
[前言] 最近用之前的方法配置hosts,想浏览下载国外网站的数据和插件,突然发现几乎所有的方法都无效了...... 本文介绍下下载谷歌助手,通过助手访问国外网站 [主体] (1)搜索谷歌助手,点击下 ...
- CPP-基础:快速排序
快速排序(Quicksort)是对冒泡排序的一种改进. 它的基本思想是:通过一趟排序将要排序的数据分割成独立的两部分,其中一部分的所有数据都比另外一部分的所有数据都要小,然后再按此方法对这两部分数据分 ...
- 【软件构造】第三章第三节 抽象数据型(ADT)
第三章第三节 抽象数据型(ADT) 3-1节研究了“数据类型”及其特性 ; 3-2节研究了方法和操作的“规约”及其特性:在本节中,我们将数据和操作复合起来,构成ADT,学习ADT的核心特征,以及如何设 ...