zoukankan      html  css  js  c++  java
  • Ping-Pong (Easy Version)(DFS)

    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

  • 相关阅读:
    关于贝宝支付的一些信息和思路
    自动执行的脚本不错的思路
    关于微信公众平台生成带参数的二维码的场景值和系统对接的问题
    centos7.3查看时区
    关于where和having的直观理解
    关于微信支付的退款那些事
    关于微信支付的那些事
    正则替换
    java 正则表达式语法
    正则表达式
  • 原文地址:https://www.cnblogs.com/cszlg/p/3244138.html
Copyright © 2011-2022 走看看