結果

問題 No.922 東北きりきざむたん
ユーザー face4face4
提出日時 2019-11-19 21:08:24
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 198 ms / 2,000 ms
コード長 2,695 bytes
コンパイル時間 895 ms
コンパイル使用メモリ 75,760 KB
実行使用メモリ 24,576 KB
最終ジャッジ日時 2024-04-15 01:06:39
合計ジャッジ時間 5,443 ms
ジャッジサーバーID
(参考情報)
judge2 / judge5
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 5 ms
12,692 KB
testcase_01 AC 4 ms
13,208 KB
testcase_02 AC 5 ms
13,052 KB
testcase_03 AC 6 ms
13,312 KB
testcase_04 AC 5 ms
12,492 KB
testcase_05 AC 6 ms
13,532 KB
testcase_06 AC 6 ms
12,900 KB
testcase_07 AC 5 ms
14,108 KB
testcase_08 AC 6 ms
12,484 KB
testcase_09 AC 54 ms
15,868 KB
testcase_10 AC 52 ms
13,248 KB
testcase_11 AC 56 ms
15,064 KB
testcase_12 AC 18 ms
15,096 KB
testcase_13 AC 20 ms
13,660 KB
testcase_14 AC 80 ms
16,820 KB
testcase_15 AC 9 ms
14,868 KB
testcase_16 AC 164 ms
17,752 KB
testcase_17 AC 169 ms
17,884 KB
testcase_18 AC 167 ms
17,752 KB
testcase_19 AC 198 ms
17,880 KB
testcase_20 AC 169 ms
17,748 KB
testcase_21 AC 184 ms
18,056 KB
testcase_22 AC 180 ms
17,900 KB
testcase_23 AC 193 ms
17,728 KB
testcase_24 AC 194 ms
17,724 KB
testcase_25 AC 173 ms
18,392 KB
testcase_26 AC 173 ms
18,520 KB
testcase_27 AC 174 ms
18,392 KB
testcase_28 AC 66 ms
15,124 KB
testcase_29 AC 178 ms
24,576 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include<iostream>
#include<vector>
using namespace std;

struct UF{
    vector<int> p;
    int n;

    UF(int n) : n(n){
        p.resize(n);
        for(int i = 0; i < n; i++)  p[i] = i;
    }

    int parent(int x){
        if(p[x] != x)   p[x] = parent(p[x]);
        return p[x];
    }

    bool same(int x, int y){
        return parent(x)==parent(y);
    }

    void unite(int x, int y){
        x = parent(x), y = parent(y);
        if(x != y)  p[x] = y;
    }
};

vector<int> parents;
vector<int> v[100000], depth(100000, -1);
int n;
int par[20][100000];

void dfs(int now, int p, int d){
    par[0][now] = p;
    depth[now] = d;
    for(int child : v[now]){
        if(child == p)  continue;
        dfs(child, now, d+1);
    }
}

void init(){
    for(int i = 0; i < n; i++){
        if(depth[i] == -1)  parents.push_back(i), dfs(i, -1, 0);
    }
    for(int k = 0; k+1 < 20; k++){
        for(int i = 0; i < n; i++){
            if(par[k][i] < 0)   par[k+1][i] = -1;
            else                par[k+1][i] = par[k][par[k][i]];
        }
    }
}

int lca(int u, int v){
    if(depth[u] > depth[v]) swap(u, v);
    for(int k = 19; k >= 0; k--){
        if(((depth[v]-depth[u])>>k)&1){
            v = par[k][v];
        }
    }
    if(u == v)  return u;
    for(int k = 19; k >= 0; k--){
        if(par[k][u] != par[k][v]){
            u = par[k][u];
            v = par[k][v];
        }
    }
    return par[0][u];
}

typedef long long ll;
vector<int> query(100000, 0);
ll cost = 0;

int updateChildren(int now, int p){
    cost += depth[now]*query[now];
    for(int child : v[now]){
        if(child != p){
            query[now] += updateChildren(child, now);
        }
    }
    return query[now];
}

ll dfs3(ll c, int now, int p, int &queryN){
    ll ret = c;
    for(int child : v[now]){
        if(child != p){
            ret = min(ret, dfs3(c-query[child]+(queryN-query[child]), child, now, queryN));
        }
    }
    return ret;
}

ll dfs2(int now){
    ll tmp = 0;
    cost = 0;
    int x = updateChildren(now, -1);
    return dfs3(cost, now, -1, x);
}

int main(){
    int m, q;
    cin >> n >> m >> q;
    UF uf(n);
    for(int i = 0; i < m; i++){
        int a, b;
        cin >> a >> b;
        a--, b--;
        v[a].push_back(b);
        v[b].push_back(a);
        uf.unite(a, b);
    }
    init();
    ll ans = 0;
    while(q-- > 0){
        int a, b;
        cin >> a >> b;
        a--, b--;
        if(uf.same(a, b)){
            int l = lca(a, b);
            ans += depth[a]-depth[l] + depth[b]-depth[l];
        }else{
            query[a]++, query[b]++;
        }
    }
    for(int p : parents)    ans += dfs2(p);
    cout << ans << endl;
    return 0;
}
0