Submission
Status:
(PPPPPPPPP)(PPPP)(PPPPPP)(PPPPPPPPPP)
Subtask/Task Score:
{25/25}{25/25}{20/20}{30/30}
Score: 100
User: havename
Problemset: เดินทางข้ามชุมชน
Language: cpp
Time: 0.138 second
Submitted On: 2026-03-07 11:47:41
#include<bits/stdc++.h>
using namespace std;
#define ll int
int findroot(vector<ll>& prt,int st){
if(prt[st]==st) return st;
return prt[st]=findroot(prt,prt[st]);
}
int main(){
ios::sync_with_stdio(false);
cin.tie(nullptr);
int n,m,q;
cin>>n>>m>>q;
vector<pair<ll,pair<ll,ll>>> edgs;
vector<ll> prt(n+1);
for(int i=0;i<n;i++){
prt[i]=i;
}
for(int i=0;i<m;i++){
int a,b,c;
cin>>a>>b>>c;
edgs.push_back({c,{a,b}});
}
vector<pair<pair<ll,ll>,pair<ll,ll>>> value;
for(int i=0;i<q;i++){
int st,ed,cost;
cin>>st>>ed>>cost;
value.push_back({{cost,i},{st,ed}});
}
vector<bool> ans(q,false);
int l=0;
sort(value.begin(),value.end());
sort(edgs.begin(),edgs.end());
for(int i=0;i<q;i++){
ll cost=value[i].first.first;
ll st=value[i].second.second;
ll ed=value[i].second.first;
ll idx=value[i].first.second;
while(l<m && edgs[l].first<=cost){
ll a=edgs[l].second.first;
ll b=edgs[l].second.second;
ll A=findroot(prt,a);
ll B=findroot(prt,b);
if(A!=B){
prt[A]=B;
}
l++;
}
if(findroot(prt,st)==findroot(prt,ed)){
ans[idx]=true;
}
}
for(int i=0;i<q;i++){
if(ans[i]) cout<<"Yes\n";
else cout<<"No\n";
}
}