320B - Ping-Pong (Easy Version) - CodeForces Solution


dfs and similar graphs *1500

Please click on ads to support us..

Python Code:

def query_add(x, y, intervals):
    intervals.append((x, y))


def can_move(a, b, c, d):
    return c < a < d or c < b < d


def query_has_path(param1, param2, intervals):
    stack = [param1]
    visited = [False] * len(intervals)
    visited[param1 - 1] = True
    while len(stack) > 0:
        cur = stack.pop()
        a, b = intervals[cur - 1]
        for i in range(0, len(intervals)):
            if visited[i]:
                continue
            c, d = intervals[i]
            if can_move(a, b, c, d):
                if i + 1 == param2:
                    print("YES")
                    return
                visited[i] = True
                stack.append(i + 1)
    print("NO")


def parse_queries(num_queries):
    queries = []
    intervals = []
    for i in range(0, num_queries):
        queries.append(input().split())
    for t, param1, param2 in queries:
        if t == "1":
            query_add(int(param1), int(param2), intervals)
        else:
            query_has_path(int(param1), int(param2), intervals)


if __name__ == '__main__':
    n = int(input())
    parse_queries(n)

C++ Code:

#include <bits.h>
#include <iostream>
#include <sstream>
#include <iomanip>
#include <algorithm>
#include <string.h>
#include <set>
#include <queue>
#include <map>
#include <unordered_map>
#include <vector>
#include <cmath>
#define ll long long

using namespace std;
const int N = 1e5 + 7, INF =1e9 + 7;
int n,m;
map<int,vector<pair<int,int>>>adj;    // *
map<int,int>vis;
bool ans =false;
void dfs(int st,pair<int,int> dis){
    vis[st] =1;
    for(auto child: adj[st]){
        if(child.first == dis.first && child.second == dis.second){ans =true;return;}
        if(!vis[child.first]){dfs(child.first,dis);}
    }
}
void init(){for(auto &i:vis){i.second =0;}}
void solve(){
    cin >> n;
    vector<pair<int,int>>input(n+10);
    int type,c,d;
    for(int i=0,j=1;i<n;i++){
        cin >> type >> c >> d;
        if(type == 1){
            for(auto pr: input){
                if(pr.first > c && pr.first < d || pr.second > c && pr.second < d){
                    adj[pr.first].push_back({c,d});
                }
                if(pr.first < c && pr.second > c || pr.first < d && pr.second > d){
                    adj[c].push_back(pr);
                }
            }
            vis[c] =0;
            vis[d] =0;
            input[j++] = {c,d};
        }
        else{
            init();
            dfs(input[c].first,input[d]);
            if(ans) cout << "YES\n";
            else cout << "NO\n";
            ans = false;
        }
    }
}
int main(){
    int t;
    t =1;
    while(t--){
        solve();
    }
    return 0;
}


Comments

Submit
0 Comments
More Questions

1618A - Polycarp and Sums of Subsequences
1618B - Missing Bigram
938. Range Sum of BST
147. Insertion Sort List
310. Minimum Height Trees
2110. Number of Smooth Descent Periods of a Stock
2109. Adding Spaces to a String
2108. Find First Palindromic String in the Array
394. Decode String
902. Numbers At Most N Given Digit Set
221. Maximal Square
1200. Minimum Absolute Difference
1619B - Squares and Cubes
1619A - Square String
1629B - GCD Arrays
1629A - Download More RAM
1629C - Meximum Array
1629D - Peculiar Movie Preferences
1629E - Grid Xor
1629F1 - Game on Sum (Easy Version)
2148. Count Elements With Strictly Smaller and Greater Elements
2149. Rearrange Array Elements by Sign
2150. Find All Lonely Numbers in the Array
2151. Maximum Good People Based on Statements
2144. Minimum Cost of Buying Candies With Discount
Non empty subsets
1630A - And Matching
1630B - Range and Partition
1630C - Paint the Middle
1630D - Flipping Range