codeforces 320B bfs
来源:互联网 发布:list排序最快方法 java 编辑:程序博客网 时间:2024/04/28 12:06
题目:
In this problem at each moment you have a set of intervals. You can move from interval(a, b) from our set to interval (c, d) from our set if and only if c < a < d orc < b < d. Also there is a path from intervalI1 from our set to intervalI2 from our set if there is a sequence of successive moves starting fromI1 so that we can reachI2.
Your program should handle the queries of the following two types:
- "1 x y" (x < y) — add the new interval(x, y) to the set of intervals. The length of the new interval is guaranteed to be strictly greater than all the previous intervals.
- "2 a b" (a ≠ b) — answer the question: is there a path froma-th (one-based) added interval to b-th (one-based) added interval?
Answer all the queries. Note, that initially you have an empty set of intervals.
The first line of the input contains integer n denoting the number of queries,(1 ≤ n ≤ 100). Each of the following lines contains a query as described above. All numbers in the input are integers and don't exceed109 by their absolute value.
It's guaranteed that all queries are correct.
For each query of the second type print "YES" or "NO" on a separate line depending on the answer.
51 1 51 5 112 1 21 2 92 1 2
NOYES
题意:区间(a,b)和(c,d)若a<c<b或c<b<d则两个区间相通。有两种操作:1、增加一个区间,2、检查两个点是否相通。
方法:广度优先搜索。
代码:
//By Sean Chen#include <iostream>#include <cstdio>#include <cstring>#include <queue>using namespace std;struct road{ int u,v;};road Map[110];int cnt,visit[110];int check(int x,int y){ int a=Map[x].u,b=Map[x].v; int c=Map[y].u,d=Map[y].v; if((c<a && a<d) || (c<b && b<d)) return 1; return 0;}void bfs(int x,int y){ queue <int> Q; Q.push(x); while(Q.size()) { int a=Q.front(); Q.pop(); for(int i=0;i<cnt;i++) if(!visit[i] && check(a,i)) { Q.push(i); visit[i]=1; } } return;}int main(){ int T,a,x,y; scanf("%d",&T); while(T--) { scanf("%d%d%d",&a,&x,&y); if(a==1) { Map[cnt].u=x; Map[cnt].v=y; cnt++; } else { memset(visit,0,sizeof(visit)); bfs(x,y); if(visit[y]) printf("YES\n"); else printf("NO\n"); } } return 0;}
- codeforces 320B bfs
- bfs--Codeforces,520B.
- Codeforces 329B bfs
- CodeForces 329B(bfs)
- CodeForces 329B(BFS)
- CodeForces 320 B. Ping-Pong DFS BFS 弗洛伊德算法
- CodeForces - 329B 《Biridian Forest》 【反向BFS】
- Codeforces 350B Resort(bfs)
- Codeforces 329B Biridian Forest(BFS)
- Codeforces 429B Working out bfs构造
- Codeforces 520 B Two Buttons BFS
- Codeforces 520B Two Buttons 【BFS】||【spfa】
- codeforces 520B Two Buttons BFS
- codeforces 585B Phillip and Trains(bfs)
- Codeforces 626B Cards 【BFS or 讨论】
- CodeForces 666B. World Tour【BFS】
- codeforces 689B Mike and Shortcuts (bfs)
- Codeforces Round #295 (Div. 2) B bfs
- C++第6次作业
- Java思维导图(3)--面向对象基础 下
- 博客-开篇
- UESTC 1598 加帕里公园的friends 线段树区间合并
- Struts2的三种传值方式
- codeforces 320B bfs
- Android中的Interpolator
- 关于EditText的高级用法之Filter
- 改造u3d第一人称控制器,使之适合Cardboard+蓝牙手柄控制
- py 字符串相关
- linux shell重定向(2>&1)
- springMVC系列(六)——拦截器实现自定义注解接口访问限制
- 分布式架构学习之:033--MySQL源码编译安装(CentOS-6.7+MySQL-5.6)
- ZIGBEE技术 Zigbee Technology