Submission #778666

#TimeUsernameProblemLanguageResultExecution timeMemory
778666eltu0815Reconstruction Project (JOI22_reconstruction)C++14
0 / 100
2417 ms224268 KiB
#include <bits/stdc++.h>
#define MAX 500005
#define MOD (ll)(1e9+7)
#define INF (ll)(1e18)
#define inf (1987654321)
 
using namespace std;    
typedef long long ll;
typedef pair<ll, ll> pll;
typedef pair<int, int> pii;
 
int n, m, q;
int parent[505], par[505][10], e[505], dep[505], used[250005];
vector<pii> tree[505];
vector<pair<int, pii> > edge;
vector<vector<int> > MST;
vector<int> X;
 
int main()
{
    ios::sync_with_stdio(false);
    cin.tie(NULL);
    
    cin >> n >> m;
    edge.push_back({0, {0, 0}});
    for(int i = 1; i <= m; ++i) {
        int a, b, w; cin >> a >> b >> w;
        edge.push_back({w, {a, b}});
    }
    sort(edge.begin(), edge.end());
    
    auto getParent = [&](auto&& self, int node) -> int {
        if(parent[node] == node) return node;
        else return parent[node] = self(self, parent[node]);
    };
    
    auto unionParent = [&](int u, int v) -> void {
        u = getParent(getParent, u), v = getParent(getParent, v);
        if(u > v) swap(u, v);
        parent[v] = u;
    };
    
    auto dfs = [&](auto&& self, int node, int p) -> void {
        dep[node] = dep[p] + 1;
        for(auto v : tree[node]) if(v.first != p) {
            e[v.first] = v.second;
            par[v.first][0] = node;
            self(self, v.first, node);
        }
    };
    
    auto LCA = [&](int u, int v) {
        if(dep[u] < dep[v]) swap(u, v);
        int diff = dep[u] - dep[v], j = 0;
        while(diff) {
            if(diff & 1) u = par[u][j];
            diff >>= 1; ++j;
        }
        if(u == v) return u;
        for(int i = 9; i >= 0; --i) {
            if(par[u][i] != par[v][i]) {
                u = par[u][i];
                v = par[v][i];
            }
        }
        return par[u][0];
    };
    
    vector<int> tmp;
    priority_queue<pii> pq;
    for(int j = 1; j <= n; ++j) parent[j] = j, tree[j].clear();
    for(int j = 1; j <= m; ++j) pq.push({-edge[j].first, j});
    while(!pq.empty()) {
        int j = pq.top().second;
        int a = edge[j].second.first, b = edge[j].second.second;
        pq.pop();
        if(getParent(getParent, a) != getParent(getParent, b)) {
            tmp.push_back(j);
            unionParent(a, b);
            used[j] = 1;
        }
    }
    
    int x = 0;
    for(int i = 1, p = 1; i <= m + 1; ++i) {
        if(x == inf) break;
        for(int j = 1; j <= n; ++j) tree[j].clear();
        for(auto j : tmp) {
            int a = edge[j].second.first, b = edge[j].second.second;
            int w = edge[j].first;
            tree[a].push_back({b, w});
            tree[b].push_back({a, w});
        }
        
        dfs(dfs, 1, 0);
        for(int k = 1; k < 10; ++k) for(int j = 1; j <= n; ++j) par[j][k] = par[par[j][k - 1]][k - 1];
        X.push_back(x); MST.push_back(tmp);
        
        while(used[p] && p <= m) ++p;
        if(p == m + 1) break;
        
        used[p] = 1;
        int a = edge[p].second.first, b = edge[p].second.second;
        int pp = LCA(a, b), mnv = inf, mni = 0;
        while(a != pp) {
            if(mnv > e[a]) mnv = e[a], mni = a;
            a = par[a][0];
        }
        while(b != pp) {
            if(mnv > e[b]) mnv = e[b], mni = b;
            b = par[b][0];
        }
        
        x = (mnv + edge[p].first)/2 + 1;
        for(auto& j : tmp) {
            int a = edge[j].second.first, b = edge[j].second.second;
            int w = edge[j].first;
            if(min(a, b) == min(mni, par[mni][0]) && max(a, b) == max(mni, par[mni][0]) && w == e[mni]) j = p;
        }
    }
    
//    for(int i = 0; i < (int)X.size(); ++i) {
//        cout << endl << X[i] << endl;
//        for(auto j : MST[i]) {
//            cout << edge[j].second.first << ' ' << edge[j].second.second << ' ' << edge[j].first << endl;
//        }
//    }
    
    cin >> q;
    int i = 0;
    while(q--) {
        int x; cin >> x;
        while(i + 1 < (int)X.size() && X[i + 1] <= x) ++i;
        ll ans = 0;
        for(auto j : MST[i]) ans += 1ll * abs(edge[j].first - x);
        cout << ans << '\n';
    }
    return 0;
}
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...