結果

問題 No.3756 Udon Network
ユーザー GOTKAKO
提出日時 2026-10-24 14:58:33
言語 C++17
(gcc 15.3.0 + boost 1.92.0 + ACL)
コンパイル:
g++-15 -O2 -lm -std=c++17 -Wuninitialized -DONLINE_JUDGE -o a.out _filename_
実行:
./a.out
結果
AC  
実行時間 676 ms / 2,000 ms
+ 581µs
コード長 2,320 bytes
記録
記録タグの例:
初AC ショートコード 純ショートコード 純主流ショートコード 最速実行時間
コンパイル時間 2,155 ms
コンパイル使用メモリ 252,756 KB
実行使用メモリ 84,616 KB
最終ジャッジ日時 2026-10-24 14:59:01
合計ジャッジ時間 20,875 ms
ジャッジサーバーID
(参考情報)
judge3_0 / judge4_0
純コード判定待ち
このコードへのチャレンジ
(要ログイン)
サブタスク 配点 結果
Example 0 % AC * 8
Subtask $1$ 2 % AC * 15
Subtask $2$ 4 % AC * 22
Subtask $3$ 8 % AC * 9
Subtask $4$ 16 % AC * 10
Subtask $5$ 32 % AC * 10
Subtask $6$ 38 % AC * 53
合計 4 * 100% = 400 点
権限があれば一括ダウンロードができます

ソースコード

diff #
raw source code

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

class UnionFind{
    private:
    vector<int> par,P;
    vector<set<int>> S;
    vector<set<pair<int,int>>> Query;
    public:
    UnionFind(vector<int> A,vector<set<pair<int,int>>> Q){
        int N = A.size(); Query = Q;
        par.resize(N,-1),P.resize(N),S.resize(N);
        iota(P.begin(),P.end(),0);
        for(int i=0; i<N; i++) S.at(i).insert(A.at(i));
    }
 
    int root(int x){ //連結成分の代表頂点を返す.
        if(par.at(x) == -1) return x;
        else return par.at(x) = root(par.at(x));
    }
    vector<int> unite(int u, int v){ //u,vを連結する 連結してた->false,した->trueを返す.
        u = root(u),v = root(v);
        if(u == v) return {};
 
        if(S.at(u).size() < S.at(v).size()) swap(u,v); //Union by size.
        par.at(v) = u;
        for(auto &s : S.at(v)) S.at(u).insert(s);
        S.at(v).clear();
        int pu = P.at(u),pv = P.at(v);
        if(Query.at(pu).size() < Query.at(pv).size()) swap(pu,pv);
        P.at(u) = pu;
        for(auto &q : Query.at(pv)) Query.at(pu).insert(q);
        Query.at(pv).clear();

        vector<int> ret;
        vector<pair<int,int>> era;
        auto itr = Query.at(pu).begin();
        while(itr != Query.at(pu).end()){
            auto [c,pos] = *itr; itr++;
            if(c <= S.at(u).size()) ret.push_back(pos),era.push_back({c,pos});
            else break;
        }
        for(auto e : era) Query.at(pu).erase(e);

        return ret;
    }
    bool issame(int u, int v){ //同じ連結成分ならtrue.
        if(root(u) == root(v)) return true;
        else return false;
    }
};

int main() {
    ios_base::sync_with_stdio(false);
    cin.tie(nullptr);

    int N,M,Q; cin >> N >> M >> Q;
    vector<int> A(N);
    for(auto &a : A) cin >> a,a--;
    vector<tuple<int,int,int>> edge(M);
    for(auto &[c,a,b] : edge) cin >> a >> b >> c,a--,b--;
    sort(edge.begin(),edge.end());

    vector<int> answer(Q,-1);
    vector<set<pair<int,int>>> Query(N);
    for(int i=0; i<Q; i++){
        int s,c; cin >> s >> c,s--;
        if(c == 1) answer.at(i) = 0;
        else Query.at(s).insert({c,i});
    }
    UnionFind Z(A,Query);
    for(auto [c,a,b] : edge) for(auto pos : Z.unite(a,b)) answer.at(pos) = c;
    
    for(auto a : answer) cout << a << "\n";
}
0