답안 #331848

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
331848 2020-11-30T12:21:07 Z EndRay Mergers (JOI19_mergers) C++17
100 / 100
1959 ms 102380 KB
#include<bits/stdc++.h>

using namespace std;

const int N = 5e5+1, logN = 19+1;

int n, k;
vector<int> g[N];
int s[N];
int p[logN][N], h[N];

void dfs_init(int v=0, int pr=-1){
    for(int u : g[v]) if(u != pr){
        p[0][u] = v;
        h[u] = h[v]+1;
        dfs_init(u, v);
    }
}

void init(){
    p[0][0] = 0;
    h[0] = 0;
    dfs_init();
    for(int d = 1; d < logN; ++d)
        for(int i = 0; i < n; ++i)
            p[d][i] = p[d-1][p[d-1][i]];
}

int lca(int u, int v){
    if(h[u] < h[v])
        swap(u, v);
    int d = h[u] - h[v];
    for(int i = 0; i < logN; ++i)
        if((d&(1<<i)))
            u = p[i][u];
    for(int i = logN-1; i >= 0; --i)
        if(p[i][u] != p[i][v]){
            u = p[i][u];
            v = p[i][v];
        }
    return (u == v ? u : p[0][u]);
}

int color_lca[N];
int ans;

pair<int, int> dfs(int v=0, int pr=-1){
    int w = color_lca[s[v]];
    int deg = 0;
    for(int u : g[v]) if(u != pr){
        pair<int, int> l = dfs(u, v);
        w = lca(l.first, w);
        deg += l.second;
    }
    if(v == w){
        if(v) ++deg;
        if(deg == 1)
            ++ans;
        return {w, 1};
    }
    return {w, deg};
}

