제출 #367087

#제출 시각아이디문제언어결과실행 시간메모리
367087Jarif_Rahman다리 (APIO19_bridges)C++17
14 / 100
106 ms11932 KiB
#include <bits/stdc++.h>
#define pb push_back
#define f first
#define sc second
using namespace std;
typedef long long int ll;
typedef string str;
struct dsu{
    int n;
    vector<int> id;
    vector<vector<int>> comp;
    dsu(int nn){
        n = nn;
        for(int i = 0; i < n;  i++) id.pb(i), comp.pb({i});
    }
    void unite(int a, int b){
        a = id[a], b = id[b];
        if(a == b) return;
        if(comp[a].size() > comp[b].size()) swap(a, b);
        while(!comp[a].empty()){
            int x = comp[a].back(); comp[a].pop_back();
            id[x] = b;
            comp[b].pb(x);
        }
    }
};
int main(){
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    int n, m; cin >> n >> m;
    dsu ds(n);
    vector<tuple<ll, int, int>> edge;
    for(int i = 0; i < m; i++){
        int a, b; ll w; cin >> a >> b >> w; a--, b--;
        edge.pb({w, a, b});
    }
    int q; cin >> q;
    sort(edge.begin(), edge.end());
    vector<tuple<ll, int, int>> query;
    for(int i = 0; i < q; i++){
        int tt, s; ll w;
        cin >> tt >> s >> w;
        s--;
        if(tt == 2) query.pb({w, s, i});
    }
    vector<int> ans(q);
    sort(query.rbegin(), query.rend());
    for(auto [w, s, in]: query){
        while(!edge.empty() && get<0>(edge.back()) >= w){
            auto [ww, a, b] = edge.back(); edge.pop_back();
            ds.unite(a, b);
        }
        ans[in] = ds.comp[ds.id[s]].size();
    }
    for(int x: ans) cout << x << "\n";
}
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...