QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#203231#7103. Red Black TreeikefumyAC ✓918ms29144kbC++205.1kb2023-10-06 16:15:412023-10-06 16:15:41

Judging History

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

  • [2023-10-06 16:15:41]
  • 评测
  • 测评结果:AC
  • 用时:918ms
  • 内存:29144kb
  • [2023-10-06 16:15:41]
  • 提交

answer

#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define ull unsigned long long
#define db double
#define pii pair<int,int>
#define pli pair<ll,int>
#define pil pair<int,ll>
#define pll pair<ll,ll>
#define ti3 tuple<int,int,int>
#define int128 __int128_t
#define pii128 pair<int128,int128>
const int inf = 1 << 30;
const ll linf = 1e18;
const ll mod = 1e9 + 7;
const db EPS = 1e-10;
const db pi = acos(-1);
template<class T> bool chmin(T& x, T y){
    if(x > y) {
        x = y;
        return true;
    } else return false;
}
template<class T> bool chmax(T& x, T y){
    if(x < y) {
        x = y;
        return true;
    } else return false;
}

// overload macro
#define CAT( A, B ) A ## B
#define SELECT( NAME, NUM ) CAT( NAME, NUM )

#define GET_COUNT( _1, _2, _3, _4, _5, _6 /* ad nauseam */, COUNT, ... ) COUNT
#define VA_SIZE( ... ) GET_COUNT( __VA_ARGS__, 6, 5, 4, 3, 2, 1 )

#define VA_SELECT( NAME, ... ) SELECT( NAME, VA_SIZE(__VA_ARGS__) )(__VA_ARGS__)

// rep(overload)
#define rep( ... ) VA_SELECT(rep, __VA_ARGS__)
#define rep2(i, n) for (int i = 0; i < int(n); i++)
#define rep3(i, a, b) for (int i = a; i < int(b); i++)
#define rep4(i, a, b, c) for (int i = a; i < int(b); i += c)

// repll(overload)
#define repll( ... ) VA_SELECT(repll, __VA_ARGS__)
#define repll2(i, n) for (ll i = 0; i < ll(n); i++)
#define repll3(i, a, b) for (ll i = a; i < ll(b); i++)
#define repll4(i, a, b, c) for (ll i = a; i < ll(b); i += c)

// rrep(overload)
#define rrep( ... ) VA_SELECT(rrep, __VA_ARGS__)
#define rrep2(i, n) for (int i = n - 1; i >= 0; i--)
#define rrep3(i, a, b) for (int i = b - 1; i >= a; i--)
#define rrep4(i, a, b, c) for (int i = b - 1; i >= a; i -= c)

// rrepll(overload)
#define rrepll( ... ) VA_SELECT(rrepll, __VA_ARGS__)
#define rrepll2(i, n) for (ll i = n - 1; i >= 0ll; i--)
#define rrepll3(i, a, b) for (ll i = b - 1; i >= ll(a); i--)
#define rrepll4(i, a, b, c) for (ll i = b - 1; i >= ll(a); i -= c)

// for_earh
#define fore(e, v) for (auto&& e : v)

// vector
#define all(v) v.begin(), v.end()
#define rall(v) v.rbegin(), v.rend()


struct LCA{
    int N;
    int logn;
    vector<vector<int>> G;
    vector<vector<int>> par;
    vector<int> depth;

    LCA(int n) : N(n), logn(0), G(n + 1), depth(n + 1) {
        while((1 << logn) <= N) logn++;
        par = vector<vector<int>>(logn, vector<int>(n + 1));
    }

    void add_edge(int u, int v){
        G[u].push_back(v);
        G[v].push_back(u);
    }

    void dfs(int v, int p, int d){
        par[0][v] = p;
        depth[v] = d;

        for(auto& u : G[v]){
            if(p == u) continue;
            dfs(u, v, d + 1);
        }
    }

    void setting(){
        dfs(1, -1, 0);
        for(int i = 1; i < logn; i++){
            for(int j = 1; j <= N; j++){
                if(par[i - 1][j] < 0) par[i][j] = -1;
                else par[i][j] = par[i - 1][par[i - 1][j]];
            }
        }
    }

