QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#357045#3047. Wind of Change_LAP_WA 2568ms90940kbC++145.7kb2024-03-18 17:44:262024-03-18 17:44:28

Judging History

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

  • [2024-03-18 17:44:28]
  • 评测
  • 测评结果:WA
  • 用时:2568ms
  • 内存:90940kb
  • [2024-03-18 17:44:26]
  • 提交

answer

#include <bits/stdc++.h>

using namespace std;

const int N = 2.5e5 + 10;
const long long INF = 0x3f3f3f3f3f3f3f3fLL;

struct Tree;
Tree *Treea, *Treeb;

struct Tree {
    int n; long long ans[N];
    vector<pair<int, int>> G[N];
    inline void input(int _n) {
        n = _n;
        for(int i = 1; i <= n; i ++) 
            ans[i] = INF;
        for(int i = 1; i < n; i ++) {
            int a, b, c; cin >> a >> b >> c;
            G[a].push_back({b, c}), G[b].push_back({a, c});
        }
    }

    int fa[N], dep[N], init_siz[N], son[N], top[N], dfn[N], dfs_clock;
    long long D[N];
    void init_dfs(int u, int f) {
        fa[u] = f, dep[u] = dep[f] + 1, init_siz[u] = 1, son[u] = 0;
        for(auto pr : G[u]) {
            int v = pr.first;
            if(v == f) continue;
            D[v] = D[u] + pr.second, init_dfs(v, u);
            init_siz[u] += init_siz[v];
            if(init_siz[v] > init_siz[son[u]]) son[u] = v;
        }
    }
    void init_hld(int u, int topf) {
        top[u] = topf, dfn[u] = ++dfs_clock;
        if(!son[u]) return;
        init_hld(son[u], topf);
        for(auto pr : G[u]) {
            int v = pr.first;
            if(v == son[u] || v == fa[u]) continue;
            init_hld(v, v);
        }
    }
    inline void init() {init_dfs(1, 0), init_hld(1, 1); }
    inline int lca(int a, int b) {
        while(top[a] != top[b]) {
            if(dep[top[a]] < dep[top[b]]) swap(a, b);
            a = fa[top[a]];
        }
        return dep[a] < dep[b] ? a : b;
    }
    inline long long dist(int a, int b) {return D[a] + D[b] - 2 * D[lca(a, b)]; }

    // 虚树上dp
    vector<int> vG[N];
    long long g[N], ming[N];
    void vTree_pre_dfs(int u) {
        ming[u] = g[u];
        for(auto v : vG[u]) {
            vTree_pre_dfs(v);
            ming[u] = min(ming[u], ming[v]);
        }
    }
    void vTree_dfs(int u, long long val) {
        pair<long long, long long> mn = {INF, INF};
        for(auto v : vG[u]) {
            if(ming[v] < mn.first) mn.second = mn.first, mn.first = ming[v];
            else if(ming[v] < mn.second) mn.second = ming[v];
        }
        ans[u] = min(ans[u], min(val, mn.first - 2 * D[u]) + g[u]);
        if(g[u] < mn.first) mn.second = mn.first, mn.first = g[u];
        else if(g[u] < mn.second) mn.second = g[u];
        for(auto v : vG[u]) {
            if(ming[v] == mn.first) vTree_dfs(v, min(val, mn.second - 2 * D[u]));
            else vTree_dfs(v, min(val, mn.first - 2 * D[u]));
        }
    }
    void calc(vector<pair<int, long long>> &vec) {
        for(int i = 0; i < vec.size(); i ++)
            vec[i].second += D[vec[i].first];
        
        // 建虚树
        sort(vec.begin(), vec.end(), [&](pair<int, long long> x, pair<int, long long> y) {
            return dfn[x.first] < dfn[y.first];
        });
        set<int> used;
        for(auto pr : vec) used.insert(pr.first);
        int tot = vec.size();
        for(int i = 1; i < tot; i ++) {
            int L = lca(vec[i - 1].first, vec[i].first);
            if(used.count(L)) continue;
            used.insert(L), vec.push_back({L, INF});
        }
        sort(vec.begin(), vec.end(), [&](pair<int, long long> x, pair<int, long long> y) {
            return dfn[x.first] < dfn[y.first];
        });
        for(int i = 0; i < vec.size(); i ++)
            g[vec[i].first] = vec[i].second;
        for(int i = 1; i < vec.size(); i ++)
            vG[lca(vec[i - 1].first, vec[i].first)].emplace_back(vec[i].first);

        vTree_pre_dfs(vec[0].first), vTree_dfs(vec[0].first, INF);
        for(int i = 0; i < vec.size(); i ++)
            vG[vec[i].first].clear(), g[vec[i].first] = 0;
    }

