結果

問題 No.922 東北きりきざむたん
ユーザー ミドリムシミドリムシ
提出日時 2019-11-08 22:43:09
言語 C++14
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 384 ms / 2,000 ms
コード長 7,089 bytes
コンパイル時間 2,429 ms
コンパイル使用メモリ 189,248 KB
実行使用メモリ 35,484 KB
最終ジャッジ日時 2023-10-13 04:21:44
合計ジャッジ時間 7,627 ms
ジャッジサーバーID
(参考情報)
judge13 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
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 1 ms
4,348 KB
testcase_04 AC 2 ms
4,352 KB
testcase_05 AC 2 ms
4,352 KB
testcase_06 AC 2 ms
4,352 KB
testcase_07 AC 2 ms
4,352 KB
testcase_08 AC 2 ms
4,352 KB
testcase_09 AC 73 ms
17,948 KB
testcase_10 AC 53 ms
7,448 KB
testcase_11 AC 68 ms
14,840 KB
testcase_12 AC 30 ms
18,660 KB
testcase_13 AC 20 ms
7,272 KB
testcase_14 AC 112 ms
26,404 KB
testcase_15 AC 21 ms
19,496 KB
testcase_16 AC 220 ms
29,236 KB
testcase_17 AC 223 ms
29,148 KB
testcase_18 AC 216 ms
29,372 KB
testcase_19 AC 218 ms
29,244 KB
testcase_20 AC 210 ms
29,328 KB
testcase_21 AC 250 ms
29,536 KB
testcase_22 AC 245 ms
29,424 KB
testcase_23 AC 278 ms
29,504 KB
testcase_24 AC 281 ms
29,340 KB
testcase_25 AC 211 ms
30,784 KB
testcase_26 AC 208 ms
30,652 KB
testcase_27 AC 213 ms
30,632 KB
testcase_28 AC 76 ms
23,160 KB
testcase_29 AC 384 ms
35,484 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;
using lint = long long;
const lint mod = 1e9 + 7;
#define all(x) (x).begin(), (x).end()
#define bitcount(n) __builtin_popcountl((lint)(n))
#define fcout cout << fixed << setprecision(15)
#define highest(x) (63 - __builtin_clzl(x))
template<class T> inline void YES(T condition){ if(condition) cout << "YES" << endl; else cout << "NO" << endl; }
template<class T> inline void Yes(T condition){ if(condition) cout << "Yes" << endl; else cout << "No" << endl; }
template<class T = string, class U = char>int character_count(T text, U character){ int ans = 0; for(U i: text){ ans += (i == character); } return ans; }
lint power(lint base, lint exponent, lint module){ if(exponent % 2){ return power(base, exponent - 1, module) * base % module; }else if(exponent){ lint root_ans = power(base, exponent / 2, module); return root_ans * root_ans % module; }else{ return 1; }}
struct position{ int y, x; }; position mv[4] = {{0, -1}, {1, 0}, {0, 1}, {-1, 0}}; // double euclidean(position first, position second){ return sqrt((second.x - first.x) * (second.x - first.x) + (second.y - first.y) * (second.y - first.y)); }
template<class T, class U> string to_string(pair<T, U> x){ return to_string(x.first) + "," + to_string(x.second); } string to_string(string x){ return x; }
template<class itr> void array_output(itr start, itr goal){ string ans; for(auto i = start; i != goal; i++) ans += to_string(*i) + " "; if(!ans.empty()) ans.pop_back(); cout << ans << endl; }
template<class itr> void cins(itr first, itr last){ for(auto i = first; i != last; i++){ cin >> (*i); } }
template<class T> T gcd(T a, T b){ if(a && b){ return gcd(min(a, b), max(a, b) % min(a, b)); }else{ return a; }} template<class T> T lcm(T a, T b){ return a / gcd(a, b) * b; }
struct combination{ vector<lint> fact, inv; combination(int sz) : fact(sz + 1), inv(sz + 1){ fact[0] = 1; for(int i = 1; i <= sz; i++){ fact[i] = fact[i - 1] * i % mod; } inv[sz] = power(fact[sz], mod - 2, mod); for(int i = sz - 1; i >= 0; i--){ inv[i] = inv[i + 1] * (i + 1) % mod; } } lint C(int p, int q) const{ if(q < 0 || p < q) return 0; return (fact[p] * inv[q] % mod * inv[p - q] % mod); } };
template<class itr> bool next_sequence(itr first, itr last, int max_bound){ itr now = last; while(now != first){ now--; (*now)++; if((*now) == max_bound){ (*now) = 0; }else{ return true; } } return false; }
template<class itr, class itr2> bool next_sequence2(itr first, itr last, itr2 first2, itr2 last2){ itr now = last; itr2 now2 = last2; while(now != first){ now--, now2--; (*now)++; if((*now) == (*now2)){ (*now) = 0; }else{ return true; } } return false; }

