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

  • 相关阅读:
    概率论——随机事件及其概率
    Web应用程序项目以配置使用IIS。未找到Web服务器”链接地址”
    LaTeX中.sty文件缺失解决办法
    IIS中的经典模式和集成模式有什么区别
    判断有序整型数组中是否存在两数,相加之和等于给定的任意整数
    51Job的搜索技巧
    登录失败。该登录名来自不受信任的域,不能与 Windows 身份验证一起使用。
    Ubuntu使用Latex模板moderncv写简历
    COM相关操作(C#)
    什么是单线程单元(STA)什么是多线程单元(MTA)
  • 原文地址:https://www.cnblogs.com/cszlg/p/3244138.html
Copyright © 2011-2022 走看看