QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#127764#6738. CoverOrmlis#AC ✓1287ms71416kbC++205.4kb2023-07-20 03:18:552023-07-20 03:18:58

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-20 03:18:58]
  • 评测
  • 测评结果:AC
  • 用时:1287ms
  • 内存:71416kb
  • [2023-07-20 03:18:55]
  • 提交

answer

#include "bits/stdc++.h"

#define rep(i, n) for (int i = 0; i < (n); ++i)
#define rep1(i, n) for (int i = 1; i < (n); ++i)
#define rep1n(i, n) for (int i = 1; i <= (n); ++i)
#define repr(i, n) for (int i = (n) - 1; i >= 0; --i)
#define pb push_back
#define eb emplace_back
#define all(a) (a).begin(), (a).end()
#define rall(a) (a).rbegin(), (a).rend()
#define each(x, a) for (auto &x : a)
#define ar array
#define vec vector
#define range(i, n) rep(i, n)

using namespace std;

using ll = long long;
using ull = unsigned long long;
using ld = long double;
using str = string;
using pi = pair<int, int>;
using pl = pair<ll, ll>;

using vi = vector<int>;
using vl = vector<ll>;
using vpi = vector<pair<int, int>>;
using vvi = vector<vi>;

int Bit(int mask, int b) { return (mask >> b) & 1; }

template<class T>
bool ckmin(T &a, const T &b) {
    if (b < a) {
        a = b;
        return true;
    }
    return false;
}

template<class T>
bool ckmax(T &a, const T &b) {
    if (b > a) {
        a = b;
        return true;
    }
    return false;
}

const int INFi = 2e9;
const ll INF = 1e18;
const int LG = 18;

const int N = 1e5 + 5;

vi g[N];

int up[N][20];
int h[N];

int lca(int v, int u) {
    if (h[v] < h[u]) swap(u, v);
    for (int i = 19; i >= 0; --i) {
        if (h[up[v][i]] >= h[u]) v = up[v][i];
    }
    if (u == v) return v;
    for (int i = 19; i >= 0; --i) {
        if (up[v][i] != up[u][i]) v = up[v][i], u = up[u][i];
    }
    return up[v][0];
}

int dist(int a, int b) {
    return h[a] + h[b] - 2 * h[lca(a, b)];
}

void dfs_lca(int v, int p) {
    up[v][0] = p;
    for (int i = 1; i < 20; ++i) up[v][i] = up[up[v][i - 1]][i - 1];
    for (auto &u: g[v]) {
        if (u == p) continue;
        h[u] = h[v] + 1;
        dfs_lca(u, v);
    }
}

int kth_anc(int v, int k) {
    rep(i, LG) if ((1 << i) & k) v = up[v][i];
    return v;
}


vi lazy[N];
ll lazy_add[N];
const int M = 5e5 + 5;
ll cost[M];
int a[M], b[M], c[M];
int aU[M], bU[M];
vi st[N];
vi en[N];
ll dp1[N];
int ind[N];
bool used[M];

void dfs(int v, int p) {
    vi ch;
    for (auto &u: g[v]) {
        if (u == p) continue;
        dfs(u, v);
        ch.push_back(u);
    }
    int sz = (int) ch.size();
    assert(sz <= 12);
    vl dp(1 << sz, 0);
    dp[0] = 0;
    vl cost1(sz);
    rep(i, sz) {
        cost1[i] = dp1[ch[i]];
        ind[ch[i]] = i;
    }
    vector<vl> cost2(sz, vl(sz, -INF));
    for (auto &i: en[v]) {
        used[i] = true;
        if (a[i] == c[i]) {
            int u = bU[i];
            cost[i] += lazy_add[u];
            cost1[ind[u]] = max(cost1[ind[u]], cost[i]);
        } else {
            int au = aU[i];
            int bu = bU[i];
            cost[i] += lazy_add[au];
            cost[i] += lazy_add[bu];
            cost2[ind[au]][ind[bu]] = max(cost2[ind[au]][ind[bu]], cost[i]);
            cost2[ind[bu]][ind[au]] = max(cost2[ind[bu]][ind[au]], cost[i]);
        }
    }
    rep(mask, 1 << sz) {
        rep(i, sz) {
            if ((1 << i) & mask) continue;
            ckmax(dp[(1 << i) | mask], dp[mask] + cost1[i]); // add 1
            for(int j = i + 1; j < sz; ++j) {
                if ((1 << j) & mask) continue;
                ckmax(dp[(1 << i) | (1 << j) | mask], dp[mask] + cost2[i][j]); // add 2
            }
            break;
        }
        repr(i, sz) {
            if ((1 << i) & mask) continue;
            ckmax(dp[(1 << i) | mask], dp[mask] + cost1[i]); // add 1
            for(int j = i - 1; j >= 0; --j) {
                if ((1 << j) & mask) continue;
                ckmax(dp[(1 << i) | (1 << j) | mask], dp[mask] + cost2[i][j]); // add 2
            }
            break;
        }
    }
    rep(i, sz) {
        int u = ch[i];
        lazy_add[u] += dp[((1 << sz) - 1) ^ (1 << i)];
        if (lazy[u].size() < lazy[v].size()) {
            for(auto &j : lazy[u]) {
                if (used[j]) continue;
                cost[j] += lazy_add[u] - lazy_add[v];
                lazy[v].push_back(j);
            }
            lazy[u].clear();
        } else {
            for(auto &j : lazy[v]) {
                if (used[j]) continue;
                cost[j] += lazy_add[v] - lazy_add[u];
                lazy[u].push_back(j);
            }
            lazy[v].clear();
            swap(lazy[u], lazy[v]);
            swap(lazy_add[u], lazy_add[v]);
        }
    }
    dp1[v] = dp.back();
    for(auto &i : st[v]) {
        lazy[v].push_back(i);
        cost[i] += dp1[v] - lazy_add[v];
    }
}

