結果

問題 No.1293 2種類の道路
ユーザー kyoprounokyoprouno
提出日時 2020-11-20 23:25:08
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
WA  
実行時間 -
コード長 4,077 bytes
コンパイル時間 2,417 ms
コンパイル使用メモリ 198,252 KB
実行使用メモリ 31,884 KB
最終ジャッジ日時 2023-09-30 20:07:12
合計ジャッジ時間 6,606 ms
ジャッジサーバーID
(参考情報)
judge15 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,384 KB
testcase_01 WA -
testcase_02 WA -
testcase_03 WA -
testcase_04 WA -
testcase_05 WA -
testcase_06 WA -
testcase_07 WA -
testcase_08 WA -
testcase_09 WA -
testcase_10 WA -
testcase_11 WA -
testcase_12 WA -
testcase_13 WA -
testcase_14 WA -
testcase_15 WA -
testcase_16 WA -
testcase_17 WA -
testcase_18 WA -
testcase_19 WA -
testcase_20 WA -
testcase_21 WA -
testcase_22 WA -
testcase_23 WA -
権限があれば一括ダウンロードができます
コンパイルメッセージ
main.cpp: 関数 ‘long long int dfs(long long int, bool, long long int)’ 内:
main.cpp:47:14: 警告: structured bindings only available with ‘-std=c++17’ or ‘-std=gnu++17’ [-Wc++17-extensions]
   47 |     for(auto [x,y]:to[v]){
      |              ^

ソースコード

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;

vector<vector<pll>> to;

vector<vector<ll>> visited;
vector<vector<ll>> ans;

ll dfs(ll v,bool ok,ll p=-1){
    ll res=1;
    if(ok) visited[0][v]=1;
    else visited[1][v]=1;
    for(auto [x,y]:to[v]){
        if(x==p) continue;
        if(visited[0][x] || visited[1][x]) continue;
        if(y==0){
            if(ans[1][v]!=0){
                res+=ans[1][v];
            }
            else{
                res+=dfs(x,false,v);
            }
        }
        if(y==1 && ok){
            if(ans[0][v]!=0){
                res+=ans[0][v];
            }
            else{
                res+=dfs(x,true,v);
            }
        }
    }
    if(ok) return ans[0][v]=res;
    else return ans[1][v]=res;
}

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);
    to=vector<vector<pll>>(n);
    ans=vector<vector<ll>>(2,vector<ll>(n));
    visited=vector<vector<ll>>(2,vector<ll>(n));
    DisjointSet ds1=DisjointSet(n+1);
    DisjointSet ds2=DisjointSet(n+1);
    rep(i,D){
        cin >> a[i] >> b[i];
        a[i]--;
        b[i]--;
        to[a[i]].push_back({b[i],1});
        to[b[i]].push_back({a[i],1});
        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]--;
        to[c[i]].push_back({d[i],0});
        to[d[i]].push_back({c[i],0});
        if(!ds2.same(c[i],d[i])){
            ds2.unite(c[i],d[i]);
        }
    }
    ll sum=0;
    rep(i,n){
        if(ans[0][i]==0) ll s=dfs(i,true);
        if(ans[1][i]==0) ll t=dfs(i,false);
        sum+=ans[0][i]+ans[1][i];
    }
    rep(i,n){
        //cout << ans[0][i] << " " << ans[1][i] << endl;
    }
    map<ll,ll> s1,s2;
    rep(i,n){
        ll p1=ds1.findSet(i);
        if(!s1.count(p1)){
            sum-=ds1.sz[p1]-1;
            s1[p1]++;
        }
        ll p2=ds2.findSet(i);
        if(!s2.count(p2)){
            sum-=ds2.sz[p2]-1;
            s2[p2]++;
        }
    }
    cout << sum << endl;
    return 0;
}
0