菜单 学习猿地 - LMONKEY

VIP

开通学习猿地VIP

尊享10项VIP特权 持续新增

知识通关挑战

打卡带练!告别无效练习

接私单赚外块

VIP优先接,累计金额超百万

学习猿地私房课免费学

大厂实战课仅对VIP开放

你的一对一导师

每月可免费咨询大牛30次

领取更多软件工程师实用特权

入驻
101
0

Ping-Pong (Easy Version)(DFS)

原创
05/13 14:22
阅读数 35361
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. "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.

近来很颓。。。

 1 #include <iostream>
 2 #include <string>
 3 #include <algorithm>
 4 #include <map>
 5 #include <vector>
 6 #include <cstdio>
 7 #include <cmath>
 8 #include <cstring>
 9 using namespace std;
10 
11 const int MAXN = 102;
12 int a[MAXN], b[MAXN];
13 bool f[MAXN];
14 int n, cnt;
15 
16 void dfs(int s, int e)
17 {
18     f[s] = 1;
19     if(f[e]) return ;
20     for(int i = 1; i < cnt; i++)
21     {
22         if(f[i]) continue;
23         if(a[s] > a[i] && a[s] < b[i]) dfs(i, e);
24         if(f[e]) return ;
25         if(b[s] > a[i] && b[s] < b[i]) dfs(i, e);
26         if(f[e]) return ;
27     }
28 }
29 
30 int main()
31 {
32     while(scanf("%d", &n) != EOF)
33     {
34         cnt = 1;
35         int m, x, y;
36         while(n--)
37         {
38             scanf("%d", &m);
39             if(m == 1)
40             {
41                 scanf("%d %d", &a[cnt], &b[cnt]);
42                 cnt++;
43             }
44             else
45             {
46                 scanf("%d %d", &x, &y);
47                 memset(f, 0, sizeof(f));
48                 dfs(x, y);
49                 if(f[y]) puts("YES");
50                 else puts("NO");
51             }
52         }
53     }
54     return 0;
55 }
View Code

发表评论

0/200
101 点赞
0 评论
收藏