class LCA{
    vector<vector<int>> roads;
    bool is_built;
    
    int size;
    vector<int> depth;
    vector<int> parent;
    vector<vector<int>> ancestor;
    vector<bool> is_came;
    vector<int> group_number;
    
    void make_rooted(int vertex, int back, int depth_now, int group_now){
        depth[vertex] = depth_now;
        is_came[vertex] = true;
        group_number[vertex] = group_now;
        for(int i: roads[vertex]){
            if(i != back){
                parent[i] = vertex;
                make_rooted(i, vertex, depth_now + 1, group_now);
            }
        }
    }
    
public:
    int find_ancestor(int vertex, int height, int bit = 31){
        if(vertex == -1){
            return -1;
        }else if(bit == -1){
            return vertex;
        }else if((height >> bit) & 1){
            return find_ancestor(ancestor[bit][vertex], height, bit - 1);
        }else{
            return find_ancestor(vertex, height, bit - 1);
        }
    }
    
    LCA(int n): size(n), roads(n), is_built(false) {}
    
    void connect(int a, int b){
        is_built = false;
        roads[a].push_back(b);
        roads[b].push_back(a);
    }
    
    void build(){
        is_built = true;
        parent = vector<int>(size);
        depth = vector<int>(size);
        group_number = vector<int>(size);
        is_came = vector<bool>(size, false);
        int group_count = 0;
        for(int i = 0; i < size; i++){
            if(!is_came[i]){
                parent[i] = -1;
                make_rooted(i, -1, 0, group_count);
                group_count++;
            }
        }
        ancestor.resize(32, vector<int>(size));
        for(int i = 0; i < size; i++){
            ancestor[0][i] = parent[i];
        }
        for(int i = 1; i < 32; i++){
            for(int j = 0; j < size; j++){
                if(ancestor[i - 1][j] == -1){
                    ancestor[i][j] = -1;
                }else{
                    ancestor[i][j] = ancestor[i - 1][ancestor[i - 1][j]];
                }
            }
        }
    }
    
    bool is_same_group(int a, int b){
        return group_number[a] == group_number[b];
    }
    
    int dist(int a, int b){
        if(!is_built){
            cout << "ERROR : Not built!" << endl;
            exit(1);
        }
        if(group_number[a] != group_number[b]){
            return -1;
        }
        int above = min(depth[a], depth[b]);
        int low = -1, high = above;
        while(low + 1 < high){
            int mid = (low + high) / 2;
            if(find_ancestor(a, depth[a] - above + mid) == find_ancestor(b, depth[b] - above + mid)){
                high = mid;
            }else{
                low = mid;
            }
        }
        return high * 2 + max(depth[a], depth[b]) - above;
    }
};

int N;
vector<vector<int>> roads;
vector<lint> cnt;
lint sum;
vector<bool> is_came;

lint get_ans(int now, int back, int dist){
    lint ans = dist * cnt[now];
    for(int i: roads[now]){
        if(i == back){
            continue;
        }
        ans += get_ans(i, now, dist + 1);
    }
    return ans;
}

vector<lint> size;

lint get_size(int now, int back){
    lint ans = cnt[now];
    for(int i: roads[now]){
        if(i == back){
            continue;
        }
        ans += get_size(i, now);
    }
    return size[now] = ans;
}

lint dfs(int now, int back, lint here_ans){
    is_came[now] = true;
    lint ans = here_ans;
    for(int i: roads[now]){
        if(i == back){
            continue;
        }
        ans = min(ans, dfs(i, now, here_ans + sum - size[i] * 2));
    }
    return ans;
}

int main(){
    int M, Q;
    cin >> N >> M >> Q;
    roads.resize(N);
    LCA graph(N);
    for(int i = 0; i < M; i++){
        int a, b;
        cin >> a >> b;
        a--, b--;
        roads[a].push_back(b);
        roads[b].push_back(a);
        graph.connect(a, b);
    }
    cnt.resize(N, 0);
    lint ans = 0;
    graph.build();
    for(int i = 0; i < Q; i++){
        int a, b;
        cin >> a >> b;
        a--, b--;
        if(graph.is_same_group(a, b)){
            ans += graph.dist(a, b);
        }else{
            cnt[a]++, cnt[b]++;
        }
    }
    is_came.resize(N, false);
    size.resize(N);
    for(int i = 0; i < N; i++){
        if(!is_came[i] && roads[i].size() <= 1){
            sum = get_size(i, -1);
            ans += dfs(i, -1, get_ans(i, -1, 0));
        }
    }
    cout << ans << endl;
}

0