結果

問題 No.922 東北きりきざむたん
ユーザー finefine
提出日時 2019-11-08 22:48:46
言語 C++14
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 183 ms / 2,000 ms
コード長 4,171 bytes
コンパイル時間 1,842 ms
コンパイル使用メモリ 174,248 KB
実行使用メモリ 36,308 KB
最終ジャッジ日時 2023-10-13 04:26:17
合計ジャッジ時間 5,726 ms
ジャッジサーバーID
(参考情報)
judge13 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 4 ms
10,656 KB
testcase_01 AC 4 ms
10,824 KB
testcase_02 AC 4 ms
10,732 KB
testcase_03 AC 4 ms
10,948 KB
testcase_04 AC 4 ms
10,780 KB
testcase_05 AC 5 ms
10,908 KB
testcase_06 AC 5 ms
11,104 KB
testcase_07 AC 5 ms
10,836 KB
testcase_08 AC 4 ms
10,848 KB
testcase_09 AC 49 ms
18,084 KB
testcase_10 AC 28 ms
12,824 KB
testcase_11 AC 42 ms
16,592 KB
testcase_12 AC 23 ms
18,544 KB
testcase_13 AC 13 ms
12,596 KB
testcase_14 AC 75 ms
22,540 KB
testcase_15 AC 18 ms
19,252 KB
testcase_16 AC 152 ms
23,716 KB
testcase_17 AC 149 ms
23,728 KB
testcase_18 AC 146 ms
23,644 KB
testcase_19 AC 149 ms
23,544 KB
testcase_20 AC 148 ms
23,564 KB
testcase_21 AC 171 ms
24,348 KB
testcase_22 AC 170 ms
24,064 KB
testcase_23 AC 183 ms
23,720 KB
testcase_24 AC 181 ms
23,608 KB
testcase_25 AC 142 ms
24,948 KB
testcase_26 AC 139 ms
24,952 KB
testcase_27 AC 143 ms
24,880 KB
testcase_28 AC 38 ms
21,072 KB
testcase_29 AC 156 ms
36,308 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>

using namespace std;

using ll = long long;

const int MAX_V = 100010;
const int MAX_LOG_V = 17;

vector<int> G[MAX_V];
int root = 0;

int parent[MAX_LOG_V][MAX_V];
int depth[MAX_V];

void dfs_(int v, int p, int d) {
    parent[0][v] = p;
    depth[v] = d;
    for (int i = 0; i < G[v].size(); i++) {
        if (G[v][i] != p) dfs_(G[v][i], v, d + 1);
    }
}

void init(int V) {
    dfs_(root, -1, 0);
    for (int k = 0; k + 1 < MAX_LOG_V; 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]];
        }
    }
}

int lca(int u, int v) {
    if (depth[u] > depth[v]) swap(u, v);
    for (int k = 0; k < MAX_LOG_V; k++) {
        if ((depth[v] - depth[u]) >> k & 1) {
            v = parent[k][v];
        }
    }
    if (u == v) return u;
    for (int k = MAX_LOG_V - 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);
    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);
    }

    root = n;
    for (int i = 0; i < n; i++) {
        if (uf.find(i) != i) continue;
        G[n].push_back(i);
        G[i].push_back(n);
    }
    init(n + 1);

    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