結果

問題 No.1293 2種類の道路
ユーザー m_tsubasam_tsubasa
提出日時 2020-11-20 21:42:57
言語 C++17
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 129 ms / 2,000 ms
コード長 1,239 bytes
コンパイル時間 2,390 ms
コンパイル使用メモリ 212,940 KB
実行使用メモリ 9,528 KB
最終ジャッジ日時 2023-09-30 19:05:20
合計ジャッジ時間 5,172 ms
ジャッジサーバーID
(参考情報)
judge11 / judge15
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,376 KB
testcase_01 AC 1 ms
4,380 KB
testcase_02 AC 2 ms
4,380 KB
testcase_03 AC 2 ms
4,380 KB
testcase_04 AC 1 ms
4,380 KB
testcase_05 AC 2 ms
4,380 KB
testcase_06 AC 1 ms
4,376 KB
testcase_07 AC 2 ms
4,376 KB
testcase_08 AC 1 ms
4,384 KB
testcase_09 AC 128 ms
5,920 KB
testcase_10 AC 129 ms
6,236 KB
testcase_11 AC 128 ms
5,920 KB
testcase_12 AC 128 ms
5,968 KB
testcase_13 AC 129 ms
5,972 KB
testcase_14 AC 83 ms
9,528 KB
testcase_15 AC 84 ms
9,400 KB
testcase_16 AC 110 ms
7,240 KB
testcase_17 AC 120 ms
8,036 KB
testcase_18 AC 77 ms
8,560 KB
testcase_19 AC 92 ms
9,400 KB
testcase_20 AC 91 ms
9,464 KB
testcase_21 AC 77 ms
4,380 KB
testcase_22 AC 76 ms
4,380 KB
testcase_23 AC 77 ms
4,380 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