제출 #376895

#제출 시각아이디문제언어결과실행 시간메모리
376895KoD다리 (APIO19_bridges)C++17
30 / 100
3051 ms5864 KiB
#include <bits/stdc++.h>

template <class T>
using Vec = std::vector<T>;

struct DSU {
    Vec<int> par;
    std::stack<std::pair<int, int>> past;
    DSU(const int n): par(n, -1) { }
    int find(int u) const {
        while (par[u] >= 0) {
            u = par[u];
        }
        return u;
    }
    int merge(int x, int y) {
        x = find(x);
        y = find(y);
        if (x == y) {
            return 0;
        }
        if (par[x] > par[y]) {
            std::swap(x, y);
        }
        past.emplace(x, par[x]);
        past.emplace(y, par[y]);
        par[x] += par[y];
        par[y] = x;
        return 2;
    }
    int comp(const int x) const {
        return -par[find(x)];
    }
    void roll(int count) {
        while (count--) {
            const auto [i, v] = past.top();
            past.pop();
            par[i] = v;
        }
    }
};

int scan() {
    int x;
    std::scanf("%d", &x);
    return x;
}

constexpr int BSIZE = 300;

int main() {
    const auto N = scan();
    const auto M = scan();
    Vec<int> U(M), V(M), D(M);
    for (int i = 0; i < M; ++i) {
        U[i] = scan() - 1;
        V[i] = scan() - 1;
        D[i] = scan();
    }
    const auto Q = scan();
    Vec<int> T(Q), K(Q), W(Q);
    for (int i = 0; i < Q; ++i) {
        T[i] = scan();
        K[i] = scan() - 1;
        W[i] = scan();
    }
    const auto Steps = (Q + BSIZE - 1) / BSIZE;
    Vec<int> ans;
    ans.reserve(Q);
    for (int step = 0; step < Steps; ++step) {
        const auto qL = step * BSIZE;
        const auto qR = std::min((step + 1) * BSIZE, Q);
        Vec<int> set;
        Vec<std::pair<int, int>> ask;
        for (int i = qL; i < qR; ++i) {
            if (T[i] == 1) {
                set.push_back(i);
            }
            else {
                ask.emplace_back(i, (int) ans.size());
                ans.push_back(0);
            }
        }
        std::sort(ask.begin(), ask.end(), [&](const auto &q1, const auto &q2) {
            return W[q1.first] > W[q2.first];
        });
        Vec<int> still, change;
        still.reserve(M);
        {
            Vec<bool> tmp(M);
            for (const auto i: set) {
                tmp[K[i]] = true;
            }
            for (int i = 0; i < M; ++i) {
                (tmp[i] ? change : still).push_back(i);
            }
        }
        std::sort(still.begin(), still.end(), [&](const int i, const int j) {
            return D[i] < D[j];
        });
        DSU dsu(N);
        for (const auto [idx, ptr]: ask) {
            while (!still.empty() && W[idx] <= D[still.back()]) {
                const auto i = still.back();
                still.pop_back();
                dsu.merge(U[i], V[i]);
            }
            std::stack<std::pair<int, int>> past;
            for (const auto i: set) {
                if (i > idx) {
                    break;
                }
                past.emplace(K[i], D[K[i]]);
                D[K[i]] = W[i];
            }
            int count = 0;
            for (const auto i: change) {
                if (W[idx] <= D[i]) {
                    count += dsu.merge(U[i], V[i]);
                }
            }
            ans[ptr] = dsu.comp(K[idx]);
            dsu.roll(count);
            while (!past.empty()) {
                const auto [i, v] = past.top();
                past.pop();
                D[i] = v;
            }
        }
        for (const auto i: set) {
            D[K[i]] = W[i];
        }
    }
    for (const auto x: ans) {
        std::printf("%d\n", x);
    }
    return 0;
}

컴파일 시 표준 에러 (stderr) 메시지

bridges.cpp: In function 'int scan()':
bridges.cpp:45:15: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
   45 |     std::scanf("%d", &x);
      |     ~~~~~~~~~~^~~~~~~~~~
#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...