답안 #128226

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
128226 2019-07-10T14:39:30 Z Osama_Alkhodairy Mergers (JOI19_mergers) C++17
100 / 100
1614 ms 132924 KB
#include <bits/stdc++.h>
using namespace std;
#define finish(x) return cout << x << endl, 0
#define ll long long

const int N = 500001;

int n, k, s[N], sum[N], dep[N], dfstime[N], deg[N], p[N][20];
vector <int> v[N], h[N];
int ct;

void dfsz(int node, int pnode){
    dfstime[node] = ct++;
    dep[node] = dep[pnode] + 1;
    p[node][0] = pnode;
    for(int i = 1 ; i < 20 ; i++){
        p[node][i] = p[p[node][i - 1]][i - 1];
    }
    for(auto &i : v[node]){
        if(i == pnode) continue;
        dfsz(i, node);
    }
}
int lift(int node, int k){
    for(int i = 19 ; i >= 0 ; i--){
        if(k & (1 << i)){
            node = p[node][i];
        }
    }
    return node;
}
int LCA(int a, int b){
    if(dep[a] > dep[b]) swap(a, b);
    b = lift(b, dep[b] - dep[a]);
    if(a == b) return a;
    for(int i = 19 ; i >= 0 ; i--){
        if(p[a][i] != p[b][i]){
            a = p[a][i];
            b = p[b][i];
        }
    }
    return p[a][0];
}
void add_path(int x, int y){
    sum[x]++;
    sum[y]++;
    sum[LCA(x, y)] -= 2;
}
void dfs(int node, int pnode){
    for(auto &i : v[node]){
        if(i == pnode) continue;
        dfs(i, node);
        sum[node] += sum[i];
    }
}
void dfs(int node, int pnode, int root){
    for(auto &i : v[node]){
        if(i == pnode) continue;
        if(sum[i] == 0){
            deg[root]++;
            deg[i]++;
            dfs(i, node, i);
        }
        else dfs(i, node, root);
    }
}
int main(){
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    cin >> n >> k;
    for(int i = 0 ; i < n - 1 ; i++){
        int x, y;
        cin >> x >> y;
        v[x].push_back(y);
        v[y].push_back(x);
    }
    for(int i = 1 ; i <= n ; i++){
        cin >> s[i];
        h[s[i]].push_back(i);
    }
    dfsz(1, 0);
    for(int i = 1 ; i <= k ; i++){
        sort(h[i].begin(), h[i].end(), [&](int l, int r){
            return dfstime[l] < dfstime[r];
        });
        int nn = h[i].size();
        for(int j = 0 ; j < nn ; j++){
            add_path(h[i][j], h[i][(j + 1) % nn]);
        }
    }
    dfs(1, 0);
    dfs(1, 0, 1);
    int ans = 0;
    for(int i = 1 ; i <= n ; i++){
        ans += deg[i] == 1;
    }
    cout << (ans + 1) / 2 << endl;
}
# 결과 실행 시간 메모리 Grader output
1 Correct 29 ms 23928 KB Output is correct
2 Correct 30 ms 23928 KB Output is correct
3 Correct 29 ms 23928 KB Output is correct
4 Correct 31 ms 23900 KB Output is correct
5 Correct 31 ms 23800 KB Output is correct
6 Correct 25 ms 23800 KB Output is correct
7 Correct 24 ms 23800 KB Output is correct
8 Correct 24 ms 23800 KB Output is correct
9 Correct 24 ms 23932 KB Output is correct
10 Correct 24 ms 23800 KB Output is correct
11 Correct 24 ms 23904 KB Output is correct
12 Correct 25 ms 23800 KB Output is correct
13 Correct 25 ms 23800 KB Output is correct
14 Correct 25 ms 23928 KB Output is correct
15 Correct 24 ms 23928 KB Output is correct
16 Correct 25 ms 23928 KB Output is correct
17 Correct 25 ms 23900 KB Output is correct
18 Correct 29 ms 23928 KB Output is correct
19 Correct 30 ms 23828 KB Output is correct
20 Correct 31 ms 23836 KB Output is correct
21 Correct 30 ms 23800 KB Output is correct
22 Correct 31 ms 23804 KB Output is correct
23 Correct 31 ms 23868 KB Output is correct
24 Correct 31 ms 23800 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 29 ms 23928 KB Output is correct
2 Correct 30 ms 23928 KB Output is correct
3 Correct 29 ms 23928 KB Output is correct
4 Correct 31 ms 23900 KB Output is correct
5 Correct 31 ms 23800 KB Output is correct
6 Correct 25 ms 23800 KB Output is correct
7 Correct 24 ms 23800 KB Output is correct
8 Correct 24 ms 23800 KB Output is correct
9 Correct 24 ms 23932 KB Output is correct
10 Correct 24 ms 23800 KB Output is correct
11 Correct 24 ms 23904 KB Output is correct
12 Correct 25 ms 23800 KB Output is correct
13 Correct 25 ms 23800 KB Output is correct
14 Correct 25 ms 23928 KB Output is correct
15 Correct 24 ms 23928 KB Output is correct
16 Correct 25 ms 23928 KB Output is correct
17 Correct 25 ms 23900 KB Output is correct
18 Correct 29 ms 23928 KB Output is correct
19 Correct 30 ms 23828 KB Output is correct
20 Correct 31 ms 23836 KB Output is correct
21 Correct 30 ms 23800 KB Output is correct
22 Correct 31 ms 23804 KB Output is correct
23 Correct 31 ms 23868 KB Output is correct
24 Correct 31 ms 23800 KB Output is correct
25 Correct 31 ms 23928 KB Output is correct
26 Correct 33 ms 24356 KB Output is correct
27 Correct 34 ms 24312 KB Output is correct
28 Correct 34 ms 24444 KB Output is correct
29 Correct 33 ms 24312 KB Output is correct
30 Correct 35 ms 24312 KB Output is correct
31 Correct 26 ms 23928 KB Output is correct
32 Correct 27 ms 24540 KB Output is correct
33 Correct 25 ms 23800 KB Output is correct
34 Correct 27 ms 24312 KB Output is correct
35 Correct 27 ms 24412 KB Output is correct
36 Correct 27 ms 24312 KB Output is correct
37 Correct 27 ms 24312 KB Output is correct
38 Correct 24 ms 23928 KB Output is correct
39 Correct 32 ms 24312 KB Output is correct
40 Correct 44 ms 24312 KB Output is correct
41 Correct 27 ms 24312 KB Output is correct
42 Correct 19 ms 24312 KB Output is correct
43 Correct 31 ms 24568 KB Output is correct
44 Correct 25 ms 23928 KB Output is correct
45 Correct 38 ms 24568 KB Output is correct
46 Correct 28 ms 24312 KB Output is correct
47 Correct 25 ms 23928 KB Output is correct
48 Correct 28 ms 24312 KB Output is correct
49 Correct 33 ms 24412 KB Output is correct
50 Correct 38 ms 24484 KB Output is correct
51 Correct 27 ms 24312 KB Output is correct
52 Correct 37 ms 24440 KB Output is correct
53 Correct 27 ms 24312 KB Output is correct
54 Correct 27 ms 24312 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 29 ms 23928 KB Output is correct
2 Correct 30 ms 23928 KB Output is correct
3 Correct 29 ms 23928 KB Output is correct
4 Correct 31 ms 23900 KB Output is correct
5 Correct 31 ms 23800 KB Output is correct
6 Correct 25 ms 23800 KB Output is correct
7 Correct 24 ms 23800 KB Output is correct
8 Correct 24 ms 23800 KB Output is correct
9 Correct 24 ms 23932 KB Output is correct
10 Correct 24 ms 23800 KB Output is correct
11 Correct 24 ms 23904 KB Output is correct
12 Correct 25 ms 23800 KB Output is correct
13 Correct 25 ms 23800 KB Output is correct
14 Correct 25 ms 23928 KB Output is correct
15 Correct 24 ms 23928 KB Output is correct
16 Correct 25 ms 23928 KB Output is correct
17 Correct 25 ms 23900 KB Output is correct
18 Correct 29 ms 23928 KB Output is correct
19 Correct 30 ms 23828 KB Output is correct
20 Correct 31 ms 23836 KB Output is correct
21 Correct 30 ms 23800 KB Output is correct
22 Correct 31 ms 23804 KB Output is correct
23 Correct 31 ms 23868 KB Output is correct
24 Correct 31 ms 23800 KB Output is correct
25 Correct 29 ms 23860 KB Output is correct
26 Correct 155 ms 38788 KB Output is correct
27 Correct 194 ms 38404 KB Output is correct
28 Correct 27 ms 24312 KB Output is correct
29 Correct 24 ms 23928 KB Output is correct
30 Correct 21 ms 23800 KB Output is correct
31 Correct 183 ms 38580 KB Output is correct
32 Correct 30 ms 24312 KB Output is correct
33 Correct 248 ms 44152 KB Output is correct
34 Correct 196 ms 38392 KB Output is correct
35 Correct 27 ms 24312 KB Output is correct
36 Correct 211 ms 39160 KB Output is correct
37 Correct 27 ms 24316 KB Output is correct
38 Correct 27 ms 24312 KB Output is correct
39 Correct 144 ms 38744 KB Output is correct
40 Correct 27 ms 24440 KB Output is correct
41 Correct 197 ms 38528 KB Output is correct
42 Correct 235 ms 40284 KB Output is correct
43 Correct 29 ms 23800 KB Output is correct
44 Correct 283 ms 44284 KB Output is correct
45 Correct 227 ms 41432 KB Output is correct
46 Correct 26 ms 24312 KB Output is correct
47 Correct 27 ms 24312 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 142 ms 37360 KB Output is correct
2 Correct 152 ms 40380 KB Output is correct
3 Correct 27 ms 24312 KB Output is correct
4 Correct 27 ms 24312 KB Output is correct
5 Correct 26 ms 23800 KB Output is correct
6 Correct 24 ms 23800 KB Output is correct
7 Correct 26 ms 24312 KB Output is correct
8 Correct 217 ms 40140 KB Output is correct
9 Correct 29 ms 24312 KB Output is correct
10 Correct 205 ms 39144 KB Output is correct
11 Correct 25 ms 23928 KB Output is correct
12 Correct 222 ms 38904 KB Output is correct
13 Correct 204 ms 40184 KB Output is correct
14 Correct 202 ms 41932 KB Output is correct
15 Correct 144 ms 38856 KB Output is correct
16 Correct 27 ms 24284 KB Output is correct
17 Correct 24 ms 23924 KB Output is correct
18 Correct 154 ms 41736 KB Output is correct
19 Correct 232 ms 45004 KB Output is correct
20 Correct 28 ms 24312 KB Output is correct
21 Correct 25 ms 23928 KB Output is correct
22 Correct 158 ms 40560 KB Output is correct
23 Correct 27 ms 24316 KB Output is correct
24 Correct 212 ms 39388 KB Output is correct
25 Correct 211 ms 43768 KB Output is correct
26 Correct 27 ms 24440 KB Output is correct
27 Correct 27 ms 24440 KB Output is correct
28 Correct 27 ms 24312 KB Output is correct
29 Correct 27 ms 24312 KB Output is correct
30 Correct 27 ms 24312 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 29 ms 23928 KB Output is correct
2 Correct 30 ms 23928 KB Output is correct
3 Correct 29 ms 23928 KB Output is correct
4 Correct 31 ms 23900 KB Output is correct
5 Correct 31 ms 23800 KB Output is correct
6 Correct 25 ms 23800 KB Output is correct
7 Correct 24 ms 23800 KB Output is correct
8 Correct 24 ms 23800 KB Output is correct
9 Correct 24 ms 23932 KB Output is correct
10 Correct 24 ms 23800 KB Output is correct
11 Correct 24 ms 23904 KB Output is correct
12 Correct 25 ms 23800 KB Output is correct
13 Correct 25 ms 23800 KB Output is correct
14 Correct 25 ms 23928 KB Output is correct
15 Correct 24 ms 23928 KB Output is correct
16 Correct 25 ms 23928 KB Output is correct
17 Correct 25 ms 23900 KB Output is correct
18 Correct 29 ms 23928 KB Output is correct
19 Correct 30 ms 23828 KB Output is correct
20 Correct 31 ms 23836 KB Output is correct
21 Correct 30 ms 23800 KB Output is correct
22 Correct 31 ms 23804 KB Output is correct
23 Correct 31 ms 23868 KB Output is correct
24 Correct 31 ms 23800 KB Output is correct
25 Correct 31 ms 23928 KB Output is correct
26 Correct 33 ms 24356 KB Output is correct
27 Correct 34 ms 24312 KB Output is correct
28 Correct 34 ms 24444 KB Output is correct
29 Correct 33 ms 24312 KB Output is correct
30 Correct 35 ms 24312 KB Output is correct
31 Correct 26 ms 23928 KB Output is correct
32 Correct 27 ms 24540 KB Output is correct
33 Correct 25 ms 23800 KB Output is correct
34 Correct 27 ms 24312 KB Output is correct
35 Correct 27 ms 24412 KB Output is correct
36 Correct 27 ms 24312 KB Output is correct
37 Correct 27 ms 24312 KB Output is correct
38 Correct 24 ms 23928 KB Output is correct
39 Correct 32 ms 24312 KB Output is correct
40 Correct 44 ms 24312 KB Output is correct
41 Correct 27 ms 24312 KB Output is correct
42 Correct 19 ms 24312 KB Output is correct
43 Correct 31 ms 24568 KB Output is correct
44 Correct 25 ms 23928 KB Output is correct
45 Correct 38 ms 24568 KB Output is correct
46 Correct 28 ms 24312 KB Output is correct
47 Correct 25 ms 23928 KB Output is correct
48 Correct 28 ms 24312 KB Output is correct
49 Correct 33 ms 24412 KB Output is correct
50 Correct 38 ms 24484 KB Output is correct
51 Correct 27 ms 24312 KB Output is correct
52 Correct 37 ms 24440 KB Output is correct
53 Correct 27 ms 24312 KB Output is correct
54 Correct 27 ms 24312 KB Output is correct
55 Correct 29 ms 23860 KB Output is correct
56 Correct 155 ms 38788 KB Output is correct
57 Correct 194 ms 38404 KB Output is correct
58 Correct 27 ms 24312 KB Output is correct
59 Correct 24 ms 23928 KB Output is correct
60 Correct 21 ms 23800 KB Output is correct
61 Correct 183 ms 38580 KB Output is correct
62 Correct 30 ms 24312 KB Output is correct
63 Correct 248 ms 44152 KB Output is correct
64 Correct 196 ms 38392 KB Output is correct
65 Correct 27 ms 24312 KB Output is correct
66 Correct 211 ms 39160 KB Output is correct
67 Correct 27 ms 24316 KB Output is correct
68 Correct 27 ms 24312 KB Output is correct
69 Correct 144 ms 38744 KB Output is correct
70 Correct 27 ms 24440 KB Output is correct
71 Correct 197 ms 38528 KB Output is correct
72 Correct 235 ms 40284 KB Output is correct
73 Correct 29 ms 23800 KB Output is correct
74 Correct 283 ms 44284 KB Output is correct
75 Correct 227 ms 41432 KB Output is correct
76 Correct 26 ms 24312 KB Output is correct
77 Correct 27 ms 24312 KB Output is correct
78 Correct 142 ms 37360 KB Output is correct
79 Correct 152 ms 40380 KB Output is correct
80 Correct 27 ms 24312 KB Output is correct
81 Correct 27 ms 24312 KB Output is correct
82 Correct 26 ms 23800 KB Output is correct
83 Correct 24 ms 23800 KB Output is correct
84 Correct 26 ms 24312 KB Output is correct
85 Correct 217 ms 40140 KB Output is correct
86 Correct 29 ms 24312 KB Output is correct
87 Correct 205 ms 39144 KB Output is correct
88 Correct 25 ms 23928 KB Output is correct
89 Correct 222 ms 38904 KB Output is correct
90 Correct 204 ms 40184 KB Output is correct
91 Correct 202 ms 41932 KB Output is correct
92 Correct 144 ms 38856 KB Output is correct
93 Correct 27 ms 24284 KB Output is correct
94 Correct 24 ms 23924 KB Output is correct
95 Correct 154 ms 41736 KB Output is correct
96 Correct 232 ms 45004 KB Output is correct
97 Correct 28 ms 24312 KB Output is correct
98 Correct 25 ms 23928 KB Output is correct
99 Correct 158 ms 40560 KB Output is correct
100 Correct 27 ms 24316 KB Output is correct
101 Correct 212 ms 39388 KB Output is correct
102 Correct 211 ms 43768 KB Output is correct
103 Correct 27 ms 24440 KB Output is correct
104 Correct 27 ms 24440 KB Output is correct
105 Correct 27 ms 24312 KB Output is correct
106 Correct 27 ms 24312 KB Output is correct
107 Correct 27 ms 24312 KB Output is correct
108 Correct 1094 ms 106868 KB Output is correct
109 Correct 1339 ms 111232 KB Output is correct
110 Correct 1515 ms 120268 KB Output is correct
111 Correct 1490 ms 132924 KB Output is correct
112 Correct 1363 ms 125252 KB Output is correct
113 Correct 934 ms 114648 KB Output is correct
114 Correct 1010 ms 96812 KB Output is correct
115 Correct 1000 ms 96868 KB Output is correct
116 Correct 1584 ms 103196 KB Output is correct
117 Correct 1183 ms 114680 KB Output is correct
118 Correct 1084 ms 98780 KB Output is correct
119 Correct 1154 ms 114492 KB Output is correct
120 Correct 1614 ms 125648 KB Output is correct
121 Correct 1235 ms 114808 KB Output is correct
122 Correct 1252 ms 108300 KB Output is correct
123 Correct 899 ms 116104 KB Output is correct
124 Correct 1414 ms 112632 KB Output is correct