結果

問題 No.1194 Replace
ユーザー MisterMister
提出日時 2020-08-22 15:02:53
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 574 ms / 2,000 ms
コード長 2,402 bytes
コンパイル時間 1,363 ms
コンパイル使用メモリ 105,580 KB
実行使用メモリ 53,424 KB
最終ジャッジ日時 2024-04-23 09:25:53
合計ジャッジ時間 11,820 ms
ジャッジサーバーID
(参考情報)
judge2 / judge1
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 352 ms
36,160 KB
testcase_01 AC 391 ms
38,244 KB
testcase_02 AC 307 ms
31,592 KB
testcase_03 AC 238 ms
27,200 KB
testcase_04 AC 381 ms
38,684 KB
testcase_05 AC 360 ms
35,620 KB
testcase_06 AC 327 ms
33,528 KB
testcase_07 AC 533 ms
53,164 KB
testcase_08 AC 536 ms
53,424 KB
testcase_09 AC 545 ms
53,276 KB
testcase_10 AC 565 ms
53,320 KB
testcase_11 AC 574 ms
53,316 KB
testcase_12 AC 558 ms
53,224 KB
testcase_13 AC 291 ms
25,092 KB
testcase_14 AC 242 ms
21,576 KB
testcase_15 AC 206 ms
21,396 KB
testcase_16 AC 277 ms
24,872 KB
testcase_17 AC 194 ms
19,776 KB
testcase_18 AC 187 ms
19,088 KB
testcase_19 AC 273 ms
25,800 KB
testcase_20 AC 2 ms
6,940 KB
testcase_21 AC 2 ms
6,940 KB
testcase_22 AC 2 ms
6,944 KB
testcase_23 AC 78 ms
11,444 KB
testcase_24 AC 25 ms
6,944 KB
testcase_25 AC 10 ms
6,940 KB
testcase_26 AC 95 ms
11,376 KB
testcase_27 AC 15 ms
6,944 KB
testcase_28 AC 40 ms
7,256 KB
testcase_29 AC 4 ms
6,944 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