結果

問題 No.922 東北きりきざむたん
ユーザー 🍮かんプリン🍮かんプリン
提出日時 2020-09-29 20:23:55
言語 C++11
(gcc 11.4.0)
結果
AC  
実行時間 303 ms / 2,000 ms
コード長 5,050 bytes
コンパイル時間 2,271 ms
コンパイル使用メモリ 186,192 KB
実行使用メモリ 49,500 KB
最終ジャッジ日時 2024-07-04 10:36:27
合計ジャッジ時間 7,737 ms
ジャッジサーバーID
(参考情報)
judge4 / judge1
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
5,248 KB
testcase_01 AC 2 ms
5,376 KB
testcase_02 AC 1 ms
5,376 KB
testcase_03 AC 2 ms
5,376 KB
testcase_04 AC 2 ms
5,376 KB
testcase_05 AC 2 ms
5,376 KB
testcase_06 AC 2 ms
5,376 KB
testcase_07 AC 2 ms
5,376 KB
testcase_08 AC 2 ms
5,376 KB
testcase_09 AC 94 ms
16,128 KB
testcase_10 AC 58 ms
6,912 KB
testcase_11 AC 83 ms
13,184 KB
testcase_12 AC 36 ms
17,920 KB
testcase_13 AC 22 ms
6,912 KB
testcase_14 AC 138 ms
23,868 KB
testcase_15 AC 24 ms
19,252 KB
testcase_16 AC 259 ms
25,232 KB
testcase_17 AC 258 ms
25,352 KB
testcase_18 AC 252 ms
25,356 KB
testcase_19 AC 264 ms
25,352 KB
testcase_20 AC 264 ms
25,232 KB
testcase_21 AC 303 ms
26,332 KB
testcase_22 AC 298 ms
26,200 KB
testcase_23 AC 300 ms
25,180 KB
testcase_24 AC 300 ms
25,180 KB
testcase_25 AC 249 ms
26,496 KB
testcase_26 AC 254 ms
26,476 KB
testcase_27 AC 257 ms
26,436 KB
testcase_28 AC 86 ms
22,448 KB
testcase_29 AC 281 ms
49,500 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

/**
 *   @FileName	a.cpp
 *   @Author	kanpurin
 *   @Created	2020.09.29 20:23:41
**/

#include "bits/stdc++.h" 
using namespace std; 
typedef long long ll;




struct LowestCommonAncestor {
private:
    int n;
    int log;
    vector<vector<int>> parent;
    vector<int> dep;
    vector<vector<int>> G;
    
    void dfs(const vector<vector<int>> &G, int v, int p, int d) {
        parent[0][v] = p;
        dep[v] = d;
        
        for (int to : G[v]) {
            if (to != p) dfs(G, to, v, d + 1);
            
        }
    }
public:
    LowestCommonAncestor(int n) : n(n) {
        G.resize(n);
    }
    void add_edge(int from, int to) {
        G[from].push_back(to);
        G[to].push_back(from);
    }
    
    void build(int root = 0) {
        log = log2(n) + 1;
        parent.resize(log, vector<int>(n));
        dep.resize(n);
        
        dfs(G, root, -1, 0);
        for (int k = 0; k + 1 < log; k++) {
            for (int v = 0; v < G.size(); v++) {
                if (parent[k][v] < 0) {
                    parent[k + 1][v] = -1;
                }
                else {
                    parent[k + 1][v] = parent[k][parent[k][v]];
                }
            }
        }
    }
    int depth(int v) {
        return dep[v];
    }
    
    int lca(int u, int v) {
        if (dep[u] > dep[v]) swap(u, v);
        for (int k = 0; k < log; k++) if ((dep[v] - dep[u]) >> k & 1) v = parent[k][v];
        if (u == v) return u;
        for (int k = log - 1; k >= 0; k--) {
            if (parent[k][u] != parent[k][v]) {
                u = parent[k][u];
                v = parent[k][v];
            }
        }
        return parent[0][u];
    }
    int dist(int u, int v) {
        return dep[u] + dep[v] - 2 * dep[lca(u, v)];
        
    }
};

