結果

問題 No.1293 2種類の道路
ユーザー m_tsubasam_tsubasa
提出日時 2020-11-20 21:42:57
言語 C++17
(gcc 11.2.0 + boost 1.78.0)
結果
AC  
実行時間 125 ms / 2,000 ms
コード長 1,239 bytes
コンパイル時間 2,059 ms
使用メモリ 9,488 KB
最終ジャッジ日時 2023-02-23 18:07:20
合計ジャッジ時間 4,963 ms
ジャッジサーバーID
(参考情報)
judge15 / judge11
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
使用メモリ
testcase_00 AC 1 ms
6,320 KB
testcase_01 AC 2 ms
6,260 KB
testcase_02 AC 1 ms
6,252 KB
testcase_03 AC 1 ms
6,176 KB
testcase_04 AC 1 ms
6,168 KB
testcase_05 AC 2 ms
6,208 KB
testcase_06 AC 2 ms
6,284 KB
testcase_07 AC 1 ms
6,268 KB
testcase_08 AC 1 ms
6,320 KB
testcase_09 AC 125 ms
6,200 KB
testcase_10 AC 125 ms
6,272 KB
testcase_11 AC 124 ms
6,208 KB
testcase_12 AC 125 ms
6,176 KB
testcase_13 AC 124 ms
6,256 KB
testcase_14 AC 80 ms
9,436 KB
testcase_15 AC 81 ms
9,488 KB
testcase_16 AC 106 ms
7,304 KB
testcase_17 AC 115 ms
8,112 KB
testcase_18 AC 75 ms
8,628 KB
testcase_19 AC 89 ms
9,488 KB
testcase_20 AC 90 ms
9,364 KB
testcase_21 AC 72 ms
6,248 KB
testcase_22 AC 76 ms
6,208 KB
testcase_23 AC 72 ms
6,248 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;

struct Unionfind {
  // tree number
  vector<int> par;
  // constructor
  Unionfind(int n = 1) : par(n, -1) {}
  // search root
  int root(int x) {
    if (par[x] < 0) return x;
    return par[x] = root(par[x]);
  }
  // is same?
  bool issame(int x, int y) { return root(x) == root(y); }

  // add
  // already added, return 0
  bool uni(int x, int y) {
    x = root(x);
    y = root(y);
    if (x == y) return 0;
    if (par[x] > par[y]) swap(x, y);
    par[x] += par[y];
    par[y] = x;
    return 1;
  }
  int size(int x) { return -par[root(x)]; }
};

using P = pair<int, int>;

long long n, d, w;
Unionfind ufd, ufw;
vector<long long> cnt;

int main() {
  cin >> n >> d >> w;
  ufd = ufw = Unionfind(n);
  for (int i = 0; i < d; ++i) {
    int x, y;
    cin >> x >> y;
    ufd.uni(--x, --y);
  }
  for (int i = 0; i < w; ++i) {
    int x, y;
    cin >> x >> y;
    ufw.uni(--x, --y);
  }
  cnt.assign(n, 0);
  set<P> st;
  for (int i = 0; i < n; ++i) {
    int f = ufd.root(i), t = ufw.root(i);
    if (!st.count(P(f, t))) cnt[f] += ufw.size(t);
    st.insert(P(f, t));
  }
  long long res = -n;
  for (int i = 0; i < n; ++i) res += cnt[ufd.root(i)];
  cout << res << endl;
  return 0;
}
0