Submission

Status:

(PPPPPPPPP)(PPPP)(PPPPPP)(PPPPPPPPPP)

Subtask/Task Score:

{25/25}{25/25}{20/20}{30/30}

Score: 100

User: krittaphot

Problemset: เดินทางข้ามชุมชน

Language: cpp

Time: 0.335 second

Submitted On: 2026-03-06 16:48:08

#include <bits/stdc++.h>

using namespace std;

int find_parent(int node,vector<int> &parent){
	if(parent[node] == node){
		return node;
	}
	return parent[node] = find_parent(parent[node],parent);
}

int main()
{
	int n,m,q;
	cin >> n >> m >> q;
	vector<int> parent(n);
	for(int i = 0;i<n;i++){
		parent[i] = i;
	}
	vector<pair<int,pair<int,int>>> edges;
	for(int i = 0;i<m;i++){
		int a,b,w;
		cin >> a >> b >> w;
		edges.push_back({w,{a,b}});
	}
	sort(edges.begin(),edges.end());
	vector<pair<pair<int,int>,pair<int,int>>> queries;
	for(int i = 0;i<q;i++){
		int a,b,cost;
		cin >> a >> b >> cost;
		queries.push_back({{cost,i},{a,b}});
	}
	sort(queries.begin(),queries.end());
	int l = 0;
	vector<bool> ans(q);
	for(int i = 0;i<q;i++){
		int cost = queries[i].first.first;
		int idx = queries[i].first.second;
		int a = queries[i].second.first;
		int b = queries[i].second.second;
		while(l < m && edges[l].first <= cost){
			int A = find_parent(edges[l].second.first,parent);
			int B = find_parent(edges[l].second.second,parent);
			parent[A] = B;
			l++;
		}
		
		if(find_parent(a,parent) == find_parent(b,parent)){
			ans[idx] = true;
		} 
		else{
			ans[idx] = false;
		}
	}
	
	
	for(auto x : ans){
		if(x){
			cout << "Yes" << "\n";
		}
		else{
			cout << "No" << "\n";
		}
	}
}