結果

問題 No.1194 Replace
ユーザー MisterMister
提出日時 2020-08-22 14:59:59
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
RE  
実行時間 -
コード長 2,400 bytes
コンパイル時間 1,635 ms
コンパイル使用メモリ 106,336 KB
実行使用メモリ 52,260 KB
最終ジャッジ日時 2024-04-23 09:23:48
合計ジャッジ時間 17,477 ms
ジャッジサーバーID
(参考情報)
judge4 / judge3
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 RE -
testcase_01 RE -
testcase_02 RE -
testcase_03 RE -
testcase_04 RE -
testcase_05 RE -
testcase_06 RE -
testcase_07 RE -
testcase_08 RE -
testcase_09 RE -
testcase_10 RE -
testcase_11 RE -
testcase_12 RE -
testcase_13 WA -
testcase_14 WA -
testcase_15 RE -
testcase_16 WA -
testcase_17 RE -
testcase_18 WA -
testcase_19 WA -
testcase_20 AC 2 ms
5,376 KB
testcase_21 AC 2 ms
5,376 KB
testcase_22 AC 1 ms
5,376 KB
testcase_23 WA -
testcase_24 RE -
testcase_25 WA -
testcase_26 WA -
testcase_27 AC 16 ms
5,376 KB
testcase_28 WA -
testcase_29 RE -
権限があれば一括ダウンロードができます

ソースコード

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<int> 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 < n; ++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