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总结OpenJDK 和 HashMap大量数据处理时,避免垃圾回收延迟的技巧二
HashMap简史 “Hash Code”这个概念第一次出现是在1953年1月的<Computing literature>中,H. P. Luhn (1896-1964) 在一篇 IB ...
- AJPFX:实现递归统计文件夹的总大小
class Statistical { public static void main(String[] args) { Scanner sc = new Scanner(Syst ...
- CF985D Sand Fortress
思路: 很奇怪的结论题,不好想.参考了http://codeforces.com/blog/entry/59623 实现: #include <bits/stdc++.h> using n ...
- QQ面板拖拽(慕课网DOM事件探秘)(下)
2.鼠标事件坐标获取 function fnDown(event) { var event = event || window.event; var oDrag = document.getEleme ...
- git diff查看修改,出现^M换行问题
通过命令git diff查看修改,出现^M换行问题,如图: 解决: git config --global core.whitespace cr-at-eol 换行符的问题: 提交时转换为LF,检出时 ...
- IOStime处理
对时间处理,在开发时,时常碰到.一般有获取具体的年月日和星期,两个不同时间的差,某一天的前一天或后一天等 .现在只介绍获取具体的年月日和星期,及某一天的前一天或后一天的方法: 对时间的处理一般都会用到 ...
- 【HEVC帧间预测论文】P1.9 Coding Tree Depth Estimation for Complexity Reduction of HEVC
Coding Tree Depth Estimation for Complexity Reduction of HEVC <HEVC标准介绍.HEVC帧间预测论文笔记>系列博客,目录见: ...
- 洛谷 P1454 圣诞夜的极光 == codevs 1293 送给圣诞夜的极光
题目背景 圣诞夜系列~~ 题目描述 圣诞老人回到了北极圣诞区,已经快到12点了.也就是说极光表演要开始了.这里的极光不是极地特有的自然极光景象.而是圣诞老人主持的人造极光. 轰隆隆……烟花响起(来自中 ...
- (2)Ngixn 编译安装设置开机自启
设置nginx开机自启 #!/bin/sh # # nginx - this script starts and stops the nginx daemon # # chkconfig: - 85 ...
- [Redis] 基于redis的分布式锁
前言分布式锁一般有三种实现方式:1. 数据库乐观锁:2. 基于Redis的分布式锁:3. 基于ZooKeeper的分布式锁.本篇博客将介绍第二种方式,基于Redis实现分布式锁. 可靠性首先,为了确保 ...