CodeForces - 320B(DFS搜索)

原文链接:http://codeforces.com/problemset/problem/320/B

思路:

这个题告诉你不断的告诉你一些区间,然后如果两个区间(a,b)(c,d)满足 c<a<d or c<b<d.那么这两个区间就是连通的,如果区间1和2 连通,区间2和3连通,那么1和3也是连通的。
是一道常规的搜索算法,就是题意不太好懂,这里运用DFS解

​
#include<iostream>
#include<cstdio>
#include<cstring>
#include<string>
#include<algorithm>
using namespace std;
typedef long long ll;
typedef pair<ll,ll>p;
bool vis[110];
p pp[110];
int counter=1;
void DFS(int x,int y)//x:开始区间,y终点区间
{
    if(vis[x]||vis[y])//如果vis[x]为1表示走遍了所有x的连通区间,又回到了x这里,所以结束操作;vis[y]为1表示走遍x的所有连通区间,到达了终点y,结束搜索。
        return;
    vis[x]=true;
    for(int i=0;i<counter;i++)
    {
        if(vis[i])
            continue;
        if((pp[x].first<pp[i].second&&pp[x].first>pp[i].first)||(pp[x].second<pp[i].second&&pp[x].second>pp[i].first))//连通性判断.找x的连通区间
        {
            DFS(i,y);
        }
    }
}
int main()
{
    int n;
    cin>>n;
    int type,a,b;
    while(n--)
    {
        scanf("%d%d%d",&type,&a,&b);
        if(type==1)
        {
            pp[counter].first=a;
            pp[counter++].second=b;
        }
        else if(type==2)
        {
            memset(vis,0,sizeof(vis));
            DFS(a,b);
            if(vis[b])
                printf("YES\n");
            else
                printf("NO\n");
        }
    }
    return 0;
}

​

 

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值