Submission #828663

# Submission time Handle Problem Language Result Execution time Memory
828663 2023-08-17T13:18:21 Z Ronin13 Mergers (JOI19_mergers) C++17
100 / 100
982 ms 131420 KB
#include <bits/stdc++.h>
#define ll long long
#define ull unsigned ll
#define f first
#define s second
#define pii pair<ll,ll>
#define pll pair<ll,ll>
#define pb push_back
#define epb emplace_back
using namespace std;
const int nmax = 5e5 + 1;
 
 
int in[nmax], out[nmax];
int timer = 0;
int p[nmax][22];
vector <vector <int> > g(nmax);
void dfs(int v, int e){
    in[v] = timer++;
    p[v][0] = e;
    for(int j = 1; j <= 20; j++)
        p[v][j] = p[p[v][j - 1]][j - 1];
    for(int to : g[v]){
        if(to == e) continue;
        dfs(to, v);
    }
    out[v] = timer++;
}

bool isancestor(int u, int v){
    return in[u] <= in[v] && out[u] >= out[v];
}

int lca(int u, int v){
    if(isancestor(u, v)) return u;
    if(isancestor(v, u)) return v;
    for(int j = 20; j >= 0; j--){
        if(!isancestor(p[u][j], v)) u = p[u][j];
    }
    return p[u][0];
}

vector <int> sz(nmax);
vector <int> a(nmax);
int c = 0;
void DFS(int v, int e){
    sz[v] = 1;
    for(int to : g[v]){
        if(to == e) continue;
        DFS(to, v);
        sz[v] += sz[to];
    }
    sz[v] -= a[v];
    if(sz[v] == 0) c++;
}

int cnt[nmax];
int leaf = 0;
void ansdfs(int v, int cur, int e){
    if(sz[v] == 0 && cur != v) cnt[v]++, cnt[cur]++;
    if(sz[v] == 0) cur = v;
    for(int to : g[v]){
        if(to != e)
        ansdfs(to, cur, v);
    }
    if(cnt[v] == 1) leaf++;
}

