Ping-Pong (Easy Version)(DFS)
2013-08-07 20:15
1366 查看
B. Ping-Pong (Easy Version)
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output
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 or c < b < d. Also there is a path from interval I1 from our set to interval I2 from our set if there is a sequence of successive moves starting from I1 so that we can reach I2.
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 from a-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.
Input
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 exceed 109 by their absolute value.
It's guaranteed that all queries are correct.
Output
For each query of the second type print "YES" or "NO" on a separate line depending on the answer.
近来很颓。。。
View Code
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output
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 or c < b < d. Also there is a path from interval I1 from our set to interval I2 from our set if there is a sequence of successive moves starting from I1 so that we can reach I2.
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 from a-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.
Input
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 exceed 109 by their absolute value.
It's guaranteed that all queries are correct.
Output
For each query of the second type print "YES" or "NO" on a separate line depending on the answer.
近来很颓。。。
#include <iostream> #include <string> #include <algorithm> #include <map> #include <vector> #include <cstdio> #include <cmath> #include <cstring> using namespace std; const int MAXN = 102; int a[MAXN], b[MAXN]; bool f[MAXN]; int n, cnt; void dfs(int s, int e) { f[s] = 1; if(f[e]) return ; for(int i = 1; i < cnt; i++) { if(f[i]) continue; if(a[s] > a[i] && a[s] < b[i]) dfs(i, e); if(f[e]) return ; if(b[s] > a[i] && b[s] < b[i]) dfs(i, e); if(f[e]) return ; } } int main() { while(scanf("%d", &n) != EOF) { cnt = 1; int m, x, y; while(n--) { scanf("%d", &m); if(m == 1) { scanf("%d %d", &a[cnt], &b[cnt]); cnt++; } else { scanf("%d %d", &x, &y); memset(f, 0, sizeof(f)); dfs(x, y); if(f[y]) puts("YES"); else puts("NO"); } } } return 0; }
View Code
相关文章推荐
- CodeForces 320B Ping-Pong (Easy Version)-DFS
- CodeForces 320 B. Ping-Pong DFS BFS 弗洛伊德算法
- Round #189 (Div.2) B. Ping-Pong (Easy Version)
- Codeforces 320B Ping-Pong (Easy Version)
- E - Ping-Pong (Easy Version)
- CodeForces - 320B Ping-Pong (Easy Version)
- codeforces 320B Ping-Pong (Easy Version)
- HDU 6203 ping ping ping (LCA+DFS序)
- 【Codeforces319E】Ping-Pong
- LA4329:Ping pong(树状数组)
- LA4329:Ping pong(树状数组入门)
- Ping Pong 操作
- Ping pong UVA - 1428 树状数组
- POJ 3928 & hdu 2492 & Uva1428 PingPong 【树状数组】
- 4.3之后的PingPong效果实现
- codeforces B. Ping-Pong (Easy Version) 解题报告
- Run the MPI PingPong benchmark
- WebSocket的ping与pong的java实现…
- Ping Pong,Beijing 2008,LA4329 树状数组 好题
- 一种ping pong请求导致的超时