    // 点分治
    bool vis[N]; int siz[N], mx_siz[N], SIZE, root;
    void find_root_dfs(int u, int fa) {
        siz[u] = 1, mx_siz[u] = 0;
        for(auto pr : G[u]) { int v = pr.first;
            if(v == fa || vis[v]) continue;
            find_root_dfs(v, u), siz[u] += siz[v];
            mx_siz[u] = max(mx_siz[u], siz[v]);
        }
        mx_siz[u] = max(mx_siz[u], SIZE - siz[u]);
        if(root == -1 || mx_siz[root] > mx_siz[u]) root = u;
    }
    inline int find_root(int x, int _SIZE) {
        SIZE = _SIZE, root = -1, find_root_dfs(x, 0);
        return root;
    }
    void get_siz_dfs(int u, int fa) {
        SIZE ++;
        for(auto pr : G[u])
            if(!vis[pr.first] && pr.first != fa) get_siz_dfs(pr.first, u);
    }
    inline int get_siz(int x) {
        SIZE = 0;
        get_siz_dfs(x, -1);
        return SIZE;
    }
    void vec_dfs(int u, int fa, vector<pair<int, long long>> &vec) {
        vec.push_back({u, 0});
        for(auto pr : G[u]) {
            if(vis[pr.first] || pr.first == fa) continue;
            vec_dfs(pr.first, u, vec);
        }
    }
    void solve(int u) {
        // cerr << u << ' ';
        vis[u] = true;
        vector<pair<int, long long>> vec;
        vec.push_back({u, 0});
        for(auto pr : G[u]) {
            int v = pr.first; if(vis[v]) continue;            
            vec_dfs(v, u, vec);
        }
        for(int i = 0; i < vec.size(); i ++)
            vec[i].second = dist(u, vec[i].first);
        Treeb->calc(vec);
        
        for(auto pr : G[u]) { int v = pr.first;
            if(vis[v]) continue;
            solve(find_root(v, get_siz(v)));
        }
    }
} T[2];

int main() {
    ios::sync_with_stdio(false), cin.tie(0);

    int n; cin >> n;

    T[0].input(n), T[1].input(n);
    T[0].init(), T[1].init();
    Treea = &T[0], Treeb = &T[1];
    T[0].solve(T[0].find_root(1, n));

    for(int i = 1; i <= n; i ++)
        cout << T[1].ans[i] << " \n"[i == n];

    return 0;
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 0
Wrong Answer
time: 2568ms
memory: 90940kb

input:

250000
137466 116241 624409157
188961 163586 148491970
13958 122239 46409636
186120 44010 919858866
72320 102560 92679302
158544 236582 882613876
22331 114267 646741536
210782 42861 679450428
56693 45397 898958944
71188 114724 904191407
15203 225401 210626781
31071 144225 278121829
110354 83972 4557...

output:

41101981722 24783524958 17386222407 38045922430 25610233683 20965687585 28581128255 10929203519 9150749133 30625084420 25595872600 26129987591 27046353907 17070234497 23912017481 18635208366 22946884015 14181900087 24113819218 39573277018 25295838334 22248020256 14082699994 28220426146 33315844289 1...

result:

wrong answer 1st lines differ - expected: '41101981722', found: '41101981722 24783524958 173862...0716853 21726319537 18588631028'