codeforces 320B bfs


题目:
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. "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.

Examples
Input
5
1 1 5
1 5 11
2 1 2
1 2 9
2 1 2
Output
NO
YES

题意:区间(a,b)和(c,d)若a<c<b或c<b<d则两个区间相通。有两种操作:1、增加一个区间,2、检查两个点是否相通。


方法:广度优先搜索。


代码:

//By Sean Chen
#include <iostream>
#include <cstdio>
#include <cstring>
#include <queue>

using namespace std;

struct road{
    int u,v;
};

road Map[110];
int cnt,visit[110];

int check(int x,int y)
{
    int a=Map[x].u,b=Map[x].v;
    int c=Map[y].u,d=Map[y].v;
    if((c<a && a<d) || (c<b && b<d))
        return 1;
    return 0;
}

void bfs(int x,int y)
{
    queue <int> Q;
    Q.push(x);
    while(Q.size())
    {
        int a=Q.front();
        Q.pop();
        for(int i=0;i<cnt;i++)
            if(!visit[i] && check(a,i))
            {
                Q.push(i);
                visit[i]=1;
            }
    }
    return;
}

int main()
{
    int T,a,x,y;
    scanf("%d",&T);
    while(T--)
    {
        scanf("%d%d%d",&a,&x,&y);
        if(a==1)
        {
            Map[cnt].u=x;
            Map[cnt].v=y;
            cnt++;
        }
        else
        {
            memset(visit,0,sizeof(visit));
            bfs(x,y);
            if(visit[y])
                printf("YES\n");
            else
                printf("NO\n");
        }
    }
    return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值