QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#186567#6738. CoverUrgantTeamWA 1301ms268164kbC++234.6kb2023-09-24 03:13:292023-09-24 03:13:29

Judging History

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

  • [2023-09-24 03:13:29]
  • 评测
  • 测评结果:WA
  • 用时:1301ms
  • 内存:268164kb
  • [2023-09-24 03:13:29]
  • 提交

answer

#include<bits/stdc++.h>

using namespace std;

#define int long long

typedef long long ll;
int const maxn = 1e5 + 5, logg = 17, K = 12;
vector < int > g[maxn];
vector < int > G[maxn];
vector < vector < int > > go[maxn], in[maxn];
vector < int > open[maxn];
int h[maxn], up[maxn][logg], W[5 * maxn];
ll dp[maxn];
unordered_map < int, ll > dp_calc[maxn];
int cmp[maxn];
ll add[maxn];
ll f[(1 << K)];
ll val_one[K], val_two[K][K];

void dfs(int v) {
    cmp[v] = v;
    for (auto u : G[v]) {
        dfs(u);
        if (dp_calc[cmp[u]].size() > dp_calc[cmp[v]].size()) cmp[v] = cmp[u];
    }
    int N = G[v].size();
    assert(N <= K);
    for (int i = 0; i < N; i++) {
        val_one[i] = dp[G[v][i]];
    }
    for (int i = 0; i < N; i++) {
        for (int j = 0; j < N; j++) val_two[i][j] = 0;
    }
    for (auto key : in[v]) {
        int ind_vertex = key[1], ind_path = key[0];
        int to = G[v][ind_vertex];
        val_one[ind_vertex] = max(val_one[ind_vertex], dp_calc[cmp[to]][ind_path] + add[cmp[to]] + W[ind_path]);
    }
    for (auto key : go[v]) {
        int ind_path = key[0], ind_v1 = key[1], ind_v2 = key[2];
        int u1 = G[v][ind_v1], u2 = G[v][ind_v2];
        ll value = dp_calc[cmp[u1]][ind_path] + dp_calc[cmp[u2]][ind_path] + add[cmp[u1]] + add[cmp[u2]] + W[ind_path];
        val_two[ind_v1][ind_v2] = max(val_two[ind_v1][ind_v2], value);
        val_two[ind_v2][ind_v1] = max(val_two[ind_v2][ind_v1], value);
    }
    for (int mask = 0; mask < (1 << N); mask++) f[mask] = 0;
    for (int mask = 0; mask < (1 << N) - 1; mask++) {
        int b = 0;
        for (int i = 0; i < N; i++) {
            if ((mask>>i)&1) continue;
            b = i;
            break;
        }
        f[mask|(1 << b)] = max(f[mask|(1 << b)], f[mask] + val_one[b]);
        for (int i = b + 1; i < N; i++) {
            if ((mask>>i)&1) continue;
            f[mask|(1 << b)|(1 << i)] = max(f[mask|(1 << b)|(1 << i)], f[mask] + val_two[b][i]);
        }
    }
    for (int i = 0; i < N; i++) {
        assert(val_one[i] >= 0);
        for (int j = 0; j < N; j++) assert(val_two[i][j] >= 0);
    }
    dp[v] = f[(1 << N) - 1];
    for (int i = 0; i < N; i++) {
        int msk = (((1 << N) - 1)^(1 << i));
        int u = G[v][i];
        add[cmp[u]] += f[msk];
    }
    for (auto u : G[v]) {
        if (cmp[v] == cmp[u]) continue;
        ll dd = add[cmp[u]] - add[cmp[v]];
        for (auto elem : dp_calc[cmp[u]]) {
            dp_calc[cmp[v]][elem.first] = elem.second + dd;
        }
    }
    ll dd = add[cmp[v]];
    for (auto key : open[v]) {
        dp_calc[cmp[v]][key] = dp[v] - dd;
    }
}

void calc(int v, int p) {
    up[v][0] = p;
    h[v] = h[p] + 1;
    for (int i = 1; i < logg; i++) up[v][i] = up[up[v][i - 1]][i - 1];
    for (auto u : g[v]) {
        if (u != p) {
            calc(u, v);
            G[v].push_back(u);
        }
    }
}

int numb(int v, int need) {
    for (int j = 0; j < G[v].size(); j++) {
        if (G[v][j] == need) return j;
    }
    assert(1 == 0);
}

int get_la(int v, int k) {
    for (int i = 0; i < logg; i++) {
        if ((k>>i)&1) v = up[v][i];
    }
    return v;
}

int get_lca(int u, int v) {
    if (h[u] > h[v]) u = get_la(u, h[u] - h[v]);
    else v = get_la(v, h[v] - h[u]);
    if (u == v) return v;
    for (int i = logg - 1; i >= 0; i--) {
        if (up[v][i] != up[u][i]) v = up[v][i], u = up[u][i];
    }
    return up[v][0];
}

main() {
#ifdef HOME
    freopen("input.txt", "r", stdin);
#endif // HOME
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);
    int n, m, k, u, v, w;
    cin >> n >> m >> k;
    ll ans = 0;
    for (int i = 1; i < n; i++) {
        cin >> u >> v;
        g[u].push_back(v);
        g[v].push_back(u);
    }
    calc(1, 0);
    for (int i = 1; i <= m; i++) {
        cin >> u >> v >> w;
        W[i] = w;
        if (u == v) {
            ans += w;
            continue;
        }
        int lca = get_lca(u, v);
        if (u != lca && v != lca) {
            go[lca].push_back({i, numb(lca, get_la(u, h[u] - h[lca] - 1)), numb(lca, get_la(v, h[v] - h[lca] - 1))});
            open[u].push_back(i);
            open[v].push_back(i);
        } else if (u == lca) {
            in[lca].push_back({i, numb(lca, get_la(v, h[v] - h[lca] - 1))});
            open[v].push_back(i);
        } else {
            in[lca].push_back({i, numb(lca, get_la(u, h[u] - h[lca] - 1))});
            open[u].push_back(i);
        }
    }
    dfs(1);
    ans += dp[1];
    cout << ans << '\n';
    return 0;
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 100
Accepted
time: 3ms
memory: 25368kb

input:

5 7 3
1 2
1 3
2 4
2 5
3 2 8
5 4 10
3 1 2
1 2 7
2 1 2
1 2 1
5 2 3

output:

19

result:

ok 1 number(s): "19"

Test #2:

score: -100
Wrong Answer
time: 1301ms
memory: 268164kb

input:

100000 500000 12
2 1
3 2
4 2
5 2
6 5
7 2
8 5
9 3
10 2
11 2
12 5
13 1
14 1
15 1
16 1
17 1
18 1
19 1
20 1
21 1
22 1
23 1
24 12
25 2
26 2
27 2
28 2
29 2
30 15
31 30
32 23
33 26
34 22
35 30
36 26
37 3
38 3
39 3
40 3
41 3
42 3
43 3
44 3
45 3
46 3
47 20
48 21
49 4
50 4
51 4
52 4
53 4
54 4
55 4
56 4
57 4
5...

output:

613739440508

result:

wrong answer 1st numbers differ - expected: '660925834533', found: '613739440508'