Submission

Status:

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

Subtask/Task Score:

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

Score: 100

User: navysrimuang

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

Language: cpp

Time: 0.143 second

Submitted On: 2026-03-19 20:11:20

#include<bits/stdc++.h>
using namespace std;
using ll = long long;

int ss,ee; ll k;
vector<vector<pair<ll,int>>> adj;
// bool ued[100001];
bool dfs(int u,int t,int p){
	if(u == t) return 1;
	for(auto [w,v] : adj[u]){
			if(v == p || w  > k) continue;
			bool rs = dfs(v,t,u);
			if(rs != 0) return rs;	
	}

	return 0;
}
struct DSU{
	vector<int> par,sz;	
	DSU(int n){
		par.resize(n); sz.resize(n,1);
		iota(par.begin(),par.end(),0);
	}

	int find(int x){
		if(par[x] == x) return x;
		else return par[x] = find(par[x]);
	}

	bool unite(int a,int b){
		int ra = find(a);
		int rb = find(b);
		if(ra == rb) return 0;

		if(sz[ra] < sz[rb]) swap(ra,rb);
		par[rb] = ra;
		sz[ra] += sz[rb];
		return 1;
	}
};

int main(){

	cin.tie(0)->sync_with_stdio(0);
	int n,m,q;
	cin >> n >> m >> q;
	vector<tuple<ll,int,int>> edges;
	for(int i = 0;i<m;i++){
		int a,b;
		ll c;
		cin >> a >> b >> c;
		edges.push_back({c,a,b});
	}	
	sort(edges.begin(),edges.end());	

	// cout << "----\n";
	// for(auto [w,a,b] : edges){
	// 	cout << a << " " << b << " " << w << "\n";
	// }	
	// cout << "fick\n";	
	vector<tuple<ll,int,int,int>> question;
	for(int i = 0;i<q;i++){
		int a,b;
		ll c;
		cin >> a >> b >> c;
		question.push_back({c,a,b,i});
	}
	
	sort(question.begin(),question.end());
	// cout << "\n";
	
	int j = 0;
	int cmp = 0;
	DSU dsu(n+1);
	vector<bool> ans(q);
	for(int i = 0;i<edges.size();i++){
		if(j == q) break;
		// cout << i << "\n";
		auto [w,a,b] = edges[i];
		auto [k,x,y,idx] = question[j];	
		// cout << a << " " << b << " " << w << "\n";
		// if(w == 1 && a == 1 && b == 4) cout << "nog " << k << "\n";
		if(k >= w){
			if(dsu.unite(a,b)){
				// ued[a] = ued[b] = 1;
				cmp++;
				// cout << "unite " << a << " " << b << " " << w << "\n";
			}else{
				// cout << "failed " << a << " " << b << " " << w << '\n';
				// cout << dsu.find(a) << " " << dsu.find(b) << "\n";
			}
		}else{
			// cout << "check " << x << " " << y << " " << k << "\n";
			// cout << idx << " " << (dsu.find(x) == dsu.find(y) ? 0 : 1 )<< "\n";
			ans[idx] = (dsu.find(x) == dsu.find(y));
			j++;
			i--;
		}
		
	}

	while(j != q){
		auto[w,x,y,idx] = question[j];
		ans[idx] = (dsu.find(x) == dsu.find(y));
		j++;
	}

	for(bool x : ans){
		if(x) cout << "Yes\n";
		else cout << "No\n";
	}


	return 0;
}