結果
| 問題 | No.1293 2種類の道路 | 
| コンテスト | |
| ユーザー |  m_tsubasa | 
| 提出日時 | 2020-11-20 21:42:57 | 
| 言語 | C++17 (gcc 13.3.0 + boost 1.87.0) | 
| 結果 | 
                                AC
                                 
                             | 
| 実行時間 | 168 ms / 2,000 ms | 
| コード長 | 1,239 bytes | 
| コンパイル時間 | 2,437 ms | 
| コンパイル使用メモリ | 207,624 KB | 
| 最終ジャッジ日時 | 2025-01-16 02:14:44 | 
| ジャッジサーバーID (参考情報) | judge1 / judge1 | 
(要ログイン)
| ファイルパターン | 結果 | 
|---|---|
| sample | AC * 2 | 
| other | AC * 22 | 
ソースコード
#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;
}
            
            
            
        