結果

問題 No.1194 Replace
ユーザー risujirohrisujiroh
提出日時 2020-08-22 15:10:11
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
RE  
実行時間 -
コード長 1,439 bytes
コンパイル時間 3,469 ms
コンパイル使用メモリ 264,740 KB
実行使用メモリ 145,216 KB
最終ジャッジ日時 2024-04-23 09:33:37
合計ジャッジ時間 16,086 ms
ジャッジサーバーID
(参考情報)
judge3 / judge5
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
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 RE -
testcase_14 RE -
testcase_15 RE -
testcase_16 RE -
testcase_17 RE -
testcase_18 RE -
testcase_19 RE -
testcase_20 AC 2 ms
5,376 KB
testcase_21 AC 2 ms
5,376 KB
testcase_22 AC 2 ms
5,376 KB
testcase_23 RE -
testcase_24 RE -
testcase_25 RE -
testcase_26 RE -
testcase_27 AC 12 ms
5,376 KB
testcase_28 RE -
testcase_29 RE -
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/extc++.h>

#ifndef DUMP
#define DUMP(...) (void)0
#endif

using namespace std;

struct graph {
  struct edge {
    int src, dst;
    int operator-(int v) const { return src ^ dst ^ v; }
  };
  int n, m;
  vector<edge> edges;
  vector<vector<pair<int, int>>> adj;
  graph(int _n = 0) : n(_n), m(0), adj(n) {}
  int add(const edge& e, bool directed = false) {
    edges.push_back(e);
    adj[e.src].emplace_back(m, e.dst);
    if (not directed) adj[e.dst].emplace_back(m, e.src);
    return m++;
  }
};

int main() {
  cin.tie(nullptr)->sync_with_stdio(false);
  int n, m;
  cin >> n >> m;
  vector<int> b(m), c(m);
  for (int i = 0; i < m; ++i) {
    cin >> b[i] >> c[i];
  }
  auto x = b;
  x.insert(end(x), begin(c), end(c));
  sort(begin(x), end(x));
  x.erase(unique(begin(x), end(x)), end(x));
  graph g(size(x));
  for (int i = 0; i < m; ++i) {
    b[i] = lower_bound(begin(x), end(x), b[i]) - begin(x);
    c[i] = lower_bound(begin(x), end(x), c[i]) - begin(x);
    g.add({c[i], b[i]}, true);
  }
  auto res = (int64_t)n * (n + 1) / 2;
  vector<bool> vis(n);
  for (int s = n; s--;) {
    if (vis[s]) continue;
    auto dfs = [&](auto&& self, int v) -> void {
      res += x[s] - x[v];
      vis[v] = true;
      for (auto [id, u] : g.adj[v]) {
        if (vis[u]) continue;
        self(self, u);
      }
    };
    dfs(dfs, s);
  }
  cout << res << '\n';
}
0