結果

問題 No.922 東北きりきざむたん
ユーザー face4face4
提出日時 2019-11-19 21:08:24
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 218 ms / 2,000 ms
コード長 2,695 bytes
コンパイル時間 979 ms
コンパイル使用メモリ 75,508 KB
実行使用メモリ 24,356 KB
最終ジャッジ日時 2023-07-27 13:02:01
合計ジャッジ時間 7,983 ms
ジャッジサーバーID
(参考情報)
judge11 / judge12
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 5 ms
12,160 KB
testcase_01 AC 5 ms
12,096 KB
testcase_02 AC 5 ms
12,076 KB
testcase_03 AC 5 ms
12,392 KB
testcase_04 AC 6 ms
12,048 KB
testcase_05 AC 5 ms
12,088 KB
testcase_06 AC 6 ms
12,044 KB
testcase_07 AC 5 ms
12,096 KB
testcase_08 AC 6 ms
12,220 KB
testcase_09 AC 55 ms
15,204 KB
testcase_10 AC 50 ms
12,884 KB
testcase_11 AC 57 ms
14,544 KB
testcase_12 AC 19 ms
14,476 KB
testcase_13 AC 20 ms
12,884 KB
testcase_14 AC 82 ms
16,352 KB
testcase_15 AC 10 ms
14,432 KB
testcase_16 AC 169 ms
17,716 KB
testcase_17 AC 172 ms
17,936 KB
testcase_18 AC 170 ms
17,920 KB
testcase_19 AC 169 ms
17,732 KB
testcase_20 AC 171 ms
17,584 KB
testcase_21 AC 193 ms
17,900 KB
testcase_22 AC 193 ms
17,844 KB
testcase_23 AC 214 ms
17,376 KB
testcase_24 AC 218 ms
17,756 KB
testcase_25 AC 186 ms
18,232 KB
testcase_26 AC 181 ms
18,160 KB
testcase_27 AC 188 ms
18,168 KB
testcase_28 AC 61 ms
15,088 KB
testcase_29 AC 188 ms
24,356 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