QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#610141#5363. ZYB 的游览计划forgotmyhandle0 0ms9804kbC++143.1kb2024-10-04 15:01:412024-10-04 15:01:42

Judging History

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

  • [2024-10-04 15:01:42]
  • 评测
  • 测评结果:0
  • 用时:0ms
  • 内存:9804kb
  • [2024-10-04 15:01:41]
  • 提交

answer

#include <iostream>
// #include <cassert>
#include <set>
using namespace std;
int n, m;
int a[200005];
int head[200005], nxt[400005], to[400005], ecnt;
void add(int u, int v) { to[++ecnt] = v, nxt[ecnt] = head[u], head[u] = ecnt; }
int top[200005], dep[200005], son[200005], sz[200005], f[200005];
int dfn[200005], _dfn[200005], ncnt;
void dfs1(int x, int fa, int d) {
    dep[x] = d;
    f[x] = fa;
    sz[x] = 1;
    for (int i = head[x]; i; i = nxt[i]) {
        int v = to[i];
        if (v != fa) {
            dfs1(v, x, d + 1);
            sz[x] += sz[v];
            if (sz[v] > sz[son[x]]) 
                son[x] = v;
        }
    }
}
void dfs2(int x, int t) {
    top[x] = t;
    _dfn[dfn[x] = ++ncnt] = x;
    if (!son[x]) 
        return;
    dfs2(son[x], t);
    for (int i = head[x]; i; i = nxt[i]) {
        int v = to[i];
        if (v != f[x] && v != son[x]) 
            dfs2(v, v);
    }
}
int LCA(int x, int y) {
    while (top[x] ^ top[y]) (dep[top[x]] < dep[top[y]]) ? (y = f[top[y]]) : (x = f[top[x]]);
    return (dep[x] < dep[y] ? x : y);
}
int dist(int x, int y) { return dep[x] + dep[y] - 2 * dep[LCA(x, y)]; }
set<int> st;
int cur = 0;
void Add(int x) {
    if (x == 1) 
        return;
    int a, b;
    int d = dfn[x];
    set<int>::iterator it = st.upper_bound(d);
    b = (it != st.end() ? _dfn[*it] : _dfn[*st.begin()]);
    // assert(it != st.begin());
    --it;
    a = _dfn[*it];
    cur -= dist(a, b);
    cur += dist(a, x);
    cur += dist(b, x);
    st.insert(dfn[x]);
}
void Erase(int x) {
    if (x == 1) 
        return;
    int a, b;
    int d = dfn[x];
    st.erase(st.find(d));
    set<int>::iterator it = st.upper_bound(d);
    b = (it != st.end() ? _dfn[*it] : _dfn[*st.begin()]);
    // assert(it != st.begin());
    --it;
    a = _dfn[*it];
    cur += dist(a, b);
    cur -= dist(a, x);
    cur -= dist(b, x);
}
int dp[505][30005];
int _l = 1, _r;
void Move(int tl, int tr) {
    while (_r < tr) Add(a[++_r]);
    while (_r > tr) Erase(a[_r--]);
    while (_l < tl) Erase(a[_l++]);
    while (_l > tl) Add(a[--_l]);
}
void Solve(int l, int r, int L, int R, int k) {
    if (l > r) 
        return;
    int mid = (l + r) >> 1;
    int p = L;
    for (int i = L; i <= mid; i++) {
        Move(i, mid);
        if (dp[k - 1][i - 1] + cur > dp[k][mid]) {
            dp[k][mid] = dp[k - 1][i - 1] + cur;
            p = i;
        }
    }
    // cout << k << " " << mid << " " << p << " " << dp[k][mid] << "\n";
    Solve(l, mid - 1, L, p, k);
    Solve(mid + 1, r, p, R, k);
}
int main() {
    cin >> n >> m;
    for (int i = 1; i <= n; i++) cin >> a[i];
    for (int i = 1; i < n; i++) {
        int u, v;
        cin >> u >> v;
        add(u, v);
        add(v, u);
    }
    dfs1(1, 0, 1);
    dfs2(1, 1);
    st.insert(1);
    for (int i = 1; i <= n; i++) {
        Add(a[i]);
        dp[1][i] = cur;
        // cout << i << " " << dp[1][i] << "\n";
    }
    for (int i = 1; i <= n; i++) Erase(a[i]);
    for (int i = 2; i <= m; i++) Solve(i, n, i, n, i);
    cout << dp[m][n] << "\n";
    return 0;
}

详细

Subtask #1:

score: 0
Runtime Error

Test #1:

score: 5
Accepted
time: 0ms
memory: 9804kb

input:

5 2
2 4 3 5 1
1 2
2 3
3 4
4 5

output:

14

result:

ok single line: '14'

Test #2:

score: 0
Runtime Error

input:

90752 2
88555 48815 61754 47133 45462 73740 84783 58077 73713 78537 14562 78533 71607 24890 16284 41187 78450 31531 25296 45169 55240 83197 82146 86338 83180 75924 9923 40553 84394 73069 7278 77214 24896 14446 87566 70680 48218 58608 86578 47698 86173 89371 77350 85782 36318 22735 80925 5435 76359 2...

output:


result:


Subtask #2:

score: 0
Runtime Error

Test #10:

score: 0
Runtime Error

input:

760 217
632 417 493 400 316 482 542 614 36 134 604 291 745 484 451 746 518 378 487 650 633 223 601 259 33 257 309 683 705 627 513 670 130 395 512 115 466 376 575 356 180 716 539 403 431 563 568 468 596 239 296 379 537 224 526 215 725 234 663 603 401 21 75 660 506 393 105 88 462 620 449 338 276 200 4...

output:


result:


Subtask #3:

score: 0
Skipped

Dependency #2:

0%

Subtask #4:

score: 0
Runtime Error

Test #29:

score: 0
Runtime Error

input:

14878 6
1663 4532 2022 11114 1768 7744 12403 7698 14863 1406 13199 9405 3528 9898 1205 3076 11342 7459 9401 10025 14498 7178 11457 1802 9923 1648 13136 10720 3002 7332 13780 2094 1716 13215 8118 318 11186 14833 7941 8174 8999 6189 7504 13738 4933 3367 12973 1889 9835 4080 3546 1993 1861 11613 2504 1...

output:


result:


Subtask #5:

score: 0
Skipped

Dependency #1:

0%