結果

問題 No.1194 Replace
ユーザー MisterMister
提出日時 2020-08-22 15:02:53
言語 C++17
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 739 ms / 2,000 ms
コード長 2,402 bytes
コンパイル時間 1,269 ms
コンパイル使用メモリ 106,644 KB
実行使用メモリ 53,172 KB
最終ジャッジ日時 2023-08-05 12:16:11
合計ジャッジ時間 13,775 ms
ジャッジサーバーID
(参考情報)
judge12 / judge14
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 484 ms
35,800 KB
testcase_01 AC 528 ms
38,472 KB
testcase_02 AC 408 ms
31,928 KB
testcase_03 AC 325 ms
27,028 KB
testcase_04 AC 521 ms
37,940 KB
testcase_05 AC 485 ms
37,000 KB
testcase_06 AC 437 ms
33,392 KB
testcase_07 AC 732 ms
52,988 KB
testcase_08 AC 724 ms
52,996 KB
testcase_09 AC 731 ms
53,080 KB
testcase_10 AC 739 ms
53,172 KB
testcase_11 AC 726 ms
52,980 KB
testcase_12 AC 721 ms
52,984 KB
testcase_13 AC 401 ms
25,048 KB
testcase_14 AC 333 ms
22,296 KB
testcase_15 AC 287 ms
21,332 KB
testcase_16 AC 377 ms
24,640 KB
testcase_17 AC 255 ms
19,660 KB
testcase_18 AC 260 ms
18,652 KB
testcase_19 AC 390 ms
25,124 KB
testcase_20 AC 2 ms
4,384 KB
testcase_21 AC 2 ms
4,384 KB
testcase_22 AC 2 ms
4,380 KB
testcase_23 AC 106 ms
11,300 KB
testcase_24 AC 29 ms
6,440 KB
testcase_25 AC 11 ms
4,380 KB
testcase_26 AC 124 ms
11,000 KB
testcase_27 AC 16 ms
4,380 KB
testcase_28 AC 46 ms
7,140 KB
testcase_29 AC 5 ms
4,384 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
#include <algorithm>
#include <vector>
#include <queue>
#include <map>

template <class T>
std::map<T, int> compress(std::vector<T>& v) {
    std::sort(v.begin(), v.end());
    v.erase(std::unique(v.begin(), v.end()), v.end());

    std::map<T, int> rev;
    for (int i = 0; i < (int)v.size(); ++i) rev[v[i]] = i;
    return rev;
}

template <class Cost = int>
struct Edge {
    int src, dst;
    Cost cost;
    Edge(int src = -1, int dst = -1, Cost cost = 1)
        : src(src), dst(dst), cost(cost){};

    bool operator<(const Edge<Cost>& e) const { return this->cost < e.cost; }
    bool operator>(const Edge<Cost>& e) const { return this->cost > e.cost; }
};

template <class Cost = int>
struct Graph {
    std::vector<std::vector<Edge<Cost>>> graph;

    Graph(int n = 0) : graph(n) {}

    void span(bool direct, int src, int dst, Cost cost = 1) {
        graph[src].emplace_back(src, dst, cost);
        if (!direct) graph[dst].emplace_back(dst, src, cost);
    }

    int size() const { return graph.size(); }
    void clear() { graph.clear(); }
    void resize(int n) { graph.resize(n); }

    std::vector<Edge<Cost>>& operator[](int v) { return graph[v]; }
    std::vector<Edge<Cost>> operator[](int v) const { return graph[v]; }
};

using lint = long long;

void solve() {
    lint n;
    int m;
    std::cin >> n >> m;

    std::vector<std::pair<lint, lint>> es(m);
    std::vector<lint> xs;
    for (auto& [u, v] : es) {
        std::cin >> u >> v;
        xs.push_back(u);
        xs.push_back(v);
    }

    auto xrev = compress(xs);
    int nn = xs.size();

    Graph<> graph(nn);
    for (auto [u, v] : es) {
        graph.span(true, xrev[v], xrev[u]);
    }

    std::vector<lint> to(nn, -1);
    for (int i = nn - 1; i >= 0; --i) {
        if (to[i] != -1) continue;

        to[i] = xs[i];
        std::queue<int> que;
        que.push(i);

        while (!que.empty()) {
            int v = que.front();
            que.pop();

            for (auto e : graph[v]) {
                int u = e.dst;
                if (to[u] != -1) continue;
                to[u] = xs[i];
                que.push(u);
            }
        }
    }

    lint ans = n * (n + 1) / 2;
    for (int i = 0; i < nn; ++i) ans += to[i] - xs[i];
    std::cout << ans << "\n";
}

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

    solve();

    return 0;
}
0