結果

問題 No.922 東北きりきざむたん
ユーザー finefine
提出日時 2019-11-10 00:55:40
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 175 ms / 2,000 ms
コード長 4,511 bytes
コンパイル時間 2,022 ms
コンパイル使用メモリ 180,592 KB
実行使用メモリ 36,308 KB
最終ジャッジ日時 2023-10-13 07:37:31
合計ジャッジ時間 6,041 ms
ジャッジサーバーID
(参考情報)
judge14 / judge13
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,348 KB
testcase_01 AC 2 ms
4,352 KB
testcase_02 AC 2 ms
4,352 KB
testcase_03 AC 2 ms
4,348 KB
testcase_04 AC 2 ms
4,348 KB
testcase_05 AC 2 ms
4,348 KB
testcase_06 AC 2 ms
4,352 KB
testcase_07 AC 2 ms
4,352 KB
testcase_08 AC 2 ms
4,348 KB
testcase_09 AC 46 ms
15,252 KB
testcase_10 AC 24 ms
6,544 KB
testcase_11 AC 37 ms
12,396 KB
testcase_12 AC 24 ms
16,828 KB
testcase_13 AC 11 ms
6,556 KB
testcase_14 AC 80 ms
22,620 KB
testcase_15 AC 21 ms
18,008 KB
testcase_16 AC 134 ms
24,056 KB
testcase_17 AC 135 ms
23,884 KB
testcase_18 AC 133 ms
23,788 KB
testcase_19 AC 137 ms
23,784 KB
testcase_20 AC 140 ms
23,880 KB
testcase_21 AC 175 ms
24,360 KB
testcase_22 AC 160 ms
24,600 KB
testcase_23 AC 167 ms
23,796 KB
testcase_24 AC 169 ms
23,828 KB
testcase_25 AC 135 ms
25,144 KB
testcase_26 AC 136 ms
25,212 KB
testcase_27 AC 149 ms
25,176 KB
testcase_28 AC 43 ms
21,020 KB
testcase_29 AC 151 ms
36,308 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>

using namespace std;

using ll = long long;

struct LCA {
    using Graph = vector< vector<int> >;

    const Graph& g;
    const int LOGV;
    const int root;

    vector< vector<int> > parent;
    vector<int> depth;

    LCA(const Graph& g, int root = 0) : g(g), LOGV(32 - __builtin_clz(g.size())), root(root) {
        int V = g.size();
        parent.resize(LOGV, vector<int>(V));
        depth.resize(V);

        dfs(root, -1, 0);
        for (int k = 0; k + 1 < LOGV; k++) {
            for (int v = 0; v < V; v++) {
                if (parent[k][v] < 0) parent[k + 1][v] = -1;
                else parent[k + 1][v] = parent[k][parent[k][v]];
            }
        }
    }

    void dfs(int v, int p, int d) {
        parent[0][v] = p;
        depth[v] = d;
        for (int i : g[v]) {
            if (i != p) dfs(i, v, d + 1);
        }
    }

    int operator()(int u, int v) const {
        if (depth[u] > depth[v]) swap(u, v);
        for (int k = 0; k < LOGV; k++) {
            if ((depth[v] - depth[u]) >> k & 1) {
                v = parent[k][v];
            }
        }
        if (u == v) return u;
        for (int k = LOGV - 1; k >= 0; k--) {
            if (parent[k][u] != parent[k][v]) {
                u = parent[k][u];
                v = parent[k][v];
            }
        }
        return parent[0][u];
    }
};

struct UnionFind {
    //各要素が属する集合の代表(根)を管理する
    //もし、要素xが根であればdata[x]は負の値を取り、-data[x]はxが属する集合の大きさに等しい
    vector<int> data;

    UnionFind(int sz) : data(sz, -1) {}

    bool unite(int x, int y) {
        x = find(x);
        y = find(y);
        bool is_union = (x != y);
        if (is_union) {
            if (data[x] > data[y]) swap(x, y);
            data[x] += data[y];
            data[y] = x;
        }
        return is_union;
    }

    int find(int x) {
        if (data[x] < 0) { //要素xが根である
            return x;
        } else {
            data[x] = find(data[x]); //data[x]がxの属する集合の根でない場合、根になるよう更新される
            return data[x];
        }
    }

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

    int size(int x) {
        return -data[find(x)];
    }
};

void pre_dfs(int cur, int par, const vector< vector<int> >& g, const vector<ll>& cnt, vector<ll>& cnt_sub, vector<ll>& ans) {
    cnt_sub[cur] += cnt[cur];
    for (int nex : g[cur]) {
        if (nex == par) continue;
        pre_dfs(nex, cur, g, cnt, cnt_sub, ans);
        cnt_sub[cur] += cnt_sub[nex];
        ans[cur] += ans[nex] + cnt_sub[nex];
    }
}

void dfs(int cur, int par, const vector< vector<int> >& g, const vector<ll>& cnt_sub, const vector<ll>& ans, ll par_ans, ll par_cnt, ll& total_ans) {
    // cerr << cur << " " << par_ans + ans[cur] << " " << cnt_sub[cur] << "\n";
    total_ans = min(total_ans, ans[cur] + par_ans);
    for (int nex : g[cur]) {
        if (nex == par) continue;
        dfs(nex, cur, g, cnt_sub, ans, ans[cur] + par_ans - ans[nex] - cnt_sub[nex] + par_cnt + cnt_sub[cur] - cnt_sub[nex], par_cnt + cnt_sub[cur] - cnt_sub[nex], total_ans);
    }
}

const ll INF = 1e15;

int main() {
    cin.tie(0);
    ios::sync_with_stdio(false);
    int n, m, q;
    cin >> n >> m >> q;
    vector< vector<int> > g(n + 1), G(n + 1);
    UnionFind uf(n);
    for (int i = 0; i < m; i++) {
        int u, v;
        cin >> u >> v;
        u--; v--;
        uf.unite(u, v);
        g[u].push_back(v);
        g[v].push_back(u);
        G[u].push_back(v);
        G[v].push_back(u);
    }

    for (int i = 0; i < n; i++) {
        if (uf.find(i) != i) continue;
        G[n].push_back(i);
        G[i].push_back(n);
    }
    LCA lca(G, n);
    const vector<int>& depth = lca.depth;

    vector<ll> cnt(n, 0);
    ll final_ans = 0;
    for (int i = 0; i < q; i++) {
        int u, v;
        cin >> u >> v;
        u--; v--;
        if (!uf.same(u, v)) {
            cnt[u]++;
            cnt[v]++;
        } else {
            final_ans += depth[u] + depth[v] - depth[lca(u, v)] * 2;
        }
    }

    vector<ll> cnt_sub(n, 0);
    vector<ll> ans(n, 0);
    
    for (int i = 0; i < n; i++) {
        if (uf.find(i) != i) continue;
        pre_dfs(i, -1, g, cnt, cnt_sub, ans);
        ll total_ans = INF;
        dfs(i, -1, g, cnt_sub, ans, 0, 0, total_ans);
        final_ans += total_ans;
    }
    cout << final_ans << "\n";
    return 0;
}
0