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.