답안 #331843

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
331843 2020-11-30T12:08:11 Z EndRay Mergers (JOI19_mergers) C++17
10 / 100
245 ms 29292 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 start;
int p[logN][N], h[N];

void dfs_init(int v, 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(int start){
    p[0][start] = 0;
    h[start] = 0;
    dfs_init(start);
    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 = 0;

int dfs(int v, int pr=-1){
    int w = color_lca[s[v]];
    int fail = 0;
    for(int u : g[v]) if(u != pr){
        int l = dfs(u, v);
        if(l < 0){
            fail -= l;
            continue;
        }
        w = lca(l, w);
    }
    if(v == start && fail == 1){
        ++ans;
        return -1;
    }
    if(fail)
        return (v == w ? -1 : -fail);
    if(v == w){
        ++ans;
        return -1;
    }
    return w;
}

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];
    }
    for(int i = 0; i < n; ++i)
        if(g[i].size() > 1)
            start = i;
    init(start);
    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(start);
    cout << (ans == 1 ? 0 : (ans+1)/2) << "\n";
}
# 결과 실행 시간 메모리 Grader output
1 Correct 9 ms 12396 KB Output is correct
2 Correct 8 ms 12268 KB Output is correct
3 Correct 8 ms 12268 KB Output is correct
4 Correct 9 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 8 ms 12268 KB Output is correct
10 Correct 8 ms 12268 KB Output is correct
11 Correct 9 ms 12268 KB Output is correct
12 Correct 8 ms 12268 KB Output is correct
13 Correct 8 ms 12268 KB Output is correct
14 Correct 9 ms 12416 KB Output is correct
15 Correct 11 ms 12268 KB Output is correct
16 Correct 9 ms 12268 KB Output is correct
17 Correct 8 ms 12268 KB Output is correct
18 Correct 9 ms 12268 KB Output is correct
19 Correct 8 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 8 ms 12268 KB Output is correct
23 Correct 8 ms 12268 KB Output is correct
24 Correct 9 ms 12268 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 9 ms 12396 KB Output is correct
2 Correct 8 ms 12268 KB Output is correct
3 Correct 8 ms 12268 KB Output is correct
4 Correct 9 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 8 ms 12268 KB Output is correct
10 Correct 8 ms 12268 KB Output is correct
11 Correct 9 ms 12268 KB Output is correct
12 Correct 8 ms 12268 KB Output is correct
13 Correct 8 ms 12268 KB Output is correct
14 Correct 9 ms 12416 KB Output is correct
15 Correct 11 ms 12268 KB Output is correct
16 Correct 9 ms 12268 KB Output is correct
17 Correct 8 ms 12268 KB Output is correct
18 Correct 9 ms 12268 KB Output is correct
19 Correct 8 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 8 ms 12268 KB Output is correct
23 Correct 8 ms 12268 KB Output is correct
24 Correct 9 ms 12268 KB Output is correct
25 Correct 9 ms 12268 KB Output is correct
26 Correct 12 ms 12652 KB Output is correct
27 Correct 12 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
31 Correct 8 ms 12268 KB Output is correct
32 Correct 10 ms 12780 KB Output is correct
33 Correct 9 ms 12268 KB Output is correct
34 Correct 11 ms 12652 KB Output is correct
35 Correct 10 ms 12652 KB Output is correct
36 Correct 10 ms 12652 KB Output is correct
37 Correct 11 ms 12652 KB Output is correct
38 Correct 9 ms 12268 KB Output is correct
39 Incorrect 11 ms 12652 KB Output isn't correct
40 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 9 ms 12396 KB Output is correct
2 Correct 8 ms 12268 KB Output is correct
3 Correct 8 ms 12268 KB Output is correct
4 Correct 9 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 8 ms 12268 KB Output is correct
10 Correct 8 ms 12268 KB Output is correct
11 Correct 9 ms 12268 KB Output is correct
12 Correct 8 ms 12268 KB Output is correct
13 Correct 8 ms 12268 KB Output is correct
14 Correct 9 ms 12416 KB Output is correct
15 Correct 11 ms 12268 KB Output is correct
16 Correct 9 ms 12268 KB Output is correct
17 Correct 8 ms 12268 KB Output is correct
18 Correct 9 ms 12268 KB Output is correct
19 Correct 8 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 8 ms 12268 KB Output is correct
23 Correct 8 ms 12268 KB Output is correct
24 Correct 9 ms 12268 KB Output is correct
25 Correct 11 ms 12268 KB Output is correct
26 Correct 109 ms 24420 KB Output is correct
27 Correct 142 ms 24300 KB Output is correct
28 Correct 11 ms 12652 KB Output is correct
29 Correct 9 ms 12268 KB Output is correct
30 Correct 9 ms 12268 KB Output is correct
31 Correct 161 ms 24044 KB Output is correct
32 Correct 10 ms 12652 KB Output is correct
33 Correct 148 ms 29292 KB Output is correct
34 Correct 134 ms 25600 KB Output is correct
35 Correct 10 ms 12652 KB Output is correct
36 Correct 159 ms 26092 KB Output is correct
37 Correct 11 ms 12652 KB Output is correct
38 Correct 10 ms 12652 KB Output is correct
39 Correct 107 ms 25828 KB Output is correct
40 Correct 12 ms 12780 KB Output is correct
41 Correct 119 ms 25452 KB Output is correct
42 Correct 141 ms 28012 KB Output is correct
43 Incorrect 8 ms 12268 KB Output isn't correct
44 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 86 ms 24420 KB Output is correct
2 Correct 89 ms 24804 KB Output is correct
3 Correct 11 ms 12652 KB Output is correct
4 Correct 14 ms 12652 KB Output is correct
5 Correct 10 ms 12268 KB Output is correct
6 Correct 10 ms 12268 KB Output is correct
7 Correct 10 ms 12652 KB Output is correct
8 Correct 245 ms 24300 KB Output is correct
9 Correct 11 ms 12652 KB Output is correct
10 Correct 196 ms 24044 KB Output is correct
11 Correct 9 ms 12268 KB Output is correct
12 Correct 209 ms 25580 KB Output is correct
13 Incorrect 215 ms 25964 KB Output isn't correct
14 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 9 ms 12396 KB Output is correct
2 Correct 8 ms 12268 KB Output is correct
3 Correct 8 ms 12268 KB Output is correct
4 Correct 9 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 8 ms 12268 KB Output is correct
10 Correct 8 ms 12268 KB Output is correct
11 Correct 9 ms 12268 KB Output is correct
12 Correct 8 ms 12268 KB Output is correct
13 Correct 8 ms 12268 KB Output is correct
14 Correct 9 ms 12416 KB Output is correct
15 Correct 11 ms 12268 KB Output is correct
16 Correct 9 ms 12268 KB Output is correct
17 Correct 8 ms 12268 KB Output is correct
18 Correct 9 ms 12268 KB Output is correct
19 Correct 8 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 8 ms 12268 KB Output is correct
23 Correct 8 ms 12268 KB Output is correct
24 Correct 9 ms 12268 KB Output is correct
25 Correct 9 ms 12268 KB Output is correct
26 Correct 12 ms 12652 KB Output is correct
27 Correct 12 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
31 Correct 8 ms 12268 KB Output is correct
32 Correct 10 ms 12780 KB Output is correct
33 Correct 9 ms 12268 KB Output is correct
34 Correct 11 ms 12652 KB Output is correct
35 Correct 10 ms 12652 KB Output is correct
36 Correct 10 ms 12652 KB Output is correct
37 Correct 11 ms 12652 KB Output is correct
38 Correct 9 ms 12268 KB Output is correct
39 Incorrect 11 ms 12652 KB Output isn't correct
40 Halted 0 ms 0 KB -