結果

問題 No.1293 2種類の道路
ユーザー saxofone111saxofone111
提出日時 2021-03-08 11:46:37
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 193 ms / 2,000 ms
コード長 2,739 bytes
コンパイル時間 2,155 ms
コンパイル使用メモリ 210,604 KB
実行使用メモリ 24,276 KB
最終ジャッジ日時 2023-07-31 02:00:49
合計ジャッジ時間 7,493 ms
ジャッジサーバーID
(参考情報)
judge15 / judge14
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,380 KB
testcase_01 AC 2 ms
4,376 KB
testcase_02 AC 1 ms
4,380 KB
testcase_03 AC 1 ms
4,380 KB
testcase_04 AC 1 ms
4,384 KB
testcase_05 AC 2 ms
4,388 KB
testcase_06 AC 1 ms
4,380 KB
testcase_07 AC 2 ms
4,384 KB
testcase_08 AC 2 ms
4,376 KB
testcase_09 AC 177 ms
17,440 KB
testcase_10 AC 175 ms
17,172 KB
testcase_11 AC 178 ms
17,356 KB
testcase_12 AC 175 ms
17,732 KB
testcase_13 AC 180 ms
17,360 KB
testcase_14 AC 76 ms
15,712 KB
testcase_15 AC 77 ms
15,632 KB
testcase_16 AC 143 ms
16,580 KB
testcase_17 AC 139 ms
16,368 KB
testcase_18 AC 90 ms
16,492 KB
testcase_19 AC 189 ms
24,164 KB
testcase_20 AC 193 ms
24,276 KB
testcase_21 AC 78 ms
4,380 KB
testcase_22 AC 77 ms
4,376 KB
testcase_23 AC 78 ms
4,380 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include "bits/stdc++.h"

#define MOD 1000000007
#define rep(i, n) for(ll i=0; i < (n); i++)
#define rrep(i, n) for(ll i=(n)-1; i >=0; i--)
#define ALL(v) v.begin(),v.end()
#define rALL(v) v.rbegin(),v.rend()
#define FOR(i, j, k) for(ll i=j;i<k;i++)
#define debug_print(var) cerr << #var << "=" << var <<endl;
#define DUMP(i, v)for(ll i=0;i<v.size();i++)cerr<<v[i]<<" "
#define fi first
#define se second

using namespace std;
typedef long long int ll;
typedef vector<ll> llvec;
typedef vector<double> dvec;
typedef pair<ll, ll> P;
typedef long double ld;
struct edge{ll x, c;};

struct UnionFind{
  ll N;
  llvec p;
  llvec cnt;
  UnionFind(ll n){
    N = n;
    p=llvec(N);
    cnt=llvec(N, 0);
    rep(i, N){
      p[i] = i;
      cnt[i] = 1;
    }
  }

  void con(ll a, ll b){
    P at = root(a);
    P bt = root(b);
    if(at.second!=bt.second){
      if(at.first>bt.first){
        swap(at, bt);
        swap(a, b);
      }
      p[at.second] = bt.second;
      cnt[bt.second]+=cnt[at.second];
      p[a]=bt.second;
    }
  }

  P root(ll a){
    ll atmp = a;
    ll c=0;
    while(atmp!=p[atmp]){
      atmp = p[atmp];
      c++;
    }
    p[a] = atmp;
    return {c, atmp};
  }

  bool is_con(ll a, ll b){
    P at=root(a);
    P bt=root(b);
    return at.second == bt.second;
  }
  
};

struct UnionFind2{
  ll N;
  llvec p;
  llvec cnt;
  vector<set<ll>> type;
  UnionFind2(ll n){
    N = n;
    p=llvec(N);
    cnt=llvec(N, 0);
    type=vector<set<ll>>(N);
    rep(i, N){
      p[i] = i;
      cnt[i] = 1;
    }
  }

  void con(ll a, ll b){
    P at = root(a);
    P bt = root(b);
    if(at.second!=bt.second){
      if(cnt[at.se]>cnt[bt.se])swap(at,bt);
      p[at.second] = bt.second;
      cnt[bt.second]+=cnt[at.second];
      for(auto is: type[at.se]){
        type[bt.se].insert(is);
      }
      p[a]=bt.second;
    }
  }

  P root(ll a){
    ll atmp = a;
    ll c=0;
    while(atmp!=p[atmp]){
      atmp = p[atmp];
      c++;
    }
    p[a] = atmp;
    return {c, atmp};
  }

  bool is_con(ll a, ll b){
    P at=root(a);
    P bt=root(b);
    return at.second == bt.second;
  }
  
};


/**************************************
** A main function starts from here  **
***************************************/
int main(){
  ll N, D, W;
  cin >> N >> D >> W;
  UnionFind uf1(N);
  UnionFind2 uf2(N);
  rep(i, D){
    ll a, b;
    cin >> a >> b;
    a--;b--;
    uf1.con(a, b);
  }

  rep(i, N){
    auto p = uf1.root(i);
    uf2.type[i].insert(p.se);
  }
  
  rep(i, W){
    ll a, b;
    cin >> a >> b;
    a--;b--;
    uf2.con(a, b);
  }
  ll ans = 0;
  rep(i, N){
    if(uf2.p[i]==i){
      for(auto is: uf2.type[i]){
        ans = ans + uf1.cnt[is] * uf2.cnt[i];
      }
    }
  }
  cout << ans-N << endl;
  return 0;
}
0