Codeforces 320 B. Ping-Pong DFS BFS fresh Algorithm

Source: Internet
Author: User


It is still a big problem to understand the meaning of the question. This question tells you to constantly tell you some intervals, and if the two intervals (A, B) (c, d) MeetCLatency <latencyALatency <latencyDOrCLatency <latencyBLatency <latencyD.
Then these two intervals are connected. If the intervals 1 and 2 are connected, and the intervals 2 and 3 are connected, then 1 and 3 are also connected. In this way, you can use a DFS () to check whether the range X can reach the range y. it is worth noting that the storage interval starts from T [0] and should be a DFS (x-1, Y-1 ).

#include<cstdio>#include<cstring>#include<iostream>#include<cstring>using namespace std;typedef struct inter{    int u,v;} inter;inter t[110];int count=0,vis[110];bool check(int x,int y){    int a=t[x].u,b=t[x].v;    int c=t[y].u,d=t[y].v;    if((c<a&&a<d)||(c<b&&b<d))        return true;    return false;}void dfs(int x,int y){    if(vis[x])  return ;    if(vis[y]) return ;    vis[x]=1;    for(int i=0; i<count; i++)    {        if(vis[i])            continue;        if(check(x,i))            dfs(i,y);    }}int main(){    //freopen("in.txt","r",stdin);    int n,a,x,y;    cin>>n;    while(n--)    {        cin>>a>>x>>y;        if(a==1)        {            t[count].u=x;            t[count].v=y;            count++;        }        else        {            memset(vis,0,sizeof(vis));            dfs(x-1,y-1);            if(vis[y-1])                cout<<"YES"<<endl;            else cout<<"NO"<<endl;        }    }    return 0;}

You can also use BFS () for this question. The idea is the same. The Code is as follows:

#include<cstdio>#include<cstring>#include<iostream>#include<queue>#include<cstring>using namespace std;typedef struct inter{    int u,v;} inter;inter t[110];bool check(int x,int y){    int a=t[x].u,b=t[x].v;    int c=t[y].u,d=t[y].v;    if((c<a&&a<d)||(c<b&&b<d))        return true;    return false;}int count=1,vis[110];queue<int>st;void bfs(int x,int y){    st.push(x);    while(!st.empty())    {        int a=st.front();        st.pop();        for(int i=1; i<count; i++)            if(!vis[i]&&check(a,i))            {                st.push(i);                vis[i]=1;            }    }}int main(){    //freopen("in.txt","r",stdin);    int n,a,x,y;    cin>>n;    while(n--)    {        cin>>a>>x>>y;        if(a==1)        {            t[count].u=x;            t[count].v=y;            count++;        }        else        {            memset(vis,0,sizeof(vis));            bfs(x,y);            if(vis[y])  puts("YES");            else puts("NO");        }    }    return 0;}

The final question can also be done using the Floyd-warshall algorithm. For details, refer to examples.


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.