結果

問題 No.1293 2種類の道路
ユーザー どららどらら
提出日時 2020-11-21 16:37:44
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 164 ms / 2,000 ms
コード長 1,724 bytes
コンパイル時間 2,172 ms
コンパイル使用メモリ 186,236 KB
実行使用メモリ 24,456 KB
最終ジャッジ日時 2023-09-30 21:59:08
合計ジャッジ時間 5,062 ms
ジャッジサーバーID
(参考情報)
judge13 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,376 KB
testcase_01 AC 1 ms
4,380 KB
testcase_02 AC 1 ms
4,376 KB
testcase_03 AC 2 ms
4,380 KB
testcase_04 AC 1 ms
4,380 KB
testcase_05 AC 2 ms
4,376 KB
testcase_06 AC 1 ms
4,380 KB
testcase_07 AC 2 ms
4,380 KB
testcase_08 AC 2 ms
4,380 KB
testcase_09 AC 146 ms
9,860 KB
testcase_10 AC 149 ms
10,004 KB
testcase_11 AC 150 ms
9,876 KB
testcase_12 AC 148 ms
9,804 KB
testcase_13 AC 146 ms
9,960 KB
testcase_14 AC 149 ms
24,456 KB
testcase_15 AC 154 ms
24,140 KB
testcase_16 AC 146 ms
15,148 KB
testcase_17 AC 164 ms
18,724 KB
testcase_18 AC 131 ms
20,524 KB
testcase_19 AC 135 ms
16,208 KB
testcase_20 AC 137 ms
16,144 KB
testcase_21 AC 79 ms
5,332 KB
testcase_22 AC 79 ms
5,464 KB
testcase_23 AC 80 ms
5,396 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;
#define REP(i,a,n) for(int i=(a); i<(int)(n); i++)
#define rep(i,n) REP(i,0,n)
#define FOR(it,c) for(__typeof((c).begin()) it=(c).begin(); it!=(c).end(); ++it)
#define ALLOF(c) (c).begin(), (c).end()
typedef long long ll;
typedef unsigned long long ull;

class UnionFind{
  int n;
  vector<int> data;
public:
  UnionFind(int _n):n(_n),data(_n,-1){}
  bool link(int a, int b){ //if new link, then true.
    int ra=find_root(a);
    int rb=find_root(b);
    if(ra!=rb){
      if(data[rb]<data[ra]) swap(ra,rb);
      data[ra]+=data[rb];
      data[rb]=ra;
    }
    return (ra!=rb);
  }
  bool check(int a, int b){ //if same set, then true.
    return (find_root(a)==find_root(b));
  }
  int find_root(int a){
    return ((data[a]<0)?a:(data[a]=find_root(data[a])));
  }
};


int main(){
  int N, D, W;
  cin >> N >> D >> W;
  vector<pair<int,int>> v, w;
  rep(i,D){
    int a, b;
    cin >> a >> b;
    a--;
    b--;
    v.emplace_back(a,b);
  }
  rep(i,W){
    int a, b;
    cin >> a >> b;
    a--;
    b--;
    w.emplace_back(a,b);
  }

  UnionFind red(N);
  rep(i,D){
    red.link(v[i].first, v[i].second);
  }
  UnionFind blue(N);
  rep(i,W){
    blue.link(w[i].first, w[i].second);
  }
  map<int,map<int,int>> belong_blue;
  map<int,int> num_blue, num_red;
  rep(i,N){
    belong_blue[blue.find_root(i)][red.find_root(i)]++;
    num_blue[blue.find_root(i)]++;
    num_red[red.find_root(i)]++;
  }

  ll ret = 0;
  FOR(it,belong_blue){
    int bi = it->first;
    map<int,int>& m = it->second;

    ll nb = num_blue[bi];
    ll nr = 0;
    FOR(it2,m){
      nr += num_red[it2->first];
    }
    ret += nb * nr;
  }

  ret -= N;
  
  cout << ret << endl;
  
  
  return 0;
}

0