int main(){
   ios_base::sync_with_stdio(false); cin.tie(0);
    int n; cin >> n;
    int k; cin >> k;
    vector <vector <int> > vec(k + 1);
    for(int i = 1; i < n; i++){
        int u, v; cin >> u >> v;
        g[u].pb(v); g[v].pb(u);
    }
    dfs(1, 1);
    for(int i = 1; i <= n; ++i){
        int x; cin >> x;
        vec[x].pb(i);
    }
    
    for(int i = 1; i <= k; i++){
        if(vec[i].empty()) continue;
        int lc = vec[i][0];
        for(int to : vec[i]) lc = lca(lc, to);
        a[lc] += vec[i].size();
    }
    DFS(1, 1);
    if(c == 1) cout << 0 << "\n";
    else{ansdfs(1, 1, 1);
    cout << ( leaf + 1) / 2 << "\n";}
    return 0;
}
# Verdict Execution time Memory Grader output
1 Correct 7 ms 15956 KB Output is correct
2 Correct 8 ms 15920 KB Output is correct
3 Correct 8 ms 15936 KB Output is correct
4 Correct 7 ms 15956 KB Output is correct
5 Correct 8 ms 15956 KB Output is correct
6 Correct 8 ms 15936 KB Output is correct
7 Correct 8 ms 15956 KB Output is correct
8 Correct 7 ms 15980 KB Output is correct
9 Correct 7 ms 15956 KB Output is correct
10 Correct 8 ms 15956 KB Output is correct
11 Correct 7 ms 15956 KB Output is correct
12 Correct 9 ms 15956 KB Output is correct
13 Correct 7 ms 16008 KB Output is correct
14 Correct 7 ms 15956 KB Output is correct
15 Correct 8 ms 15956 KB Output is correct
16 Correct 8 ms 15956 KB Output is correct
17 Correct 7 ms 15936 KB Output is correct
18 Correct 10 ms 15956 KB Output is correct
19 Correct 9 ms 15932 KB Output is correct
20 Correct 8 ms 15932 KB Output is correct
21 Correct 8 ms 15956 KB Output is correct
22 Correct 9 ms 15956 KB Output is correct
23 Correct 8 ms 15956 KB Output is correct
24 Correct 9 ms 15956 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 7 ms 15956 KB Output is correct
2 Correct 8 ms 15920 KB Output is correct
3 Correct 8 ms 15936 KB Output is correct
4 Correct 7 ms 15956 KB Output is correct
5 Correct 8 ms 15956 KB Output is correct
6 Correct 8 ms 15936 KB Output is correct
7 Correct 8 ms 15956 KB Output is correct
8 Correct 7 ms 15980 KB Output is correct
9 Correct 7 ms 15956 KB Output is correct
10 Correct 8 ms 15956 KB Output is correct
11 Correct 7 ms 15956 KB Output is correct
12 Correct 9 ms 15956 KB Output is correct
13 Correct 7 ms 16008 KB Output is correct
14 Correct 7 ms 15956 KB Output is correct
15 Correct 8 ms 15956 KB Output is correct
16 Correct 8 ms 15956 KB Output is correct
17 Correct 7 ms 15936 KB Output is correct
18 Correct 10 ms 15956 KB Output is correct
19 Correct 9 ms 15932 KB Output is correct
20 Correct 8 ms 15932 KB Output is correct
21 Correct 8 ms 15956 KB Output is correct
22 Correct 9 ms 15956 KB Output is correct
23 Correct 8 ms 15956 KB Output is correct
24 Correct 9 ms 15956 KB Output is correct
25 Correct 7 ms 15956 KB Output is correct
26 Correct 9 ms 16468 KB Output is correct
27 Correct 10 ms 16340 KB Output is correct
28 Correct 9 ms 16596 KB Output is correct
29 Correct 9 ms 16468 KB Output is correct
30 Correct 9 ms 16340 KB Output is correct
31 Correct 7 ms 15992 KB Output is correct
32 Correct 9 ms 16704 KB Output is correct
33 Correct 7 ms 15956 KB Output is correct
34 Correct 9 ms 16340 KB Output is correct
35 Correct 9 ms 16468 KB Output is correct
36 Correct 9 ms 16340 KB Output is correct
37 Correct 10 ms 16468 KB Output is correct
38 Correct 7 ms 15964 KB Output is correct
39 Correct 10 ms 16468 KB Output is correct
40 Correct 9 ms 16396 KB Output is correct
41 Correct 9 ms 16340 KB Output is correct
42 Correct 10 ms 16468 KB Output is correct
43 Correct 9 ms 16536 KB Output is correct
44 Correct 8 ms 15956 KB Output is correct
45 Correct 9 ms 16596 KB Output is correct
46 Correct 8 ms 16468 KB Output is correct
47 Correct 7 ms 15956 KB Output is correct
48 Correct 9 ms 16452 KB Output is correct
49 Correct 9 ms 16468 KB Output is correct
50 Correct 9 ms 16592 KB Output is correct
51 Correct 9 ms 16340 KB Output is correct
52 Correct 9 ms 16412 KB Output is correct
53 Correct 9 ms 16424 KB Output is correct
54 Correct 9 ms 16340 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 7 ms 15956 KB Output is correct
2 Correct 8 ms 15920 KB Output is correct
3 Correct 8 ms 15936 KB Output is correct
4 Correct 7 ms 15956 KB Output is correct
5 Correct 8 ms 15956 KB Output is correct
6 Correct 8 ms 15936 KB Output is correct
7 Correct 8 ms 15956 KB Output is correct
8 Correct 7 ms 15980 KB Output is correct
9 Correct 7 ms 15956 KB Output is correct
10 Correct 8 ms 15956 KB Output is correct
11 Correct 7 ms 15956 KB Output is correct
12 Correct 9 ms 15956 KB Output is correct
13 Correct 7 ms 16008 KB Output is correct
14 Correct 7 ms 15956 KB Output is correct
15 Correct 8 ms 15956 KB Output is correct
16 Correct 8 ms 15956 KB Output is correct
17 Correct 7 ms 15936 KB Output is correct
18 Correct 10 ms 15956 KB Output is correct
19 Correct 9 ms 15932 KB Output is correct
20 Correct 8 ms 15932 KB Output is correct
21 Correct 8 ms 15956 KB Output is correct
22 Correct 9 ms 15956 KB Output is correct
23 Correct 8 ms 15956 KB Output is correct
24 Correct 9 ms 15956 KB Output is correct
25 Correct 7 ms 15956 KB Output is correct
26 Correct 54 ms 29492 KB Output is correct
27 Correct 68 ms 29140 KB Output is correct
28 Correct 9 ms 16364 KB Output is correct
29 Correct 9 ms 15972 KB Output is correct
30 Correct 8 ms 15956 KB Output is correct
31 Correct 82 ms 29264 KB Output is correct
32 Correct 8 ms 16388 KB Output is correct
33 Correct 84 ms 36268 KB Output is correct
34 Correct 71 ms 29232 KB Output is correct
35 Correct 9 ms 16320 KB Output is correct
36 Correct 81 ms 29964 KB Output is correct
37 Correct 9 ms 16340 KB Output is correct
38 Correct 8 ms 16340 KB Output is correct
39 Correct 49 ms 29444 KB Output is correct
40 Correct 9 ms 16596 KB Output is correct
41 Correct 62 ms 29292 KB Output is correct
42 Correct 113 ms 31396 KB Output is correct
43 Correct 8 ms 15988 KB Output is correct
44 Correct 94 ms 36616 KB Output is correct
45 Correct 79 ms 32868 KB Output is correct
46 Correct 9 ms 16340 KB Output is correct
47 Correct 9 ms 16340 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 60 ms 29480 KB Output is correct
2 Correct 63 ms 34736 KB Output is correct
3 Correct 9 ms 16468 KB Output is correct
4 Correct 9 ms 16420 KB Output is correct
5 Correct 7 ms 15956 KB Output is correct
6 Correct 7 ms 15956 KB Output is correct
7 Correct 9 ms 16348 KB Output is correct
8 Correct 86 ms 31688 KB Output is correct
9 Correct 10 ms 16416 KB Output is correct
10 Correct 80 ms 29620 KB Output is correct
11 Correct 8 ms 15956 KB Output is correct
12 Correct 88 ms 29672 KB Output is correct
13 Correct 84 ms 31672 KB Output is correct
14 Correct 88 ms 34520 KB Output is correct
15 Correct 51 ms 29424 KB Output is correct
16 Correct 9 ms 16468 KB Output is correct
17 Correct 8 ms 15956 KB Output is correct
18 Correct 63 ms 34240 KB Output is correct
19 Correct 106 ms 38408 KB Output is correct
20 Correct 9 ms 16468 KB Output is correct
21 Correct 8 ms 15956 KB Output is correct
22 Correct 71 ms 31980 KB Output is correct
23 Correct 11 ms 16340 KB Output is correct
24 Correct 81 ms 30088 KB Output is correct
25 Correct 110 ms 37048 KB Output is correct
26 Correct 9 ms 16516 KB Output is correct
27 Correct 9 ms 16596 KB Output is correct
28 Correct 9 ms 16468 KB Output is correct
29 Correct 9 ms 16340 KB Output is correct
30 Correct 9 ms 16468 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 7 ms 15956 KB Output is correct
2 Correct 8 ms 15920 KB Output is correct
3 Correct 8 ms 15936 KB Output is correct
4 Correct 7 ms 15956 KB Output is correct
5 Correct 8 ms 15956 KB Output is correct
6 Correct 8 ms 15936 KB Output is correct
7 Correct 8 ms 15956 KB Output is correct
8 Correct 7 ms 15980 KB Output is correct
9 Correct 7 ms 15956 KB Output is correct
10 Correct 8 ms 15956 KB Output is correct
11 Correct 7 ms 15956 KB Output is correct
12 Correct 9 ms 15956 KB Output is correct
13 Correct 7 ms 16008 KB Output is correct
14 Correct 7 ms 15956 KB Output is correct
15 Correct 8 ms 15956 KB Output is correct
16 Correct 8 ms 15956 KB Output is correct
17 Correct 7 ms 15936 KB Output is correct
18 Correct 10 ms 15956 KB Output is correct
19 Correct 9 ms 15932 KB Output is correct
20 Correct 8 ms 15932 KB Output is correct
21 Correct 8 ms 15956 KB Output is correct
22 Correct 9 ms 15956 KB Output is correct
23 Correct 8 ms 15956 KB Output is correct
24 Correct 9 ms 15956 KB Output is correct
25 Correct 7 ms 15956 KB Output is correct
26 Correct 9 ms 16468 KB Output is correct
27 Correct 10 ms 16340 KB Output is correct
28 Correct 9 ms 16596 KB Output is correct
29 Correct 9 ms 16468 KB Output is correct
30 Correct 9 ms 16340 KB Output is correct
31 Correct 7 ms 15992 KB Output is correct
32 Correct 9 ms 16704 KB Output is correct
33 Correct 7 ms 15956 KB Output is correct
34 Correct 9 ms 16340 KB Output is correct
35 Correct 9 ms 16468 KB Output is correct
36 Correct 9 ms 16340 KB Output is correct
37 Correct 10 ms 16468 KB Output is correct
38 Correct 7 ms 15964 KB Output is correct
39 Correct 10 ms 16468 KB Output is correct
40 Correct 9 ms 16396 KB Output is correct
41 Correct 9 ms 16340 KB Output is correct
42 Correct 10 ms 16468 KB Output is correct
43 Correct 9 ms 16536 KB Output is correct
44 Correct 8 ms 15956 KB Output is correct
45 Correct 9 ms 16596 KB Output is correct
46 Correct 8 ms 16468 KB Output is correct
47 Correct 7 ms 15956 KB Output is correct
48 Correct 9 ms 16452 KB Output is correct
49 Correct 9 ms 16468 KB Output is correct
50 Correct 9 ms 16592 KB Output is correct
51 Correct 9 ms 16340 KB Output is correct
52 Correct 9 ms 16412 KB Output is correct
53 Correct 9 ms 16424 KB Output is correct
54 Correct 9 ms 16340 KB Output is correct
55 Correct 7 ms 15956 KB Output is correct
56 Correct 54 ms 29492 KB Output is correct
57 Correct 68 ms 29140 KB Output is correct
58 Correct 9 ms 16364 KB Output is correct
59 Correct 9 ms 15972 KB Output is correct
60 Correct 8 ms 15956 KB Output is correct
61 Correct 82 ms 29264 KB Output is correct
62 Correct 8 ms 16388 KB Output is correct
63 Correct 84 ms 36268 KB Output is correct
64 Correct 71 ms 29232 KB Output is correct
65 Correct 9 ms 16320 KB Output is correct
66 Correct 81 ms 29964 KB Output is correct
67 Correct 9 ms 16340 KB Output is correct
68 Correct 8 ms 16340 KB Output is correct
69 Correct 49 ms 29444 KB Output is correct
70 Correct 9 ms 16596 KB Output is correct
71 Correct 62 ms 29292 KB Output is correct
72 Correct 113 ms 31396 KB Output is correct
73 Correct 8 ms 15988 KB Output is correct
74 Correct 94 ms 36616 KB Output is correct
75 Correct 79 ms 32868 KB Output is correct
76 Correct 9 ms 16340 KB Output is correct
77 Correct 9 ms 16340 KB Output is correct
78 Correct 60 ms 29480 KB Output is correct
79 Correct 63 ms 34736 KB Output is correct
80 Correct 9 ms 16468 KB Output is correct
81 Correct 9 ms 16420 KB Output is correct
82 Correct 7 ms 15956 KB Output is correct
83 Correct 7 ms 15956 KB Output is correct
84 Correct 9 ms 16348 KB Output is correct
85 Correct 86 ms 31688 KB Output is correct
86 Correct 10 ms 16416 KB Output is correct
87 Correct 80 ms 29620 KB Output is correct
88 Correct 8 ms 15956 KB Output is correct
89 Correct 88 ms 29672 KB Output is correct
90 Correct 84 ms 31672 KB Output is correct
91 Correct 88 ms 34520 KB Output is correct
92 Correct 51 ms 29424 KB Output is correct
93 Correct 9 ms 16468 KB Output is correct
94 Correct 8 ms 15956 KB Output is correct
95 Correct 63 ms 34240 KB Output is correct
96 Correct 106 ms 38408 KB Output is correct
97 Correct 9 ms 16468 KB Output is correct
98 Correct 8 ms 15956 KB Output is correct
99 Correct 71 ms 31980 KB Output is correct
100 Correct 11 ms 16340 KB Output is correct
101 Correct 81 ms 30088 KB Output is correct
102 Correct 110 ms 37048 KB Output is correct
103 Correct 9 ms 16516 KB Output is correct
104 Correct 9 ms 16596 KB Output is correct
105 Correct 9 ms 16468 KB Output is correct
106 Correct 9 ms 16340 KB Output is correct
107 Correct 9 ms 16468 KB Output is correct
108 Correct 440 ms 93688 KB Output is correct
109 Correct 841 ms 99240 KB Output is correct
110 Correct 767 ms 110472 KB Output is correct
111 Correct 982 ms 131420 KB Output is correct
112 Correct 856 ms 121740 KB Output is correct
113 Correct 484 ms 107064 KB Output is correct
114 Correct 652 ms 81248 KB Output is correct
115 Correct 526 ms 81324 KB Output is correct
116 Correct 677 ms 87980 KB Output is correct
117 Correct 780 ms 108472 KB Output is correct
118 Correct 673 ms 82616 KB Output is correct
119 Correct 688 ms 108408 KB Output is correct
120 Correct 887 ms 122392 KB Output is correct
121 Correct 696 ms 108432 KB Output is correct
122 Correct 812 ms 97480 KB Output is correct
123 Correct 447 ms 109892 KB Output is correct
124 Correct 935 ms 106524 KB Output is correct