int main(){
    ios_base::sync_with_stdio(false);
    cout.tie(0);
    cin.tie(0);
    cin >> n >> k;
    if(n == 2){
        cout << k-1 << "\n";
        return 0;
    }
    for(int i = 0; i < n-1; ++i){
        int u, v;
        cin >> u >> v;
        --u; --v;
        g[u].push_back(v);
        g[v].push_back(u);
    }
    for(int i = 0; i < n; ++i){
        cin >> s[i];
        --s[i];
    }
    init();
    for(int i = 0; i < k; ++i)
        color_lca[i] = -1;
    for(int i = 0; i < n; ++i){
        if(color_lca[s[i]] == -1)
            color_lca[s[i]] = i;
        color_lca[s[i]] = lca(i, color_lca[s[i]]);
    }
    dfs();
    cout << (ans+1)/2 << "\n";
}
# 결과 실행 시간 메모리 Grader output
1 Correct 9 ms 12268 KB Output is correct
2 Correct 9 ms 12268 KB Output is correct
3 Correct 9 ms 12268 KB Output is correct
4 Correct 11 ms 12268 KB Output is correct
5 Correct 9 ms 12268 KB Output is correct
6 Correct 9 ms 12268 KB Output is correct
7 Correct 9 ms 12268 KB Output is correct
8 Correct 9 ms 12268 KB Output is correct
9 Correct 9 ms 12268 KB Output is correct
10 Correct 9 ms 12268 KB Output is correct
11 Correct 9 ms 12268 KB Output is correct
12 Correct 10 ms 12396 KB Output is correct
13 Correct 9 ms 12268 KB Output is correct
14 Correct 11 ms 12268 KB Output is correct
15 Correct 9 ms 12268 KB Output is correct
16 Correct 9 ms 12268 KB Output is correct
17 Correct 9 ms 12268 KB Output is correct
18 Correct 9 ms 12268 KB Output is correct
19 Correct 9 ms 12268 KB Output is correct
20 Correct 9 ms 12268 KB Output is correct
21 Correct 9 ms 12268 KB Output is correct
22 Correct 9 ms 12268 KB Output is correct
23 Correct 10 ms 12416 KB Output is correct
24 Correct 12 ms 12268 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 9 ms 12268 KB Output is correct
2 Correct 9 ms 12268 KB Output is correct
3 Correct 9 ms 12268 KB Output is correct
4 Correct 11 ms 12268 KB Output is correct
5 Correct 9 ms 12268 KB Output is correct
6 Correct 9 ms 12268 KB Output is correct
7 Correct 9 ms 12268 KB Output is correct
8 Correct 9 ms 12268 KB Output is correct
9 Correct 9 ms 12268 KB Output is correct
10 Correct 9 ms 12268 KB Output is correct
11 Correct 9 ms 12268 KB Output is correct
12 Correct 10 ms 12396 KB Output is correct
13 Correct 9 ms 12268 KB Output is correct
14 Correct 11 ms 12268 KB Output is correct
15 Correct 9 ms 12268 KB Output is correct
16 Correct 9 ms 12268 KB Output is correct
17 Correct 9 ms 12268 KB Output is correct
18 Correct 9 ms 12268 KB Output is correct
19 Correct 9 ms 12268 KB Output is correct
20 Correct 9 ms 12268 KB Output is correct
21 Correct 9 ms 12268 KB Output is correct
22 Correct 9 ms 12268 KB Output is correct
23 Correct 10 ms 12416 KB Output is correct
24 Correct 12 ms 12268 KB Output is correct
25 Correct 9 ms 12268 KB Output is correct
26 Correct 12 ms 12668 KB Output is correct
27 Correct 14 ms 12652 KB Output is correct
28 Correct 13 ms 12780 KB Output is correct
29 Correct 13 ms 12652 KB Output is correct
30 Correct 12 ms 12652 KB Output is correct
31 Correct 9 ms 12268 KB Output is correct
32 Correct 12 ms 12780 KB Output is correct
33 Correct 9 ms 12268 KB Output is correct
34 Correct 12 ms 12652 KB Output is correct
35 Correct 11 ms 12652 KB Output is correct
36 Correct 11 ms 12780 KB Output is correct
37 Correct 12 ms 12652 KB Output is correct
38 Correct 9 ms 12396 KB Output is correct
39 Correct 12 ms 12652 KB Output is correct
40 Correct 11 ms 12652 KB Output is correct
41 Correct 11 ms 12652 KB Output is correct
42 Correct 11 ms 12652 KB Output is correct
43 Correct 11 ms 12780 KB Output is correct
44 Correct 9 ms 12268 KB Output is correct
45 Correct 11 ms 12652 KB Output is correct
46 Correct 11 ms 12652 KB Output is correct
47 Correct 9 ms 12268 KB Output is correct
48 Correct 11 ms 12780 KB Output is correct
49 Correct 11 ms 12652 KB Output is correct
50 Correct 11 ms 12652 KB Output is correct
51 Correct 13 ms 12652 KB Output is correct
52 Correct 11 ms 12652 KB Output is correct
53 Correct 11 ms 12652 KB Output is correct
54 Correct 11 ms 12652 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 9 ms 12268 KB Output is correct
2 Correct 9 ms 12268 KB Output is correct
3 Correct 9 ms 12268 KB Output is correct
4 Correct 11 ms 12268 KB Output is correct
5 Correct 9 ms 12268 KB Output is correct
6 Correct 9 ms 12268 KB Output is correct
7 Correct 9 ms 12268 KB Output is correct
8 Correct 9 ms 12268 KB Output is correct
9 Correct 9 ms 12268 KB Output is correct
10 Correct 9 ms 12268 KB Output is correct
11 Correct 9 ms 12268 KB Output is correct
12 Correct 10 ms 12396 KB Output is correct
13 Correct 9 ms 12268 KB Output is correct
14 Correct 11 ms 12268 KB Output is correct
15 Correct 9 ms 12268 KB Output is correct
16 Correct 9 ms 12268 KB Output is correct
17 Correct 9 ms 12268 KB Output is correct
18 Correct 9 ms 12268 KB Output is correct
19 Correct 9 ms 12268 KB Output is correct
20 Correct 9 ms 12268 KB Output is correct
21 Correct 9 ms 12268 KB Output is correct
22 Correct 9 ms 12268 KB Output is correct
23 Correct 10 ms 12416 KB Output is correct
24 Correct 12 ms 12268 KB Output is correct
25 Correct 9 ms 12268 KB Output is correct
26 Correct 89 ms 25208 KB Output is correct
27 Correct 123 ms 24940 KB Output is correct
28 Correct 14 ms 12800 KB Output is correct
29 Correct 9 ms 12268 KB Output is correct
30 Correct 10 ms 12268 KB Output is correct
31 Correct 136 ms 25068 KB Output is correct
32 Correct 12 ms 12652 KB Output is correct
33 Correct 148 ms 31212 KB Output is correct
34 Correct 135 ms 25580 KB Output is correct
35 Correct 14 ms 12652 KB Output is correct
36 Correct 161 ms 26092 KB Output is correct
37 Correct 12 ms 12652 KB Output is correct
38 Correct 12 ms 12652 KB Output is correct
39 Correct 103 ms 25828 KB Output is correct
40 Correct 11 ms 12780 KB Output is correct
41 Correct 126 ms 25580 KB Output is correct
42 Correct 155 ms 27372 KB Output is correct
43 Correct 10 ms 12268 KB Output is correct
44 Correct 163 ms 31340 KB Output is correct
45 Correct 145 ms 28652 KB Output is correct
46 Correct 11 ms 12652 KB Output is correct
47 Correct 11 ms 12652 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 105 ms 24548 KB Output is correct
2 Correct 98 ms 24932 KB Output is correct
3 Correct 13 ms 12652 KB Output is correct
4 Correct 11 ms 12652 KB Output is correct
5 Correct 10 ms 12396 KB Output is correct
6 Correct 11 ms 12268 KB Output is correct
7 Correct 13 ms 12652 KB Output is correct
8 Correct 249 ms 26080 KB Output is correct
9 Correct 11 ms 12652 KB Output is correct
10 Correct 229 ms 24940 KB Output is correct
11 Correct 9 ms 12268 KB Output is correct
12 Correct 215 ms 25580 KB Output is correct
13 Correct 239 ms 26092 KB Output is correct
14 Correct 233 ms 26220 KB Output is correct
15 Correct 91 ms 25828 KB Output is correct
16 Correct 11 ms 12652 KB Output is correct
17 Correct 9 ms 12268 KB Output is correct
18 Correct 112 ms 26596 KB Output is correct
19 Correct 341 ms 29292 KB Output is correct
20 Correct 13 ms 12652 KB Output is correct
21 Correct 9 ms 12268 KB Output is correct
22 Correct 127 ms 26212 KB Output is correct
23 Correct 11 ms 12652 KB Output is correct
24 Correct 265 ms 25656 KB Output is correct
25 Correct 260 ms 28268 KB Output is correct
26 Correct 11 ms 12652 KB Output is correct
27 Correct 11 ms 12652 KB Output is correct
28 Correct 11 ms 12652 KB Output is correct
29 Correct 11 ms 12652 KB Output is correct
30 Correct 11 ms 12652 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 9 ms 12268 KB Output is correct
2 Correct 9 ms 12268 KB Output is correct
3 Correct 9 ms 12268 KB Output is correct
4 Correct 11 ms 12268 KB Output is correct
5 Correct 9 ms 12268 KB Output is correct
6 Correct 9 ms 12268 KB Output is correct
7 Correct 9 ms 12268 KB Output is correct
8 Correct 9 ms 12268 KB Output is correct
9 Correct 9 ms 12268 KB Output is correct
10 Correct 9 ms 12268 KB Output is correct
11 Correct 9 ms 12268 KB Output is correct
12 Correct 10 ms 12396 KB Output is correct
13 Correct 9 ms 12268 KB Output is correct
14 Correct 11 ms 12268 KB Output is correct
15 Correct 9 ms 12268 KB Output is correct
16 Correct 9 ms 12268 KB Output is correct
17 Correct 9 ms 12268 KB Output is correct
18 Correct 9 ms 12268 KB Output is correct
19 Correct 9 ms 12268 KB Output is correct
20 Correct 9 ms 12268 KB Output is correct
21 Correct 9 ms 12268 KB Output is correct
22 Correct 9 ms 12268 KB Output is correct
23 Correct 10 ms 12416 KB Output is correct
24 Correct 12 ms 12268 KB Output is correct
25 Correct 9 ms 12268 KB Output is correct
26 Correct 12 ms 12668 KB Output is correct
27 Correct 14 ms 12652 KB Output is correct
28 Correct 13 ms 12780 KB Output is correct
29 Correct 13 ms 12652 KB Output is correct
30 Correct 12 ms 12652 KB Output is correct
31 Correct 9 ms 12268 KB Output is correct
32 Correct 12 ms 12780 KB Output is correct
33 Correct 9 ms 12268 KB Output is correct
34 Correct 12 ms 12652 KB Output is correct
35 Correct 11 ms 12652 KB Output is correct
36 Correct 11 ms 12780 KB Output is correct
37 Correct 12 ms 12652 KB Output is correct
38 Correct 9 ms 12396 KB Output is correct
39 Correct 12 ms 12652 KB Output is correct
40 Correct 11 ms 12652 KB Output is correct
41 Correct 11 ms 12652 KB Output is correct
42 Correct 11 ms 12652 KB Output is correct
43 Correct 11 ms 12780 KB Output is correct
44 Correct 9 ms 12268 KB Output is correct
45 Correct 11 ms 12652 KB Output is correct
46 Correct 11 ms 12652 KB Output is correct
47 Correct 9 ms 12268 KB Output is correct
48 Correct 11 ms 12780 KB Output is correct
49 Correct 11 ms 12652 KB Output is correct
50 Correct 11 ms 12652 KB Output is correct
51 Correct 13 ms 12652 KB Output is correct
52 Correct 11 ms 12652 KB Output is correct
53 Correct 11 ms 12652 KB Output is correct
54 Correct 11 ms 12652 KB Output is correct
55 Correct 9 ms 12268 KB Output is correct
56 Correct 89 ms 25208 KB Output is correct
57 Correct 123 ms 24940 KB Output is correct
58 Correct 14 ms 12800 KB Output is correct
59 Correct 9 ms 12268 KB Output is correct
60 Correct 10 ms 12268 KB Output is correct
61 Correct 136 ms 25068 KB Output is correct
62 Correct 12 ms 12652 KB Output is correct
63 Correct 148 ms 31212 KB Output is correct
64 Correct 135 ms 25580 KB Output is correct
65 Correct 14 ms 12652 KB Output is correct
66 Correct 161 ms 26092 KB Output is correct
67 Correct 12 ms 12652 KB Output is correct
68 Correct 12 ms 12652 KB Output is correct
69 Correct 103 ms 25828 KB Output is correct
70 Correct 11 ms 12780 KB Output is correct
71 Correct 126 ms 25580 KB Output is correct
72 Correct 155 ms 27372 KB Output is correct
73 Correct 10 ms 12268 KB Output is correct
74 Correct 163 ms 31340 KB Output is correct
75 Correct 145 ms 28652 KB Output is correct
76 Correct 11 ms 12652 KB Output is correct
77 Correct 11 ms 12652 KB Output is correct
78 Correct 105 ms 24548 KB Output is correct
79 Correct 98 ms 24932 KB Output is correct
80 Correct 13 ms 12652 KB Output is correct
81 Correct 11 ms 12652 KB Output is correct
82 Correct 10 ms 12396 KB Output is correct
83 Correct 11 ms 12268 KB Output is correct
84 Correct 13 ms 12652 KB Output is correct
85 Correct 249 ms 26080 KB Output is correct
86 Correct 11 ms 12652 KB Output is correct
87 Correct 229 ms 24940 KB Output is correct
88 Correct 9 ms 12268 KB Output is correct
89 Correct 215 ms 25580 KB Output is correct
90 Correct 239 ms 26092 KB Output is correct
91 Correct 233 ms 26220 KB Output is correct
92 Correct 91 ms 25828 KB Output is correct
93 Correct 11 ms 12652 KB Output is correct
94 Correct 9 ms 12268 KB Output is correct
95 Correct 112 ms 26596 KB Output is correct
96 Correct 341 ms 29292 KB Output is correct
97 Correct 13 ms 12652 KB Output is correct
98 Correct 9 ms 12268 KB Output is correct
99 Correct 127 ms 26212 KB Output is correct
100 Correct 11 ms 12652 KB Output is correct
101 Correct 265 ms 25656 KB Output is correct
102 Correct 260 ms 28268 KB Output is correct
103 Correct 11 ms 12652 KB Output is correct
104 Correct 11 ms 12652 KB Output is correct
105 Correct 11 ms 12652 KB Output is correct
106 Correct 11 ms 12652 KB Output is correct
107 Correct 11 ms 12652 KB Output is correct
108 Correct 925 ms 83420 KB Output is correct
109 Correct 1077 ms 93216 KB Output is correct
110 Correct 1049 ms 102380 KB Output is correct
111 Correct 1959 ms 101600 KB Output is correct
112 Correct 1225 ms 93876 KB Output is correct
113 Correct 718 ms 84948 KB Output is correct
114 Correct 836 ms 79084 KB Output is correct
115 Correct 819 ms 78904 KB Output is correct
116 Correct 1413 ms 81296 KB Output is correct
117 Correct 1420 ms 83428 KB Output is correct
118 Correct 1209 ms 78828 KB Output is correct
119 Correct 1413 ms 83448 KB Output is correct
120 Correct 1681 ms 94444 KB Output is correct
121 Correct 1408 ms 83300 KB Output is correct
122 Correct 1291 ms 82540 KB Output is correct
123 Correct 656 ms 84700 KB Output is correct
124 Correct 1732 ms 81644 KB Output is correct