Submission

Status:

(PPPPPPPPP)(PPPP)(PPTSSS)(SSSSSSSSSS)

Subtask/Task Score:

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

Score: 50

User: colo39

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

Language: cpp

Time: 1.078 second

Submitted On: 2026-11-28 11:18:02

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

struct Edge{
    int u,v,w;
    bool operator<(const Edge &o) const{
        if (w != o.w) return w < o.w;
        if (u != o.u) return u < o.u;
        return v < o.v;
    }
};
struct Q{
    int u,v,w,idx;
    bool operator<(const Q &o) const{
        if (w != o.w) return w < o.w;
        if (u != o.u) return u < o.u;
        return v < o.v;
    }
};

int head[100005], sz[100005];
int findhead(int n) {
    if (head[n] == n) return n;
    return head[n] = findhead(head[n]);
}

void unionhead(int a,int b) {
    a = findhead(a);
    b = findhead(b);
    if (a != b) {
        if (sz[a] < sz[b]) {
            head[a] = b;
            sz[b] += sz[a];
        } else {
            head[b] = a;
            sz[a] += sz[b];
        }
    }
}
int main () {
    ios_base::sync_with_stdio(false);
    cin.tie(0);
    
    int n,m,q;
    vector<Edge> vec;
    vector<Q> que;
    vector<pair<int,int>> ans;
    cin >> n >> m >> q;
    for (int i = 1;i <= m;i++) {
        Edge e;
        cin >> e.u >> e.v >> e.w;
        vec.push_back(e);
    }
    
    for (int i = 1;i <= q;i++) {
        Q e;
        cin >> e.u >> e.v >> e.w;
        e.idx = i;
        que.push_back(e);
    }
    sort(vec.begin(),vec.end());
    sort(que.begin(),que.end());
    for (int i = 0;i < n;i++) {
        head[i] = i;
        sz[i] = 1;
    }
    for (auto &x : que) {
        for (auto &y : vec) {
            if (y.w > x.w) break;
            unionhead(y.u,y.v);
        }
        if (findhead(x.u) == findhead(x.v)) {
            ans.push_back({x.idx,1});
        } else {
            ans.push_back({x.idx,0});
        }
    }
    sort(ans.begin(),ans.end());
    for (auto &x : ans) {
        if (x.second == 1) cout << "Yes\n";
        else cout << "No\n";
    }
    return 0;
}