Codeforces 320B Ping-Pong (Easy Version)

来源:互联网 发布:win7所有软件都打不开 编辑:程序博客网 时间:2024/05/01 10:09

题目链接: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-7struct 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