    int get_lca(int u, int v){
        if(depth[u] < depth[v]) swap(u, v);
        int d = depth[u] - depth[v];

        for(int i = 0; i < logn; i++){
            if(d >> i & 1) u = par[i][u];
        }
        if(u == v) return u;

        for(int i = logn - 1; i >= 0; i--){
            if(par[i][u] != par[i][v]){
                u = par[i][u];
                v = par[i][v];
            }
        }
        return par[0][u];
    }

    int get_len(int u, int v) {
        int cp = get_lca(u, v);
        return depth[u] + depth[v] - depth[cp] * 2;
    }
};

void solve() {
    int n, m, q;
    cin >> n >> m >> q;
    vector<int> red(n + 1, false);
    vector<ll> cost(n + 1), dist(n + 1);
    vector<vector<pll>> G(n + 1);

    rep (i, m) {
        int v;
        cin >> v;
        red[v] = true;
    }

    LCA lca(n);
    rep (i, n - 1) {
        ll u, v, w;
        cin >> u >> v >> w;
        G[u].emplace_back(v, w);
        G[v].emplace_back(u, w);
        lca.add_edge(u, v);
    }
    lca.setting();

    auto dfs = [&](auto&& self, int v, int p, ll red_dist) -> void {
        if (red[v]) red_dist = dist[v];
        cost[v] = dist[v] - red_dist;
        for (auto&& [u, w] : G[v]) {
            if (u == p) continue;
            dist[u] = dist[v] + w;
            self(self, u, v, red_dist);
        }
    };

    dfs(dfs, 1, 0, 0);

    rep (_, q) {
        int k;
        cin >> k;
        vector<int> v(k);
        rep (i, k) cin >> v[i];
        sort(all(v), [&](int a, int b) {
            return cost[a] > cost[b];
        });

        v.push_back(0);
        ll ans = cost[v[0]];
        ll len = 0;
        int ca = v[0];
        rep (i, k) {
            int nca = lca.get_lca(ca, v[i]);
            len = max(len + dist[ca] - dist[nca], dist[v[i]] - dist[nca]);
            ca = nca;
            chmin(ans, max(len, cost[v[i + 1]]));
        }
        cout << ans << '\n';
    }
}

int main() {
    cin.tie(nullptr);
    ios_base::sync_with_stdio(false);
    cout << fixed << setprecision(20);
    int t;
    cin >> t;
    while (t--) solve();
    return 0;
}

这程序好像有点Bug,我给组数据试试?

詳細信息

Test #1:

score: 100
Accepted
time: 0ms
memory: 3656kb

input:

2
12 2 4
1 9
1 2 1
2 3 4
3 4 3
3 5 2
2 6 2
6 7 1
6 8 2
2 9 5
9 10 2
9 11 3
1 12 10
3 3 7 8
4 4 5 7 8
4 7 8 10 11
3 4 5 12
3 2 3
1 2
1 2 1
1 3 1
1 1
2 1 2
3 1 2 3

output:

4
5
3
8
0
0
0

result:

ok 7 lines

Test #2:

score: 0
Accepted
time: 918ms
memory: 29144kb

input:

522
26 1 3
1
1 4 276455
18 6 49344056
18 25 58172365
19 9 12014251
2 1 15079181
17 1 50011746
8 9 2413085
23 24 23767115
22 2 26151339
26 21 50183935
17 14 16892041
9 26 53389093
1 20 62299200
24 18 56114328
11 2 50160143
6 26 14430542
16 7 32574577
3 16 59227555
3 15 8795685
4 12 5801074
5 20 57457...

output:

148616264
148616264
0
319801028
319801028
255904892
317070839
1265145897
1265145897
1072765445
667742619
455103436
285643094
285643094
285643094
317919339
0
785245841
691421476
605409472
479058444
371688030
303203698
493383271
919185207
910180170
919185207
121535083
181713164
181713164
181713164
181...

result:

ok 577632 lines

Extra Test:

score: 0
Extra Test Passed