답안 #796630

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
796630 2023-07-28T14:59:18 Z rnl42 Mergers (JOI19_mergers) C++14
70 / 100
305 ms 129120 KB
#include <iostream>
#include <vector>
#include <algorithm>
#include <numeric>
using namespace std;

int N, K;
vector<int> dist;
vector<vector<int>> adj;
vector<int> group_of;
vector<vector<int>> members;
vector<pair<int,int>> lcatree;
vector<int> first;
int lcatree_i = 0;
const int lcatree_shift = 1<<19;
vector<int> mergeuntil;
vector<bool> penible;

vector<int> uf;

int root(int u) {
    return uf[u] == u ? u : uf[u] = root(uf[u]);
}
void merge(int u, int v) {
    if (dist[u] > dist[v]) {
        swap(u, v);
    }
    uf[root(v)] = root(u);
}

void dfs(int u) {
    first[u] = lcatree_i;
    lcatree[lcatree_shift+lcatree_i++] = make_pair(dist[u], u);
    for (int v : adj[u]) {
        adj[v].erase(find(adj[v].begin(), adj[v].end(), u));
        dist[v] = dist[u]+1;
        dfs(v);
        lcatree[lcatree_shift+lcatree_i++] = make_pair(dist[u], u);
    }
}

int dfs2(int u) {
    int ret = mergeuntil[u];
    for (int v : adj[u]) {
        int r = dfs2(v);
        if (dist[r] < dist[ret]) ret = r;
        if (r != v) {
            merge(v, u);
        }
    }
    return ret;
}

int lca(int u, int v) {
    if (first[u] > first[v]) swap(u, v);
    int l = lcatree_shift+first[u], r = lcatree_shift+first[v]+1;
    pair<int,int> ret(1e9, 1e9);
    for (; l < r; l >>= 1, r >>= 1) {
        if (l&1) {
            ret = min(ret, lcatree[l++]);
        }
        if (r&1) {
            ret = min(ret, lcatree[--r]);
        }
    }
    return ret.second;
}

int dfs3(int u) {
    int ret = 0;
    for (int v : adj[u]) {
        ret += dfs3(v);
    }
    ret = max(ret, (int)penible[u]);
    return ret;
}

