結果

問題 No.1293 2種類の道路
ユーザー beetbeet
提出日時 2020-11-20 21:37:28
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 144 ms / 2,000 ms
コード長 1,613 bytes
コンパイル時間 2,099 ms
コンパイル使用メモリ 210,684 KB
実行使用メモリ 19,784 KB
最終ジャッジ日時 2023-09-30 19:02:30
合計ジャッジ時間 4,741 ms
ジャッジサーバーID
(参考情報)
judge14 / judge11
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,376 KB
testcase_01 AC 2 ms
4,376 KB
testcase_02 AC 2 ms
4,380 KB
testcase_03 AC 2 ms
4,376 KB
testcase_04 AC 1 ms
4,376 KB
testcase_05 AC 2 ms
4,376 KB
testcase_06 AC 2 ms
4,380 KB
testcase_07 AC 1 ms
4,376 KB
testcase_08 AC 2 ms
4,380 KB
testcase_09 AC 144 ms
19,784 KB
testcase_10 AC 139 ms
19,760 KB
testcase_11 AC 119 ms
19,728 KB
testcase_12 AC 116 ms
19,668 KB
testcase_13 AC 118 ms
19,608 KB
testcase_14 AC 87 ms
19,316 KB
testcase_15 AC 90 ms
19,156 KB
testcase_16 AC 95 ms
18,404 KB
testcase_17 AC 93 ms
18,496 KB
testcase_18 AC 66 ms
18,188 KB
testcase_19 AC 62 ms
18,568 KB
testcase_20 AC 62 ms
18,604 KB
testcase_21 AC 30 ms
4,380 KB
testcase_22 AC 30 ms
4,376 KB
testcase_23 AC 30 ms
4,380 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