Codeforces 320B Ping-Pong (Easy Version)

题目链接:http://codeforces.com/contest/320/problem/B

题意:先输入n,接下来输入n行,每行输入三个数,q,a,b,若q==1,则添加一个区间(a,b)。若q==2,表示查询第a个区间能否到达第b个区间。注意: 区间a:(x,y)能到区间b(c,d)的条件是:

c < x < d || c < y < d

这个条件有些特殊,一旦从一个小区间进入一个包含它的大区间后就不能再进入它包含的任何一个小区间了。

思路:因为n数量级不大,才100。直接dfs。但需要注意,搜的时候回溯条件不需要改变,因为对每次查询的vis[i]表示是否存在一条路从a到i,若存在就不需要继续重复搜了。

#include <iostream>
#include <iomanip>
#include <cstdio>
#include <algorithm>
#include <cmath>
#include <queue>
#include <vector>
#include <stack>
#include <string>
#include <cstring>
#include <cassert>
using namespace std;

typedef long long ll;
const int maxn=2222;
const int INF=0x7fffffff;
const int mod=1e7+7;
#define LSON l,m,rt<<1
#define RSON m+1,r,rt<<1|1
#define ESP 1e-7

struct point {
    int l,r;
};

point p[111];
int n,sum;
bool vis[111];

bool check(int a, int b) {//判断区间a能否到区间b
    if(p[a].l>p[b].l && p[a].l<p[b].r)
        return true;
    else if(p[a].r>p[b].l && p[a].r<p[b].r)
        return true;
    return false;
}

bool dfs(int now, int ed) {
    if(now==ed) {
        return true;
    }
    for(int i=1;i<sum;i++) {
        if(check(now, i) && !vis[i]) {
            vis[i]=true;
            if(dfs(i, ed))
                return true;
        }
    }
    return false;
}

int main() {
    scanf("%d", &n);
    sum=1;
    for(int cc=0;cc<n;cc++) {
        int order;
        scanf("%d", &order);
        if(order==1) {
            scanf("%d%d", &p[sum].l, &p[sum].r);
            sum++;
        }
        else {
            int a,b;
            scanf("%d%d", &a, &b);
            for(int i=1;i<sum;i++) vis[i]=false;
            vis[a]=true;
            if(dfs(a, b)) 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、付费专栏及课程。

余额充值