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

总之就是用传递性 问能否从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. spring mvc支持跨域请求

    @WebFilter(urlPatterns = "/*", filterName = "corsFilter") public class CorsFilte ...

  2. iOS面试题之内存管理

    本文围绕内存管理的几种方法展开叙述. 1.内存管理是什么? 内存管理,就是对内存资源进行优化. 2.内存管理的三种方法? Objective-C的内存管理主要有三种方式ARC(自动内存计数).MRC( ...

  3. AJPFX关于表结构的相关语句

    //表结构的相关语句==================================== 建表语句: create table 表名(                                ...

  4. AJPFX循环结构整理资料

    Java语言基础(循环结构概述和for语句的格式及其使用)* A:循环结构的分类        * for,while,do...while * B:循环结构for语句的格式:*           ...

  5. 看Spring Data如何简化数据操作

    Spring Data 概述 Spring Data 用于简化数据库访问,支持NoSQL 和 关系数据存储,其主要目标是使数据库的访问变得方便快捷. SpringData 项目所支持 NoSQL 存储 ...

  6. pip install python-igraph 报错,C core of igraph 没有安装。

    (一)问题描述 Centos7 安装python-igraph时,pip install python-igraph 报错,C core of igraph 没有安装. failure: repoda ...

  7. Python 中列表、元祖、字典

    1.元祖: 对象有序排列,通过索引读取读取, 对象不可变,可以是数字.字符串.列表.字典.其他元祖 2.列表: 对象有序排列,通过索引读取读取, 对象是可变的,可以是数字.字符串.元祖.其他列表.字典 ...

  8. jquery 定位

    jquery 定位 <html> <head> <title>jquery 定位</title> </head> <body> ...

  9. 12scrapy_redis

    一.简介 1.redis redis是一个key-value存储系统.和Memcached类似,它支持存储的value类型相对更多,包括string(字符串).list(链表).set(集合).zse ...

  10. codevs 2853 方格游戏--棋盘dp

    方格游戏:http://codevs.cn/problem/2853/ 这和传纸条和noip方格取数这两个题有一定的相似性,当第一眼看到的时候我们就会想到设计$dp[i][j][k][l]$(i,j表 ...