class UnionFind {
private:
    vector<int> par;
public:
    UnionFind(int n) {
        par.resize(n, -1);
    }
    int root(int x) {
        if (par[x] < 0) return x;
        return par[x] = root(par[x]);
    }
    bool unite(int x, int y) {
        int rx = root(x);
        int ry = root(y);
        if (rx == ry) return false;
        if (size(rx) < size(ry)) swap(rx, ry);
        par[rx] += par[ry];
        par[ry] = rx;
        return true;
    }
    bool same(int x, int y) {
        int rx = root(x);
        int ry = root(y);
        return rx == ry;
    }
    int size(int x) {
        return -par[root(x)];
    }
};
using T = ll; 
int n; 
vector<vector<int>> tree;
vector<T> dist; 
vector<T> dist2; 
vector<T> ans;
vector<T> cost;
vector<bool> used;
void dfs1(int v, int p = -1) {
    used[v] = true;
    T res = 0, res2 = cost[v];
    for (auto &u : tree[v]) {
        if (u == p) continue;
        dfs1(u, v);
        res += dist[u] + dist2[u];
        res2 += dist2[u];
    }
    dist2[v] = res2;
    dist[v] = res;
}
T dfs2(int v, T d_par, T d_par2, int p = -1) {
    used[v] = true;
    
    ans[v] = d_par + dist[v] + d_par2;
    T res = ans[v];
    
    
    
    auto f = [](T a, T b) { return a + b; }; 
    T e = 0; 
    vector<T> lf(tree[v].size(), e), ri(tree[v].size(), e); 
    vector<T> lf2(tree[v].size(), e), ri2(tree[v].size(), e); 
    for (int i = 1; i < tree[v].size(); i++) {
        if (tree[v][i - 1] == p) lf[i] = f(d_par, lf[i - 1]);
        else lf[i] = f(dist[tree[v][i - 1]], lf[i - 1]);
        if (tree[v][i - 1] == p) lf2[i] = f(d_par2, lf2[i - 1]);
        else lf2[i] = f(dist2[tree[v][i - 1]], lf2[i - 1]);
    }
    for (int i = tree[v].size() - 2; i >= 0; i--) {
        if (tree[v][i + 1] == p) ri[i] = f(d_par, ri[i + 1]);
        else ri[i] = f(dist[tree[v][i + 1]], ri[i + 1]);
        
        if (tree[v][i + 1] == p) ri2[i] = f(d_par2, ri2[i + 1]);
        else ri2[i] = f(dist2[tree[v][i + 1]], ri2[i + 1]);
    }
    for (int i = 0; i < tree[v].size(); i++) {
        if (tree[v][i] == p) continue;
        res = min(res,dfs2(tree[v][i], f(lf[i], ri[i]) + f(lf2[i], ri2[i]), f(lf2[i], ri2[i]) + cost[v], v));
    }
    return res;
}
int main(){
    int m,q;cin >> n >> m >> q;
    tree.resize(n);
    dist.resize(n);
    dist2.resize(n);
    ans.resize(n);
    cost.resize(n,0);
    UnionFind uf(n),uf2(n);
    LowestCommonAncestor lca(n);
    ll sum = 0;
    for (int i = 0; i < m; i++) {
        int u, v; cin >> u >> v;
        u--; v--;
        tree[u].push_back(v);
        tree[v].push_back(u);
        uf.unite(u,v);
        uf2.unite(u,v);
        lca.add_edge(u,v);
    }
    for (int i = 1; i < n; i++) {
        if (!uf2.same(0,i)) uf2.unite(0,i),lca.add_edge(0,i);
    }
    lca.build();
    for (int i = 0; i < q; i++) {
        int a,b;cin >> a >> b;
        a--;b--;
        if (uf.same(a,b)) {
            sum += lca.dist(a,b);
        }
        else {
            cost[a]++;
            cost[b]++;
        }
    }
    used.resize(n,false);
    for (int i = 0; i < n; i++) {
        if (used[i]) continue;
        dfs1(i);
    }
    used.assign(n,false);
    for (int i = 0; i < n; i++) {
        if (used[i]) continue;
        sum += dfs2(i, 0, 0);
    }
    cout << sum << endl;
    return 0;
}
0