题目最开始 完全不懂 配合案例也看不懂-_-

总之就是用传递性 问能否从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)的更多相关文章

  1. codeforces Equalizing by Division (easy version)

    output standard output The only difference between easy and hard versions is the number of elements ...

  2. Codeforces 1118F1 Tree Cutting (Easy Version) (简单树形DP)

    <题目链接> 题目大意: 给定一棵树,树上的点有0,1,2三中情况,0代表该点无色.现在需要你将这棵树割掉一些边,使得割掉每条边分割成的两部分均最多只含有一种颜色的点,即分割后的两部分不能 ...

  3. Codeforces 1296E1 - String Coloring (easy version)

    题目大意: 给定一段长度为n的字符串s 你需要给每个字符进行涂色,然后相邻的不同色的字符可以进行交换 需要保证涂色后能通过相邻交换把这个字符串按照字典序排序(a~z) 你只有两种颜色可以用来涂 问是否 ...

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

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

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

  7. Codeforces 1077F1 Pictures with Kittens (easy version)(DP)

    题目链接:Pictures with Kittens (easy version) 题意:给定n长度的数字序列ai,求从中选出x个满足任意k长度区间都至少有一个被选到的最大和. 题解:$dp[i][j ...

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

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

  10. Codeforces Round #622(Div 2) C1. Skyscrapers (easy version)

    题目链接: C1. Skyscrapers (easy version) 题目描述: 有一行数,使得整个序列满足 先递增在递减(或者只递增,或者只递减) ,每个位置上的数可以改变,但是最大不能超过原来 ...

随机推荐

  1. empty 和 isset的区别和联系

    empty 和 isset的区别和联系 要说它们的联系,其共同点就是empty()和isset()都是变量处理函数,作用是判断变量是否已经配置,正是由于它们在处理变量过程中有很大的相似性,才导致对它们 ...

  2. 《Head First HTML与CSS》的CSS属性

    关于继承的结论. 1.元素选择器的作用强于继承的作用:用户定义强于浏览器默认(详见(3)<Head First HTML与CSS>学习笔记---CSS入门的2) 2.基于类的选择器> ...

  3. c# 从DataGridVieew导出到excel

    public static bool DataGridViewToExcel(DataGridView dataGridView, bool isShowExcel) { int rowsQty = ...

  4. TensorFlow 安装 Win10 Python+GPU

    前叙:有灵魂的程序都是每一个程序员的最终目标.TensorFlow了解下? 打算花几个月学机器学习,TensorFlow是很好的选择,折腾了会环境,略有心得分享下. 环境:win10 Python:3 ...

  5. 手写MVVM框架 之vue双向数据绑定原理剖析

    <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title> ...

  6. MATLAB GUI制作快速入门

    创建空白的GUI在MATLAB命令行中输入guide新建GUI,选择Blank GUI (Default),点击确定后就生成了一个空白的GUI制作界面,如下图所示 图1制作GUI的具体过程简单加法器将 ...

  7. Boxes And Balls(三叉哈夫曼编码)

    题目 原题链接:http://codeforces.com/problemset/problem/884/D 现有一堆小石子,要求按要求的数目分成N堆,分别为a1.a2....an.具体的,每次选一个 ...

  8. Java中System.setProperty()用法

    /*  * 设置指定键对值的系统属性  * setProperty (String prop, String value);  *  * 参数:  * prop - 系统属性的名称.  * value ...

  9. 查看cuda版本和cudann

    nvcc -V 没有找到直接查询cudann版本的命令,但发现cudann装在 /usr/local/cuda/lib64/目录下,libcudnn.so就是相应版本

  10. QT+信号有参数与无参数的实现+QT4和QT5在信号和槽使用上的区别

    在QT5中,信号有参数和无参数 #ifndef SUBWIDGET_H #define SUBWIDGET_H #include <QWidget> #include <QPushB ...