結果

問題 No.3639 Itsukin
コンテスト
ユーザー ting shuo
提出日時 2026-08-25 18:11:28
言語 C++17
(gcc 15.2.0 + boost 1.90.0)
コンパイル:
g++-15 -O2 -lm -std=c++17 -Wuninitialized -DONLINE_JUDGE -o a.out _filename_
実行:
./a.out
結果
AC  
実行時間 155 ms / 2,000 ms
+ 857µs
コード長 1,792 bytes
記録
記録タグの例:
初AC ショートコード 純ショートコード 純主流ショートコード 最速実行時間
コンパイル時間 1,456 ms
コンパイル使用メモリ 227,004 KB
実行使用メモリ 14,332 KB
最終ジャッジ日時 2026-08-25 18:11:37
合計ジャッジ時間 7,350 ms
ジャッジサーバーID
(参考情報)
judge2_0 / judge1_0
このコードへのチャレンジ
(要ログイン)
サブタスク 配点 結果
サンプル 0 % AC * 1
小課題1 10 % AC * 6
小課題2 15 % AC * 5
小課題3 15 % AC * 16
小課題4 20 % AC * 10
小課題5 10 % AC * 15
小課題6 30 % AC * 35
合計 100 点
権限があれば一括ダウンロードができます

ソースコード

diff #
raw source code

#include <bits/stdc++.h>

using i64 = long long;
using u64 = unsigned long long;
using u32 = unsigned;

using u128 = unsigned __int128;
using i128 = __int128;

struct DSU {
    std::vector<int> f, siz;
    DSU() {}
    DSU(int n) {
        init(n);
    }
    void init(int n) {
        f.resize(n);
        siz.assign(n, 1);
        std::iota(f.begin(), f.end(), 0);
    }
    int find(int x) {
        while (x != f[x]) {
            x = f[x] = f[f[x]];
        }
        return x;
    }
    bool merge(int x, int y) {
        x = find(x);
        y = find(y);
        if (x == y) {
            return false;
        }
        f[y] = x;
        siz[x] += siz[y];
        return true;
    }
    int get_size(int x) {
        return siz[find(x)];
    }
};

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

    int N, M;
    std::cin >> N >> M;

    std::vector<std::array<int, 3>> adj, Queries;
    for (int i = 0; i < M; i++) {
        int u, v, l;
        std::cin >> u >> v >> l;
        u--;
        v--;
        adj.push_back({v, u, l});
    }
    std::sort(adj.begin(), adj.end(), [](std::array<int, 3>& a, std::array<int, 3>& b){
        return a[2] < b[2];
    });

    int Q;
    std::cin >> Q;
    std::vector<int> ans(Q);
    for (int i = 0; i < Q; i++) {
        int P, T;
        std::cin >> P >> T;
        T--;  //! 这里忘记转成0-based了
        Queries.push_back({P, T, i});
    }
    std::sort(Queries.begin(), Queries.end());

    DSU dsu(N);
    int j = 0;
    for (auto [p, t, idx] : Queries) {
        while (j < M && adj[j][2] <= p) {
            auto [u, v, l] = adj[j++];
            dsu.merge(u, v);
        }
        ans[idx] = dsu.get_size(t);
    }
    for (int x : ans) {
        std::cout << x << '\n';
    }

    return 0;
}
0