結果

問題 No.1293 2種類の道路
ユーザー beetbeet
提出日時 2020-11-20 21:37:28
言語 C++17
(gcc 11.2.0 + boost 1.78.0)
結果
AC  
実行時間 97 ms / 2,000 ms
コード長 1,613 bytes
コンパイル時間 1,736 ms
使用メモリ 19,740 KB
最終ジャッジ日時 2023-02-23 18:02:44
合計ジャッジ時間 3,986 ms
ジャッジサーバーID
(参考情報)
judge13 / judge15
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
使用メモリ
testcase_00 AC 1 ms
6,180 KB
testcase_01 AC 1 ms
6,184 KB
testcase_02 AC 2 ms
6,328 KB
testcase_03 AC 1 ms
6,284 KB
testcase_04 AC 1 ms
6,256 KB
testcase_05 AC 1 ms
6,284 KB
testcase_06 AC 1 ms
6,192 KB
testcase_07 AC 2 ms
6,272 KB
testcase_08 AC 1 ms
6,200 KB
testcase_09 AC 97 ms
19,628 KB
testcase_10 AC 92 ms
19,740 KB
testcase_11 AC 96 ms
19,640 KB
testcase_12 AC 93 ms
19,612 KB
testcase_13 AC 97 ms
19,648 KB
testcase_14 AC 76 ms
19,296 KB
testcase_15 AC 75 ms
19,196 KB
testcase_16 AC 77 ms
18,360 KB
testcase_17 AC 78 ms
18,436 KB
testcase_18 AC 55 ms
18,240 KB
testcase_19 AC 52 ms
18,600 KB
testcase_20 AC 50 ms
18,672 KB
testcase_21 AC 24 ms
6,272 KB
testcase_22 AC 23 ms
6,256 KB
testcase_23 AC 23 ms
6,280 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

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

using Int = long long;
const char newl = '\n';

template<typename T1,typename T2> inline void chmin(T1 &a,T2 b){if(a>b) a=b;}
template<typename T1,typename T2> inline void chmax(T1 &a,T2 b){if(a<b) a=b;}
template<typename T> void drop(const T &x){cout<<x<<endl;exit(0);}
template<typename T=Int>
vector<T> read(size_t n){
  vector<T> ts(n);
  for(size_t i=0;i<n;i++) cin>>ts[i];
  return ts;
}




struct QuickFind{
  vector<Int> rs,ps;
  vector< vector<Int> > vs;
  QuickFind(Int n):rs(n,1),ps(n),vs(n){
    iota(ps.begin(),ps.end(),0);
    for(Int i=0;i<n;i++) vs[i].assign(1,i);
  }
  Int find(Int x) const{return ps[x];}
  bool same(Int x,Int y) const{
    return find(x)==find(y);
  }
  void unite(Int x,Int y){
    x=ps[x];y=ps[y];
    if(x==y) return;
    if(rs[x]<rs[y]) swap(x,y);
    rs[x]+=rs[y];
    for(Int e:vs[y]){
      ps[e]=x;
      vs[x].emplace_back(e);
    }
    vs[y].clear();
    vs[y].shrink_to_fit();
  }
  const vector<Int>& elements(Int x) const{return vs[x];}
};

//INSERT ABOVE HERE
signed main(){
  cin.tie(0);
  ios::sync_with_stdio(0);

  Int n,d,w;
  cin>>n>>d>>w;

  QuickFind qf1(n),qf2(n);
  for(Int i=0;i<d;i++){
    Int a,b;
    cin>>a>>b;
    a--;b--;
    qf1.unite(a,b);
  }
  for(Int i=0;i<w;i++){
    Int c,d;
    cin>>c>>d;
    c--;d--;
    qf2.unite(c,d);
  }

  Int ans=0;
  for(Int i=0;i<n;i++){
    if(qf1.find(i)!=i) continue;
    set<Int> rs;
    for(Int v:qf1.elements(i))
      rs.emplace(qf2.find(v));
    Int num=0;
    for(Int r:rs) num+=qf2.rs[r];
    ans+=qf1.rs[i]*num;
  }
  cout<<ans-n<<newl;
  return 0;
}
0