結果

問題 No.1293 2種類の道路
ユーザー kyoprounokyoprouno
提出日時 2020-11-20 23:57:37
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 158 ms / 2,000 ms
コード長 2,973 bytes
コンパイル時間 2,153 ms
コンパイル使用メモリ 195,948 KB
実行使用メモリ 25,288 KB
最終ジャッジ日時 2023-09-30 20:15:24
合計ジャッジ時間 5,215 ms
ジャッジサーバーID
(参考情報)
judge14 / judge11
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,376 KB
testcase_01 AC 2 ms
4,380 KB
testcase_02 AC 1 ms
4,376 KB
testcase_03 AC 2 ms
4,376 KB
testcase_04 AC 1 ms
4,380 KB
testcase_05 AC 1 ms
4,376 KB
testcase_06 AC 1 ms
4,380 KB
testcase_07 AC 1 ms
4,376 KB
testcase_08 AC 1 ms
4,380 KB
testcase_09 AC 151 ms
22,724 KB
testcase_10 AC 153 ms
22,464 KB
testcase_11 AC 156 ms
22,828 KB
testcase_12 AC 153 ms
22,468 KB
testcase_13 AC 158 ms
22,852 KB
testcase_14 AC 110 ms
25,288 KB
testcase_15 AC 110 ms
25,100 KB
testcase_16 AC 135 ms
23,212 KB
testcase_17 AC 142 ms
23,972 KB
testcase_18 AC 87 ms
21,792 KB
testcase_19 AC 79 ms
20,336 KB
testcase_20 AC 80 ms
20,224 KB
testcase_21 AC 80 ms
6,168 KB
testcase_22 AC 81 ms
6,264 KB
testcase_23 AC 80 ms
6,168 KB
権限があれば一括ダウンロードができます
コンパイルメッセージ
main.cpp: 関数 ‘int main()’ 内:
main.cpp:114:18: 警告: structured bindings only available with ‘-std=c++17’ or ‘-std=gnu++17’ [-Wc++17-extensions]
  114 |         for(auto [x,y]:s[i]){
      |                  ^
main.cpp:118:18: 警告: structured bindings only available with ‘-std=c++17’ or ‘-std=gnu++17’ [-Wc++17-extensions]
  118 |         for(auto [x,y]:m){
      |                  ^

ソースコード

diff #

#pragma GCC optimize("O3")
#include <bits/stdc++.h>
#define ll long long
#define rep(i,n) for(ll i=0;i<(n);i++)
#define pll pair<ll,ll>
#define pii pair<int,int>
#define pq priority_queue
#define pb push_back
#define eb emplace_back
#define fi first
#define se second
#define endl '\n'
#define ios ios_base::sync_with_stdio(0),cin.tie(0),cout.tie(0);
#define lb(c,x) distance(c.begin(),lower_bound(all(c),x))
#define ub(c,x) distance(c.begin(),upper_bound(all(c),x))

using namespace std;

inline int topbit(unsigned long long x){
	return x?63-__builtin_clzll(x):-1;
}

inline int popcount(unsigned long long x){
	return __builtin_popcountll(x);
}

inline int parity(unsigned long long x){//popcount%2
	return __builtin_parity(x);
}



template<class T> inline bool chmax(T& a,T b){if(a<b){a=b;return 1;}return 0;}
template<class T> inline bool chmin(T& a,T b){if(a>b){a=b;return 1;}return 0;}

const ll INF=1e9+7;

class DisjointSet{
    public:
    vector<ll> rank,p,sz;
    DisjointSet(){}
    DisjointSet(ll size){ //作られうる木の頂点数の最大値を入れる。
        rank.resize(size,0);
        p.resize(size,0);
        sz.resize(size,1);
        rep(i,size) makeSet(i);
    }
    void makeSet(ll x){ //xだけが属する木を作る。
        p[x]=x;
        rank[x]=0;
    }
    bool same(ll x,ll y){ //xとyが同じ木に属するかどうか
        return findSet(x)==findSet(y);
    }
    void unite(ll x, ll y){
        link(findSet(x),findSet(y));
    }
    void link(ll x, ll y){ //rankが大きい方の根に小さい方の根をつける。
        if(rank[x]>rank[y]){
            p[y]=x;
        }
        else{
            p[x]=y;
            if(rank[x]==rank[y]){
                rank[y]++; //xとyの木のrankが同じであれば、統合するとrankが1増える。
            }
        }
        sz[x]+=sz[y];
        sz[y]=sz[x];
    }
    ll findSet(ll x){ //xが属する木の根の番号を返す
        if(x!=p[x]){
            p[x]=findSet(p[x]);
        }
        return p[x];
    }
};

int main(){
    ll n,D,w;
    cin >> n >>D >> w;
    vector<ll> a(D),b(D);
    vector<ll> c(w),d(w);
    DisjointSet ds1=DisjointSet(n+1);
    DisjointSet ds2=DisjointSet(n+1);
    rep(i,D){
        cin >> a[i] >> b[i];
        a[i]--;
        b[i]--;
        if(!ds1.same(a[i],b[i])){
            ds1.unite(a[i],b[i]);
        }
    }
    rep(i,w){
        cin >> c[i] >> d[i];
        c[i]--;
        d[i]--;
        if(!ds2.same(c[i],d[i])){
            ds2.unite(c[i],d[i]);
        }
    }
    
    
    vector<set<pll>> s(n);
    rep(i,n){
        ll p1=ds1.findSet(i);
        ll p2=ds2.findSet(i);
        s[p1].insert({i,p2});
    }
    ll ans=0;
    rep(i,n){
        map<ll,ll> m;
        ll sum=0;
        for(auto [x,y]:s[i]){
            m[y]++;
        }
        ll all=s[i].size();
        for(auto [x,y]:m){
            sum+=ds2.sz[x];
        }
        ans+=sum*all;
    }
    ans-=n;
    cout << ans << endl;
}
0