signed main() {
    ios::sync_with_stdio(false), cin.tie(0), cout.tie(0);
    cin >> N >> K;
    dist.resize(N);
    adj.resize(N);
    first.resize(N);
    group_of.resize(N);
    members.resize(K);
    lcatree.resize(1<<20);
    uf.resize(N);
    mergeuntil.resize(N);
    penible.resize(N);
    iota(mergeuntil.begin(), mergeuntil.end(), 0);
    iota(uf.begin(), uf.end(), 0);
    int u, v;
    for (int i = 0; i < N-1; i++) {
        cin >> u >> v, u--, v--;
        adj[u].push_back(v);
        adj[v].push_back(u);
    }
    for (int i = 0; i < N; i++) {
        cin >> group_of[i], group_of[i]--;
        members[group_of[i]].push_back(i);
    }
    dfs(0);
    for (int i = lcatree_shift-1; i > 0; i--) {
        lcatree[i] = min(lcatree[2*i], lcatree[2*i+1]);
    }
    for (int g = 0; g < K; g++) {
        int a = members[g][0];
        for (auto m : members[g]) {
            a = lca(a, m);
        }
        for (auto m : members[g]) {
            mergeuntil[m] = a;
        }
    }
    dfs2(0);
    bool plus1 = false;
    int lcapenible = -1;
    for (int i = 1; i < N; i++) {
        if (root(i) == i) {
            if (lcapenible == -1) lcapenible = i;
            else lcapenible = lca(lcapenible, i);
            penible[i] = true;
        }
    }
    for (int i = 1; i < N; i++) {
        if (penible[i]) {
            if (lca(lcapenible, i) == i) {
                plus1 = true;
                break;
            }
        }
    }
    int ans = dfs3(0)+plus1;
    cout << ((ans+1)>>1) << '\n';
}
# 결과 실행 시간 메모리 Grader output
1 Correct 5 ms 8532 KB Output is correct
2 Correct 5 ms 8532 KB Output is correct
3 Correct 5 ms 8624 KB Output is correct
4 Correct 5 ms 8532 KB Output is correct
5 Correct 5 ms 8532 KB Output is correct
6 Correct 6 ms 8512 KB Output is correct
7 Correct 5 ms 8532 KB Output is correct
8 Correct 7 ms 8640 KB Output is correct
9 Correct 5 ms 8532 KB Output is correct
10 Correct 4 ms 8532 KB Output is correct
11 Correct 4 ms 8532 KB Output is correct
12 Correct 4 ms 8516 KB Output is correct
13 Correct 4 ms 8532 KB Output is correct
14 Correct 4 ms 8532 KB Output is correct
15 Correct 5 ms 8532 KB Output is correct
16 Correct 4 ms 8532 KB Output is correct
17 Correct 4 ms 8532 KB Output is correct
18 Correct 5 ms 8516 KB Output is correct
19 Correct 4 ms 8532 KB Output is correct
20 Correct 4 ms 8516 KB Output is correct
21 Correct 5 ms 8488 KB Output is correct
22 Correct 4 ms 8532 KB Output is correct
23 Correct 4 ms 8532 KB Output is correct
24 Correct 4 ms 8532 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 5 ms 8532 KB Output is correct
2 Correct 5 ms 8532 KB Output is correct
3 Correct 5 ms 8624 KB Output is correct
4 Correct 5 ms 8532 KB Output is correct
5 Correct 5 ms 8532 KB Output is correct
6 Correct 6 ms 8512 KB Output is correct
7 Correct 5 ms 8532 KB Output is correct
8 Correct 7 ms 8640 KB Output is correct
9 Correct 5 ms 8532 KB Output is correct
10 Correct 4 ms 8532 KB Output is correct
11 Correct 4 ms 8532 KB Output is correct
12 Correct 4 ms 8516 KB Output is correct
13 Correct 4 ms 8532 KB Output is correct
14 Correct 4 ms 8532 KB Output is correct
15 Correct 5 ms 8532 KB Output is correct
16 Correct 4 ms 8532 KB Output is correct
17 Correct 4 ms 8532 KB Output is correct
18 Correct 5 ms 8516 KB Output is correct
19 Correct 4 ms 8532 KB Output is correct
20 Correct 4 ms 8516 KB Output is correct
21 Correct 5 ms 8488 KB Output is correct
22 Correct 4 ms 8532 KB Output is correct
23 Correct 4 ms 8532 KB Output is correct
24 Correct 4 ms 8532 KB Output is correct
25 Correct 5 ms 8532 KB Output is correct
26 Correct 6 ms 8916 KB Output is correct
27 Correct 6 ms 8788 KB Output is correct
28 Correct 8 ms 9152 KB Output is correct
29 Correct 6 ms 8916 KB Output is correct
30 Correct 8 ms 8788 KB Output is correct
31 Correct 5 ms 8532 KB Output is correct
32 Correct 9 ms 9040 KB Output is correct
33 Correct 5 ms 8532 KB Output is correct
34 Correct 7 ms 8788 KB Output is correct
35 Correct 6 ms 8916 KB Output is correct
36 Correct 6 ms 8748 KB Output is correct
37 Correct 7 ms 8788 KB Output is correct
38 Correct 5 ms 8532 KB Output is correct
39 Correct 6 ms 8788 KB Output is correct
40 Correct 6 ms 8788 KB Output is correct
41 Correct 6 ms 8816 KB Output is correct
42 Correct 6 ms 8908 KB Output is correct
43 Correct 6 ms 9044 KB Output is correct
44 Correct 5 ms 8532 KB Output is correct
45 Correct 6 ms 8916 KB Output is correct
46 Correct 8 ms 8884 KB Output is correct
47 Correct 5 ms 8532 KB Output is correct
48 Correct 6 ms 8788 KB Output is correct
49 Correct 9 ms 8916 KB Output is correct
50 Correct 6 ms 8928 KB Output is correct
51 Correct 6 ms 8788 KB Output is correct
52 Correct 6 ms 8780 KB Output is correct
53 Correct 6 ms 8788 KB Output is correct
54 Correct 6 ms 8788 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 5 ms 8532 KB Output is correct
2 Correct 5 ms 8532 KB Output is correct
3 Correct 5 ms 8624 KB Output is correct
4 Correct 5 ms 8532 KB Output is correct
5 Correct 5 ms 8532 KB Output is correct
6 Correct 6 ms 8512 KB Output is correct
7 Correct 5 ms 8532 KB Output is correct
8 Correct 7 ms 8640 KB Output is correct
9 Correct 5 ms 8532 KB Output is correct
10 Correct 4 ms 8532 KB Output is correct
11 Correct 4 ms 8532 KB Output is correct
12 Correct 4 ms 8516 KB Output is correct
13 Correct 4 ms 8532 KB Output is correct
14 Correct 4 ms 8532 KB Output is correct
15 Correct 5 ms 8532 KB Output is correct
16 Correct 4 ms 8532 KB Output is correct
17 Correct 4 ms 8532 KB Output is correct
18 Correct 5 ms 8516 KB Output is correct
19 Correct 4 ms 8532 KB Output is correct
20 Correct 4 ms 8516 KB Output is correct
21 Correct 5 ms 8488 KB Output is correct
22 Correct 4 ms 8532 KB Output is correct
23 Correct 4 ms 8532 KB Output is correct
24 Correct 4 ms 8532 KB Output is correct
25 Correct 4 ms 8532 KB Output is correct
26 Correct 52 ms 18328 KB Output is correct
27 Correct 58 ms 18012 KB Output is correct
28 Correct 6 ms 8788 KB Output is correct
29 Correct 5 ms 8532 KB Output is correct
30 Correct 5 ms 8532 KB Output is correct
31 Correct 59 ms 17980 KB Output is correct
32 Correct 6 ms 8780 KB Output is correct
33 Correct 78 ms 25212 KB Output is correct
34 Correct 66 ms 17996 KB Output is correct
35 Correct 6 ms 8780 KB Output is correct
36 Correct 59 ms 18764 KB Output is correct
37 Correct 6 ms 8788 KB Output is correct
38 Correct 6 ms 8776 KB Output is correct
39 Correct 50 ms 18256 KB Output is correct
40 Correct 6 ms 9044 KB Output is correct
41 Correct 61 ms 18028 KB Output is correct
42 Correct 84 ms 20360 KB Output is correct
43 Correct 5 ms 8532 KB Output is correct
44 Correct 72 ms 25412 KB Output is correct
45 Correct 60 ms 21736 KB Output is correct
46 Correct 8 ms 8788 KB Output is correct
47 Correct 8 ms 8788 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 44 ms 16972 KB Output is correct
2 Correct 69 ms 23568 KB Output is correct
3 Correct 8 ms 8916 KB Output is correct
4 Correct 6 ms 8776 KB Output is correct
5 Correct 4 ms 8588 KB Output is correct
6 Correct 5 ms 8532 KB Output is correct
7 Correct 5 ms 8788 KB Output is correct
8 Correct 68 ms 20500 KB Output is correct
9 Correct 6 ms 8788 KB Output is correct
10 Correct 66 ms 18152 KB Output is correct
11 Correct 4 ms 8532 KB Output is correct
12 Correct 55 ms 18076 KB Output is correct
13 Correct 70 ms 20464 KB Output is correct
14 Correct 89 ms 23300 KB Output is correct
15 Correct 49 ms 18284 KB Output is correct
16 Correct 8 ms 8788 KB Output is correct
17 Correct 5 ms 8532 KB Output is correct
18 Correct 74 ms 23112 KB Output is correct
19 Correct 85 ms 27140 KB Output is correct
20 Correct 6 ms 8788 KB Output is correct
21 Correct 4 ms 8532 KB Output is correct
22 Correct 61 ms 20656 KB Output is correct
23 Correct 6 ms 8788 KB Output is correct
24 Correct 68 ms 18744 KB Output is correct
25 Correct 86 ms 25888 KB Output is correct
26 Correct 6 ms 8916 KB Output is correct
27 Correct 6 ms 8916 KB Output is correct
28 Correct 6 ms 8788 KB Output is correct
29 Correct 6 ms 8788 KB Output is correct
30 Correct 6 ms 8900 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 5 ms 8532 KB Output is correct
2 Correct 5 ms 8532 KB Output is correct
3 Correct 5 ms 8624 KB Output is correct
4 Correct 5 ms 8532 KB Output is correct
5 Correct 5 ms 8532 KB Output is correct
6 Correct 6 ms 8512 KB Output is correct
7 Correct 5 ms 8532 KB Output is correct
8 Correct 7 ms 8640 KB Output is correct
9 Correct 5 ms 8532 KB Output is correct
10 Correct 4 ms 8532 KB Output is correct
11 Correct 4 ms 8532 KB Output is correct
12 Correct 4 ms 8516 KB Output is correct
13 Correct 4 ms 8532 KB Output is correct
14 Correct 4 ms 8532 KB Output is correct
15 Correct 5 ms 8532 KB Output is correct
16 Correct 4 ms 8532 KB Output is correct
17 Correct 4 ms 8532 KB Output is correct
18 Correct 5 ms 8516 KB Output is correct
19 Correct 4 ms 8532 KB Output is correct
20 Correct 4 ms 8516 KB Output is correct
21 Correct 5 ms 8488 KB Output is correct
22 Correct 4 ms 8532 KB Output is correct
23 Correct 4 ms 8532 KB Output is correct
24 Correct 4 ms 8532 KB Output is correct
25 Correct 5 ms 8532 KB Output is correct
26 Correct 6 ms 8916 KB Output is correct
27 Correct 6 ms 8788 KB Output is correct
28 Correct 8 ms 9152 KB Output is correct
29 Correct 6 ms 8916 KB Output is correct
30 Correct 8 ms 8788 KB Output is correct
31 Correct 5 ms 8532 KB Output is correct
32 Correct 9 ms 9040 KB Output is correct
33 Correct 5 ms 8532 KB Output is correct
34 Correct 7 ms 8788 KB Output is correct
35 Correct 6 ms 8916 KB Output is correct
36 Correct 6 ms 8748 KB Output is correct
37 Correct 7 ms 8788 KB Output is correct
38 Correct 5 ms 8532 KB Output is correct
39 Correct 6 ms 8788 KB Output is correct
40 Correct 6 ms 8788 KB Output is correct
41 Correct 6 ms 8816 KB Output is correct
42 Correct 6 ms 8908 KB Output is correct
43 Correct 6 ms 9044 KB Output is correct
44 Correct 5 ms 8532 KB Output is correct
45 Correct 6 ms 8916 KB Output is correct
46 Correct 8 ms 8884 KB Output is correct
47 Correct 5 ms 8532 KB Output is correct
48 Correct 6 ms 8788 KB Output is correct
49 Correct 9 ms 8916 KB Output is correct
50 Correct 6 ms 8928 KB Output is correct
51 Correct 6 ms 8788 KB Output is correct
52 Correct 6 ms 8780 KB Output is correct
53 Correct 6 ms 8788 KB Output is correct
54 Correct 6 ms 8788 KB Output is correct
55 Correct 4 ms 8532 KB Output is correct
56 Correct 52 ms 18328 KB Output is correct
57 Correct 58 ms 18012 KB Output is correct
58 Correct 6 ms 8788 KB Output is correct
59 Correct 5 ms 8532 KB Output is correct
60 Correct 5 ms 8532 KB Output is correct
61 Correct 59 ms 17980 KB Output is correct
62 Correct 6 ms 8780 KB Output is correct
63 Correct 78 ms 25212 KB Output is correct
64 Correct 66 ms 17996 KB Output is correct
65 Correct 6 ms 8780 KB Output is correct
66 Correct 59 ms 18764 KB Output is correct
67 Correct 6 ms 8788 KB Output is correct
68 Correct 6 ms 8776 KB Output is correct
69 Correct 50 ms 18256 KB Output is correct
70 Correct 6 ms 9044 KB Output is correct
71 Correct 61 ms 18028 KB Output is correct
72 Correct 84 ms 20360 KB Output is correct
73 Correct 5 ms 8532 KB Output is correct
74 Correct 72 ms 25412 KB Output is correct
75 Correct 60 ms 21736 KB Output is correct
76 Correct 8 ms 8788 KB Output is correct
77 Correct 8 ms 8788 KB Output is correct
78 Correct 44 ms 16972 KB Output is correct
79 Correct 69 ms 23568 KB Output is correct
80 Correct 8 ms 8916 KB Output is correct
81 Correct 6 ms 8776 KB Output is correct
82 Correct 4 ms 8588 KB Output is correct
83 Correct 5 ms 8532 KB Output is correct
84 Correct 5 ms 8788 KB Output is correct
85 Correct 68 ms 20500 KB Output is correct
86 Correct 6 ms 8788 KB Output is correct
87 Correct 66 ms 18152 KB Output is correct
88 Correct 4 ms 8532 KB Output is correct
89 Correct 55 ms 18076 KB Output is correct
90 Correct 70 ms 20464 KB Output is correct
91 Correct 89 ms 23300 KB Output is correct
92 Correct 49 ms 18284 KB Output is correct
93 Correct 8 ms 8788 KB Output is correct
94 Correct 5 ms 8532 KB Output is correct
95 Correct 74 ms 23112 KB Output is correct
96 Correct 85 ms 27140 KB Output is correct
97 Correct 6 ms 8788 KB Output is correct
98 Correct 4 ms 8532 KB Output is correct
99 Correct 61 ms 20656 KB Output is correct
100 Correct 6 ms 8788 KB Output is correct
101 Correct 68 ms 18744 KB Output is correct
102 Correct 86 ms 25888 KB Output is correct
103 Correct 6 ms 8916 KB Output is correct
104 Correct 6 ms 8916 KB Output is correct
105 Correct 6 ms 8788 KB Output is correct
106 Correct 6 ms 8788 KB Output is correct
107 Correct 6 ms 8900 KB Output is correct
108 Runtime error 305 ms 129120 KB Execution killed with signal 11
109 Halted 0 ms 0 KB -