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