QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#518734#3847. AirlinewisniewskijCompile Error//C++203.6kb2024-08-14 03:40:172024-08-14 03:40:17

Judging History

你现在查看的是最新测评结果

  • [2024-08-14 03:40:17]
  • 评测
  • [2024-08-14 03:40:17]
  • 提交

answer

#include <bits/stdc++.h>

#define ndl '\n'
#define ll unsigned long long
#define all(x) (x).begin(), (x).end()

using namespace std;

void dfs_sub_tree_size(ll v, const vector<vector<ll>> &G, vector<ll> &sub_tree_size, vector<ll> &parent) {
    sub_tree_size[v] = 1;
    for(auto x: G[v]) {
        if(x != parent[v]) {
            parent[x] = v;
            dfs_sub_tree_size(x, G, sub_tree_size, parent);
            sub_tree_size[v] += sub_tree_size[x];
        }
    }
}

void fill_depth_and_jump_table(vector<ll> &depth, vector<vector<ll>> &jump_table, const vector<ll> &parent) {
    for(ll j=0; j<jump_table[0].size(); j++) {
        for(ll i=2; i<jump_table.size(); i++) {
            jump_table[i][j] = (j == 0) ? parent[i] : jump_table[jump_table[i][j-1]][j-1];
            depth[i] = depth[jump_table[i][0]] + 1;
        }
    }
}

ll find_lca(ll x, ll y, const vector<ll> &depth, const vector<vector<ll>> &jump_table) {
    if(depth[x] < depth[y]) swap(x, y);
    ll depth_difference = depth[x] - depth[y];
    for(ll i=0; (1<<i) <= depth_difference; i++) 
        if((1<<i)&depth_difference) x = jump_table[x][i];

    if(x == y) return x;

    for(ll i=jump_table[0].size()-1; i>=0; i--) {
        while(jump_table[x][i] != jump_table[y][i]) {
            x = jump_table[x][i];
            y = jump_table[y][i];
        }
    }
    return jump_table[x][0];
}

void fill_path(ll x, ll y, ll lca, const vector<ll> &depth, const vector<vector<ll>> &jump_table, vector<ll> &path) {
    if(depth[x] < depth[y]) swap(x, y);
    while (x != lca) {
        path.push_back(x);
        x = jump_table[x][0];
    } 
    path.push_back(lca);
    if(lca != y) {
        vector<ll> stack;
        while (y != lca) {
            stack.push_back(y);
            y = jump_table[y][0];
        } 
        for(ll i=stack.size()-1; i>=0; i--)
            path.push_back(stack[i]);
    }
}

void fill_appendices(const ll lca, const vector<vector<ll>> &G, const vector<ll> &path, const vector<ll> &subtree_sizes, const vector<vector<ll>> &jump_table, vector<ll> &appendices) {
    for(ll i=0; i<path.size(); i++) {
        ll v = path[i], lower = (i != 0 ? path[i-1] : -1), upper = (i != path.size()-1 ? path[i+1] : -1);
        appendices.push_back(1);
        if(v == lca) {
            appendices.back() += subtree_sizes[1] - subtree_sizes[lca];
        }
        for(auto x:G[v]) {
            if(x != lower && x != upper && x != jump_table[v][0]) 
                appendices.back() += subtree_sizes[x];
        } 
    }
}

int main(){
    ios_base::sync_with_stdio(0); cin.tie(0);
    
    ll n, q; cin>>n>>q;
    vector<vector<ll>> G(n+1);
    for(ll i=0;i<n-1;i++) {
        ll a, b; cin>>a>>b;
        G[a].push_back(b);
        G[b].push_back(a);
    }

    vector<ll> parent(n+1), sub_tree_size(n+1);
    dfs_sub_tree_size(1, G, sub_tree_size, parent);

    vector<ll> depth(n+1);
    vector<vector<ll>> jump_table(n+1, vector<ll>(__log(n)+1));
    fill_depth_and_jump_table(depth, jump_table, parent);

    while(q--) {
        ll a, b; cin>>a>>b;
        ll lca = find_lca(a, b, depth, jump_table);
        vector<ll> path, appendices;
        fill_path(a, b, lca, depth, jump_table, path);
        fill_appendices(lca, G, path, sub_tree_size, jump_table, appendices);
        
        ll ans = 0, left_sum = 0, half_cycle_length = ((path.size()-1)/2);
        for(ll i=0; i<half_cycle_length; i++) {
            left_sum += appendices[i];
            ans += left_sum * appendices[appendices.size()-half_cycle_length+i]; 
        }
        cout<<ans<<'\n';
    }

}

詳細信息

/usr/bin/ld: /tmp/ccMqdPTm.o: in function `main':
answer.code:(.text.startup+0x330): undefined reference to `__log'
collect2: error: ld returned 1 exit status