QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#423932#7103. Red Black TreeHHH666AC ✓1599ms41760kbC++144.1kb2024-05-28 19:42:232024-05-28 19:42:24

Judging History

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

  • [2024-05-28 19:42:24]
  • 评测
  • 测评结果:AC
  • 用时:1599ms
  • 内存:41760kb
  • [2024-05-28 19:42:23]
  • 提交

answer

#include<bits/stdc++.h>
#define ll long long
#define ld long double
#define i1 __int128
#define pii pair<int, int>
#define pll pair<ll, ll>
#define mp make_pair
#define mt make_tuple
#define fi first
#define se second
#define ep emplace
#define eb emplace_back
#define all(v) v.begin(), v.end()

using namespace std;

template<typename types>
void read(types &x) {
    x = 0; char c = getchar(); int f = 1;
    while (!isdigit(c)) { if (c == '-') f = -1; c = getchar(); }
    while (isdigit(c)) x = x * 10 + c - '0', c = getchar();
    x *= f; return;
}
void read() {}
template<typename types, typename... Args>
void read(types &x, Args&... args) {
    read(x), read(args...);
    return;
}
template<typename types>
void write(types x) {
    if (x < 0) putchar('-'), x = -x;
    types k = x / 10;
    if (k) write(k);
    putchar(x - k * 10 + '0');
    return;
}
void print() { puts(""); }
template<typename types, typename... Args>
void print(types x, Args... args) {
    write(x), putchar(' '), print(args...);
    return;
}

const int MAXN = 2e5 + 10;
const int MAXM = MAXN * 2;
const int LGN = 21;

int n, m, q;
bool b[MAXN], vis[MAXN];
ll dis[MAXN], dd[MAXN];
int head[MAXN], to[MAXM], w[MAXM], nxt[MAXM], idx = 1;
int dfn[MAXN], timecnt;
int depth[MAXN], pa[MAXN], mn[MAXN][LGN], lg[MAXN];
vector<int> to2[MAXN];
ll f[MAXN], g[MAXN], h[MAXN];

void addedge(int x, int y, int z) {
    nxt[++idx] = head[x];
    to[idx] = y;
    w[idx] = z;
    head[x] = idx;
    return;
}
int cmp(int x, int y) { return depth[x] < depth[y] ? x : y; }
void dfs(int x, int p, ll d) {
    dfn[x] = ++timecnt;
    if (b[x]) d = 0;
    dis[x] = d, pa[x] = p, depth[x] = depth[p] + 1;
    mn[dfn[x]][0] = x;
    for (int i = head[x]; i; i = nxt[i]) {
        int y = to[i];
        if (y == p) continue;
        dd[y] = dd[x] + w[i];
        dfs(y, x, d + w[i]);
    }
    return;
}
int query(int l, int r) {
    int k = lg[r - l + 1];
    return cmp(mn[l][k], mn[r - (1 << k) + 1][k]);
}
int lca(int x, int y) {
    if (x == y) return x;
    if (dfn[x] > dfn[y]) swap(x, y);
    return pa[query(dfn[x] + 1, dfn[y])];
}
void dp(int x, ll t) {
    f[x] = g[x] = h[x] = 0;
    for (int y : to2[x]) {
        dp(y, t);
        f[x] += f[y];
        if (!h[y]) continue;
        if (dis[y] < dd[y] - dd[x]) {
            if (dis[y] + g[y] > t) ++f[x];
        }
        else {
            if (dd[y] - dd[x] + g[y] > t) ++f[x];
            else g[x] = max(g[x], dd[y] - dd[x] + g[y]), h[x] = true;
        }
        if (f[x] > 1) return;
    }
    h[x] |= vis[x];
    if (b[x]) h[x] = false;
    return;
}
void solve(vector<int> &v) {
    for (int i : v) vis[i] = !b[i];
    v.eb(1);
    auto cmp = [&] (int x, int y) { return dfn[x] < dfn[y]; };
    sort(all(v), cmp);
    for (int i = 0, t = v.size(); i + 1 < t; ++i)
        v.eb(lca(v[i], v[i + 1]));
    sort(all(v), cmp), v.erase(unique(all(v)), v.end());
    for (int i : v) to2[i].clear();
    for (int i = 1; i < v.size(); ++i) to2[lca(v[i - 1], v[i])].eb(v[i]);
    ll l = 0, r = 1e14;
    while (l < r) {
        ll mid = (l + r) >> 1;
        dp(1, mid);
        if (f[1] <= 1) r = mid;
        else l = mid + 1;
    }
    write(l), puts("");
    for (int i : v) vis[i] = false;
    return;
}
void solve() {
    read(n, m, q);
    timecnt = idx = 0;
    fill(b + 1, b + n + 1, false);
    fill(head + 1, head + n + 1, 0);
    while (m--) {
        int x; read(x);
        b[x] = true;
    }
    for (int i = 1; i < n; ++i) {
        int x, y, z; read(x, y, z);
        addedge(x, y, z), addedge(y, x, z);
    }
    dfs(1, 0, 0);
    for (int i = 2; i <= n; ++i) lg[i] = lg[i >> 1] + 1;
    for (int k = 1; k <= lg[n]; ++k)
        for (int i = 1; i + (1 << k) - 1 <= n; ++i) mn[i][k] = cmp(mn[i][k - 1], mn[i + (1 << k - 1)][k - 1]);
    while (q--) {
        vector<int> v;
        int k; read(k);
        while (k--) { int x; read(x), v.eb(x); }
        solve(v);
    }
    return;
}

int main() {
    int t; read(t);
    while (t--) solve();
    return 0;
}

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

Details

Tip: Click on the bar to expand more detailed information

Test #1:

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

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: 1599ms
memory: 41760kb

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