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

  • 相关阅读:
    ios的自动转屏
    Acoustic Echo Cancellation (AEC) 回音消除技术探索
    springMVC之数据传递
    redis.conf 配置详解
    HDU 1880 字符串hash 入门题
    算法题: 求一个整数数组中,通过元素加减运算得到指定结果的所有运算过程. 例如【5,4,6,7,1】= 9 ?
    创建和使用SQL Server SSAS本地多维数据集
    yum局域网软件源搭建
    [置顶] 使用Android OpenGL ES 2.0绘图之六:响应触摸事件
    [置顶] Nosql笔记(一)——关系型数据库回顾
  • 原文地址:https://www.cnblogs.com/cszlg/p/3244138.html
Copyright © 2011-2022 走看看