Round #189 (Div.2) B. Ping-Pong (Easy Version)
2014-07-25 15:11
423 查看
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.
Sample test(s)
Input
Output
————————————————————Dividing Line————————————————————
思路:一开始我们以为这是区间之间有交集,就属于同一个集合,否则就不是。然后他们用并查集做了一遍,错了。后来发现不是简单的集合关系,因为有可能从区间A可以到区间B但是从区间B去无法到达区间A。比如(5,6)和(3,8)。所以要用dfs,在查询的时候,从起点dfs到终点,判断是否有路。
代码如下:
#include <cstdio>
#include <cstdlib>
#include <cstring>
const int N = 105;
struct Node
{
int l, r;
}inter
;
bool vis
;
int cnt = 0;
bool judge(int id_a, int id_b)
{
if(inter[id_a].l > inter[id_b].l && inter[id_a].l < inter[id_b].r)
return true;
if(inter[id_a].r > inter[id_b].l && inter[id_a].r < inter[id_b].r)
return true;
return false;
}
bool dfs(int id_s, int id_e)
{
if(id_s == id_e)
return true;
vis[id_s] = true;
for(int i = 1; i <= cnt; i++) if(!vis[i]) {
if(judge(id_s, i) && dfs(i, id_e))
return true;
}
return false;
}
int main()
{
int n;
int op, a, b;
scanf("%d", &n);
while(n--) {
scanf("%d%d%d", &op, &a, &b);
if(op == 1) {
cnt++;
inter[cnt].l = a;
inter[cnt].r = b;
}
else if(op == 2) {
memset(vis, false, sizeof(vis));
if(dfs(a, b))
puts("YES");
else
puts("NO");
}
}
return 0;
}
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.
Sample test(s)
Input
5 1 1 5 1 5 11 2 1 2 1 2 9 2 1 2
Output
NO YES
————————————————————Dividing Line————————————————————
思路:一开始我们以为这是区间之间有交集,就属于同一个集合,否则就不是。然后他们用并查集做了一遍,错了。后来发现不是简单的集合关系,因为有可能从区间A可以到区间B但是从区间B去无法到达区间A。比如(5,6)和(3,8)。所以要用dfs,在查询的时候,从起点dfs到终点,判断是否有路。
代码如下:
#include <cstdio>
#include <cstdlib>
#include <cstring>
const int N = 105;
struct Node
{
int l, r;
}inter
;
bool vis
;
int cnt = 0;
bool judge(int id_a, int id_b)
{
if(inter[id_a].l > inter[id_b].l && inter[id_a].l < inter[id_b].r)
return true;
if(inter[id_a].r > inter[id_b].l && inter[id_a].r < inter[id_b].r)
return true;
return false;
}
bool dfs(int id_s, int id_e)
{
if(id_s == id_e)
return true;
vis[id_s] = true;
for(int i = 1; i <= cnt; i++) if(!vis[i]) {
if(judge(id_s, i) && dfs(i, id_e))
return true;
}
return false;
}
int main()
{
int n;
int op, a, b;
scanf("%d", &n);
while(n--) {
scanf("%d%d%d", &op, &a, &b);
if(op == 1) {
cnt++;
inter[cnt].l = a;
inter[cnt].r = b;
}
else if(op == 2) {
memset(vis, false, sizeof(vis));
if(dfs(a, b))
puts("YES");
else
puts("NO");
}
}
return 0;
}
相关文章推荐
- CodeForces 320B Ping-Pong (Easy Version)-DFS
- Ping-Pong (Easy Version)(DFS)
- CodeForces - 320B Ping-Pong (Easy Version)
- Codeforces 320B Ping-Pong (Easy Version)
- E - Ping-Pong (Easy Version)
- codeforces 320B Ping-Pong (Easy Version)
- LA4329:Ping pong(树状数组)
- Ping pong(树状数组)
- LA 4329 Ping pong / 树状数组
- Ping pong UVA - 1428 树状数组
- websocket心跳连接pingpong;
- pku hdu pingpong
- Frequent values && Ping pong
- kernel ping pong boot(bootloader kernel 传递参数)
- 为 ngui TweenPosition 添加 pingpongone
- Ping Pong 操作
- pingpong线程输出问题
- 【瞎玩】PingPong0.2
- Ping pong(树状数组经典)
- Windows下Socket TCP网络传输及ping-pong缓冲区进程同步研究实现