QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#129825#6738. CoverUndertrainedOverpressure#WA 889ms37760kbC++235.0kb2023-07-23 01:25:332023-07-23 01:25:36

Judging History

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

  • [2023-08-10 23:21:45]
  • System Update: QOJ starts to keep a history of the judgings of all the submissions.
  • [2023-07-23 01:25:36]
  • 评测
  • 测评结果:WA
  • 用时:889ms
  • 内存:37760kb
  • [2023-07-23 01:25:33]
  • 提交

answer

#include <bits/stdc++.h>

using namespace std;

typedef long long ll;
typedef long double ld;
#define TIME (clock() * 1.0 / CLOCKS_PER_SEC)

const int M = 1e5 + 239;
const int T = (1 << 18) + 239;
const int N = 5e5 + 239;
const int L = 18;

int n, m, k;
vector<int> v[M];
vector<int> tree[M];

int go[M][L];
int h[M];
int l[M], r[M];
vector<int> et;
int loc[M];

void dfs_dp(int p, int ls) {
    l[p] = et.size();
    et.emplace_back(p);
    if (ls == -1) {
        h[p] = 0;
        for (int i = 0; i < L; i++) {
            go[p][i] = p;
        }
    } else {
        h[p] = h[ls] + 1;
        go[p][0] = ls;
        for (int i = 1; i < L; i++) {
            go[p][i] = go[go[p][i - 1]][i - 1];
        }
    }
    for (int i : v[p]) {
        if (i != ls) {
            tree[p].emplace_back(i);
            dfs_dp(i, p);
        }
    }
    r[p] = et.size();
}

int goup(int f, int cnt) {
    for (int i = 0; i < L; i++) {
        if ((cnt >> i) & 1) {
            f = go[f][i];
        }
    }
    return f;
}

int lca(int s, int f) {
    if (h[s] > h[f]) {
        swap(s, f);
    }
    if (h[f] != h[s]) {
        int cnt = h[f] - h[s];
        f = goup(f, cnt);
    }
    if (s == f) {
        return s;
    }
    for (int i = L - 1; i >= 0; i--) {
        if (go[s][i] != go[f][i]) {
            s = go[s][i];
            f = go[f][i];
        }
    }
    return go[s][0];
}

vector<tuple<int, int, int>> paths[M];

const int MS = (1 << 12) + 10;

ll dp_mask[MS];
ll dp[M];

ll both[L][L];
int idx[M];

ll sm[T], add[T];

void push(int i, int l, int r) {
    sm[i] += add[i];
    if (r - l == 1) {
        add[2 * i + 1] += add[i];
        add[2 * i + 2] += add[i];
    }
    add[i] = 0;
}

void upd(int i, int l, int r, int ql, int qr, ll x) {
    push(i, l, r);
    if (r <= ql || qr <= l) {
        return;
    }
    if (ql <= l && r <= qr) {
        add[i] += x;
        push(i, l, r);
        return;
    }
    int mid = (l + r) / 2;
    upd(2 * i + 1, l, mid, ql, qr, x);
    upd(2 * i + 2, mid, r, ql, qr, x);
    sm[i] = min(sm[2 * i + 1], sm[2 * i + 2]);
}

ll get(int i, int l, int r, int p) {
    push(i, l, r);
    if (r - l == 1) {
        return sm[i];
    }
    int mid = (l + r) / 2;
    push(2 * i + 1, l, mid);
    push(2 * i + 2, mid, r);
    ll ans = 0;
    if (p < mid) {
        ans = get(2 * i + 1, l, mid, p);
    } else {
        ans = get(2 * i + 2, mid, r, p);
    }
    sm[i] = min(sm[2 * i + 1], sm[2 * i + 2]);
    return ans;
}

ll getval(int p) {
    return get(0, 0, n, loc[p]);
}

void dfs(int p) {
    int cnt = 0;
    for (int i : tree[p]) {
        idx[i] = cnt++;
        dfs(i);
    }
    int sz = tree[p].size();
    for (int i = 0; i < sz; i++) {
        for (int j = 0; j < sz; j++) {
            both[i][j] = -1;
        }
    }
    for (auto [s, f, w] : paths[p]) {
        if (f == p) {
            swap(s, f);
        }
        if (s == p) {
            int cur_id = idx[goup(f, h[f] - h[p] - 1)];
            both[cur_id][cur_id] = max(both[cur_id][cur_id], getval(f) + w);
            continue;
        }
        int idx1 = idx[goup(s, h[s] - h[p] - 1)];
        int idx2 = idx[goup(f, h[f] - h[p] - 1)];
        if (idx1 > idx2) {
            swap(idx1, idx2);
            swap(s, f);
        }
        both[idx1][idx2] = max(both[idx1][idx2], getval(s) + getval(f) + w);
    }
    dp_mask[0] = 0;
    for (int mask = 1; mask < (1 << sz); mask++) {
        int i;
        for (i = 0; i < sz; i++) {
            if ((mask >> i) & 1) {
                break;
            }
        }
        dp_mask[mask] = dp_mask[mask ^ (1 << i)] + dp[tree[p][i]];
        if (both[i][i] != -1) {
            dp_mask[mask] = max(dp_mask[mask], dp_mask[mask ^ (1 << i)] + both[i][i]);
        }
        for (int j = i + 1; j < sz; j++) {
            if ((mask >> j) & 1) {
                if (both[i][j] != -1) {
                    dp_mask[mask] = max(dp_mask[mask], dp_mask[mask ^ (1 << i) ^ (1 << j)] + both[i][j]);
                }
            }
        }
    }
    dp[p] = dp_mask[(1 << sz) - 1];
    upd(0, 0, n, l[p], l[p] + 1, dp[p]);
    for (int i = 0; i < sz; i++) {
        int to = tree[p][i];
        upd(0, 0, n, l[to], r[to], dp_mask[((1 << sz) - 1) ^ (1 << i)]);
    }
}

void solve() {
    cin >> n >> m >> k;
    for (int i = 0; i < n - 1; i++) {
        int s, f;
        cin >> s >> f;
        s--, f--;
        v[s].emplace_back(f);
        v[f].emplace_back(s);
    }
    dfs_dp(0, -1);
    for (int i = 0; i < n; i++) {
        loc[et[i]] = i;
    }
    for (int i = 0; i < m; i++) {
        int s, f, w;
        cin >> s >> f >> w;
        s--, f--;
        paths[lca(s, f)].emplace_back(s, f, w);
    }
    dfs(0);
    cout << dp[0] << "\n";
}

int main() {
#ifdef ONPC
    freopen("input", "r", stdin);
#endif
    ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);

    int t = 1;
    //cin >> t;
    while (t--) {
        solve();
    }

    return 0;
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

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

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: 889ms
memory: 37760kb

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:

3632134857934

result:

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