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

总之就是用传递性 问能否从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. python实战之爬取喜玛拉雅专辑信息

    import urllib.request import json from lxml import etree url='http://www.ximalaya.com/dq/8.ajax' hea ...

  2. const和volatile

    const是只读变量 const修饰的变量是只读的,其本质还是变量 const修饰的局部变量在栈上分配空间 const修饰的全局变量在全局数据区分配空间 const只在编译期有用,在运行期无用 con ...

  3. IOS状态栏

    IOS状态栏是什么地方? 它是IOS设备屏幕顶部显示信号以及电池的区域.状态栏默认的高度是20像素,状态栏在软件开发中有何作用?联网应用中可在自动帮用户下载数据时使用,推荐在状态栏中予以显示.状态栏可 ...

  4. Selenium私房菜系列1 -- Selenium简介

    一.Selenium是什么? Selenium是ThroughtWorks公司一个强大的开源Web功能测试工具系列,本系列现在主要包括以下4款: 1.Selenium Core:支持DHTML的测试案 ...

  5. (转)Spring的bean管理(注解方式)

    http://blog.csdn.net/yerenyuan_pku/article/details/69663779 Spring的bean管理(注解方式) 注解:代码中的特殊标记,注解可以使用在类 ...

  6. 导入Excel表格(二)

    1. 提取session中的数据.并进行分页操作,上传excel表格,保存到临时表格. 初始化临时表格,提交表单,判断状态是否为真,若为真,则启用 导入到数据库 的按钮:为false,让查询的url ...

  7. Java集合(四)--基于JDK1.8的ArrayList源码解读

    public class ArrayList<E> extends AbstractList<E> implements List<E>, RandomAccess ...

  8. 1-jdk的安装与配置

    1- Jvm.jdk.jre之间的关系 JVM:Java虚拟机,保证java程序跨平台.(Java Virtual Machine) JRE: Java运行环境,包含JVM和核心类库.如果只是想运行j ...

  9. Java集合系列之HashMap

    概要 第1部分 HashMap介绍 HashMap简介 HashMap 是一个散列表,它存储的内容是键值对(key-value)映射.HashMap 继承于AbstractMap,实现了Map.Clo ...

  10. 关闭Visual Studio 2015 关闭单击打开文件的功能

    工具-->选项-->环境-->选项卡和窗口-->预览选项卡 去掉“在解决方案资源管理器中预览选定的文件(在按住Alt的同时单击可避免预览)(X)”的勾选