結果

問題 No.1194 Replace
ユーザー risujirohrisujiroh
提出日時 2020-08-22 15:04:45
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
RE  
実行時間 -
コード長 1,097 bytes
コンパイル時間 3,478 ms
コンパイル使用メモリ 257,700 KB
実行使用メモリ 815,196 KB
最終ジャッジ日時 2024-04-23 09:27:11
合計ジャッジ時間 6,308 ms
ジャッジサーバーID
(参考情報)
judge3 / judge2
このコードへのチャレンジ
(要ログイン)

テストケース

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

ソースコード

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;
  graph g(n);
  while (m--) {
    int b, c;
    cin >> b >> c;
    --b, --c;
    g.add({c, b}, true);
  }
  int64_t res = 0;
  vector<bool> vis(n);
  for (int s = n; s--;) {
    if (vis[s]) continue;
    auto dfs = [&](auto&& self, int v) -> void {
      res += s + 1;
      vis[v] = true;
      for (auto [id, u] : g.adj[v]) {
        if (vis[u]) continue;
        self(self, u);
      }
    };
    dfs(dfs, s);
  }
  cout << res << '\n';
}
0