1851G - Vlad and the Mountains - CodeForces Solution


binary search data structures dsu graphs implementation sortings trees two pointers

Please click on ads to support us..

C++ Code:

#include <bits/stdc++.h>
using namespace std;
struct DSU{
	vector<int> parent, sz; 
	DSU(int n){
		parent.resize(n + 1);  
		iota(parent.begin(), parent.end(), 0) ;
		sz.assign(n+1, 1) ;  
	}
	void make_set(int v){
		parent[v] = v; 
		sz[v] = 1; 
	}	
	int find_set(int v){
		if(parent[v] == v){
			return v; 
		}
		return parent[v] = find_set(parent[v]) ; 
	}
	bool same(int a, int b){
		return find_set(a) == find_set(b); 
	}
	void merge(int a,int b){
		a = find_set(a) , b = find_set(b);
		if(a != b){
			if(sz[a] < sz[b]) swap(a,b) ; 
			parent[b] = a; 
			sz[a] += sz[b] ; 
		}
	}
};
const int mxN = 2e5; 
vector<vector<int>> all_vertices, check; 
void solve(){
	int n , m ; cin >> n >> m ; 
	vector<int> H(n) ; for(auto &h: H) cin >> h; 
	all_vertices.clear(), check.clear();
	for(int i = 0 ; i < m ; ++i){
		int u ,v ; cin >> u >> v ;
		int f=max(H[u-1],H[v-1]) ; 
		all_vertices.push_back({f,u,v}) ; 
	}
	sort(begin(all_vertices), end(all_vertices)) ; 

	int q; cin >> q; 
	vector<bool> ans(q); 
	for(int i = 0 ; i < q; i ++){
		int a, b, e; cin >> a >> b >> e; 
		// a--,b--;
		//mountain a -> mountain b using energy e
		//H[b] has to be within H[a] + e 
		e += H[a-1];
		check.push_back({e,a,b,i}) ; 
	}
	sort(begin(check), end(check));
	DSU dsu(n) ;
	int j = 0, f = 0 ;  
	for(int i = 0 ; i < check.size() ; i ++){
		while(j < all_vertices.size() && all_vertices[j][0] <= check[i][0]){
			// dsu.make_set(all_vertices[j][1]) , dsu.make_set(all_vertices[j][2]);
			dsu.merge(all_vertices[j][1], all_vertices[j][2]) ;  
			++j;  
		}
		ans[check[i][3]] = dsu.same(check[i][1],check[i][2]) ; 
	}
	//for each query we need to check if for all vertices where max <= e + H[a], b is among them
	for(int i = 0 ; i < q; i ++){
		cout << (ans[i]?"YES\n" : "NO\n") ; 
	}
}
int main(){
	ios_base::sync_with_stdio(false), cin.tie(NULL) ; 
	int t; cin >> t;
	while(t--)solve();
}


Comments

Submit
0 Comments
More Questions

1704. Determine if String Halves Are Alike
1732. Find the Highest Altitude
709. To Lower Case
1688. Count of Matches in Tournament
1684. Count the Number of Consistent Strings
1588. Sum of All Odd Length Subarrays
1662. Check If Two String Arrays are Equivalent
1832. Check if the Sentence Is Pangram
1678. Goal Parser Interpretation
1389. Create Target Array in the Given Order
1313. Decompress Run-Length Encoded List
1281. Subtract the Product and Sum of Digits of an Integer
1342. Number of Steps to Reduce a Number to Zero
1528. Shuffle String
1365. How Many Numbers Are Smaller Than the Current Number
771. Jewels and Stones
1512. Number of Good Pairs
672. Richest Customer Wealth
1470. Shuffle the Array
1431. Kids With the Greatest Number of Candies
1480. Running Sum of 1d Array
682. Baseball Game
496. Next Greater Element I
232. Implement Queue using Stacks
844. Backspace String Compare
20. Valid Parentheses
746. Min Cost Climbing Stairs
392. Is Subsequence
70. Climbing Stairs
53. Maximum Subarray