void solve() {
    int n, m, k;
    cin >> n >> m >> k;
    rep(_, n - 1) {
        int x, y;
        cin >> x >> y;
        g[x].push_back(y);
        g[y].push_back(x);
    }
    dfs_lca(1, 1);
    rep(i, m) {
        cin >> a[i] >> b[i] >> cost[i];
        c[i] = lca(a[i], b[i]);
        if (b[i] == c[i]) swap(a[i], b[i]);
        assert(b[i] != c[i]);
        if (c[i] != a[i]) {
            st[a[i]].push_back(i);
            aU[i] = kth_anc(a[i], h[a[i]] - h[c[i]] - 1);
        }
        st[b[i]].push_back(i);
        bU[i] = kth_anc(b[i], h[b[i]] - h[c[i]] - 1);
        en[c[i]].push_back(i);
    }
    dfs(1, -1);
    cout << dp1[1] << '\n';
}

signed main() {
    ios_base::sync_with_stdio(false);
    cin.tie(0);
    cout << setprecision(12) << fixed;
    int t = 1;
    rep(i, t) {
        solve();
    }
    return 0;
}

详细

Test #1:

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

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: 0
Accepted
time: 1276ms
memory: 71416kb

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:

660925834533

result:

ok 1 number(s): "660925834533"

Test #3:

score: 0
Accepted
time: 1278ms
memory: 71000kb

input:

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

output:

664434138007

result:

ok 1 number(s): "664434138007"

Test #4:

score: 0
Accepted
time: 1287ms
memory: 71124kb

input:

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

output:

639691495391

result:

ok 1 number(s): "639691495391"

Test #5:

score: 0
Accepted
time: 1285ms
memory: 71140kb

input:

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

output:

662244733768

result:

ok 1 number(s): "662244733768"

Test #6:

score: 0
Accepted
time: 1284ms
memory: 71328kb

input:

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

output:

669458090009

result:

ok 1 number(s): "669458090009"

Test #7:

score: 0
Accepted
time: 473ms
memory: 62348kb

input:

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

output:

488921502446

result:

ok 1 number(s): "488921502446"

Test #8:

score: 0
Accepted
time: 442ms
memory: 65116kb

input:

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

output:

468137226275

result:

ok 1 number(s): "468137226275"

Test #9:

score: 0
Accepted
time: 371ms
memory: 64548kb

input:

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

output:

483733769728

result:

ok 1 number(s): "483733769728"

Test #10:

score: 0
Accepted
time: 466ms
memory: 63796kb

input:

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

output:

478945297872

result:

ok 1 number(s): "478945297872"

Test #11:

score: 0
Accepted
time: 439ms
memory: 62904kb

input:

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

output:

489443708266

result:

ok 1 number(s): "489443708266"

Test #12:

score: 0
Accepted
time: 196ms
memory: 56932kb

input:

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

output:

560772428222

result:

ok 1 number(s): "560772428222"

Test #13:

score: 0
Accepted
time: 201ms
memory: 56232kb

input:

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

output:

572767352204

result:

ok 1 number(s): "572767352204"

Test #14:

score: 0
Accepted
time: 214ms
memory: 56924kb

input:

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

output:

585482767864

result:

ok 1 number(s): "585482767864"

Test #15:

score: 0
Accepted
time: 189ms
memory: 54452kb

input:

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

output:

564574799774

result:

ok 1 number(s): "564574799774"

Test #16:

score: 0
Accepted
time: 214ms
memory: 57152kb

input:

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

output:

575291114848

result:

ok 1 number(s): "575291114848"