Submission #828661

# Submission time Handle Problem Language Result Execution time Memory
828661 2023-08-17T13:17:28 Z Ronin13 Mergers (JOI19_mergers) C++17
100 / 100
1270 ms 141456 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 9 ms 15956 KB Output is correct
2 Correct 7 ms 15956 KB Output is correct
3 Correct 7 ms 15928 KB Output is correct
4 Correct 9 ms 15976 KB Output is correct
5 Correct 9 ms 15956 KB Output is correct
6 Correct 8 ms 15992 KB Output is correct
7 Correct 8 ms 15956 KB Output is correct
8 Correct 6 ms 15956 KB Output is correct
9 Correct 6 ms 15876 KB Output is correct
10 Correct 6 ms 15956 KB Output is correct
11 Correct 9 ms 15956 KB Output is correct
12 Correct 8 ms 15916 KB Output is correct
13 Correct 8 ms 15956 KB Output is correct
14 Correct 7 ms 15956 KB Output is correct
15 Correct 7 ms 15956 KB Output is correct
16 Correct 7 ms 15956 KB Output is correct
17 Correct 8 ms 15992 KB Output is correct
18 Correct 7 ms 15956 KB Output is correct
19 Correct 8 ms 15956 KB Output is correct
20 Correct 7 ms 15964 KB Output is correct
21 Correct 7 ms 15992 KB Output is correct
22 Correct 8 ms 15996 KB Output is correct
23 Correct 8 ms 15956 KB Output is correct
24 Correct 8 ms 15956 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 9 ms 15956 KB Output is correct
2 Correct 7 ms 15956 KB Output is correct
3 Correct 7 ms 15928 KB Output is correct
4 Correct 9 ms 15976 KB Output is correct
5 Correct 9 ms 15956 KB Output is correct
6 Correct 8 ms 15992 KB Output is correct
7 Correct 8 ms 15956 KB Output is correct
8 Correct 6 ms 15956 KB Output is correct
9 Correct 6 ms 15876 KB Output is correct
10 Correct 6 ms 15956 KB Output is correct
11 Correct 9 ms 15956 KB Output is correct
12 Correct 8 ms 15916 KB Output is correct
13 Correct 8 ms 15956 KB Output is correct
14 Correct 7 ms 15956 KB Output is correct
15 Correct 7 ms 15956 KB Output is correct
16 Correct 7 ms 15956 KB Output is correct
17 Correct 8 ms 15992 KB Output is correct
18 Correct 7 ms 15956 KB Output is correct
19 Correct 8 ms 15956 KB Output is correct
20 Correct 7 ms 15964 KB Output is correct
21 Correct 7 ms 15992 KB Output is correct
22 Correct 8 ms 15996 KB Output is correct
23 Correct 8 ms 15956 KB Output is correct
24 Correct 8 ms 15956 KB Output is correct
25 Correct 8 ms 15956 KB Output is correct
26 Correct 10 ms 16524 KB Output is correct
27 Correct 9 ms 16436 KB Output is correct
28 Correct 12 ms 16644 KB Output is correct
29 Correct 12 ms 16552 KB Output is correct
30 Correct 10 ms 16348 KB Output is correct
31 Correct 9 ms 15992 KB Output is correct
32 Correct 11 ms 16672 KB Output is correct
33 Correct 7 ms 15956 KB Output is correct
34 Correct 10 ms 16464 KB Output is correct
35 Correct 10 ms 16596 KB Output is correct
36 Correct 14 ms 16388 KB Output is correct
37 Correct 11 ms 16512 KB Output is correct
38 Correct 8 ms 15956 KB Output is correct
39 Correct 12 ms 16468 KB Output is correct
40 Correct 11 ms 16384 KB Output is correct
41 Correct 12 ms 16464 KB Output is correct
42 Correct 10 ms 16428 KB Output is correct
43 Correct 11 ms 16548 KB Output is correct
44 Correct 8 ms 15956 KB Output is correct
45 Correct 14 ms 16648 KB Output is correct
46 Correct 13 ms 16516 KB Output is correct
47 Correct 9 ms 15912 KB Output is correct
48 Correct 10 ms 16468 KB Output is correct
49 Correct 11 ms 16596 KB Output is correct
50 Correct 11 ms 16596 KB Output is correct
51 Correct 12 ms 16468 KB Output is correct
52 Correct 11 ms 16412 KB Output is correct
53 Correct 11 ms 16468 KB Output is correct
54 Correct 13 ms 16332 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 9 ms 15956 KB Output is correct
2 Correct 7 ms 15956 KB Output is correct
3 Correct 7 ms 15928 KB Output is correct
4 Correct 9 ms 15976 KB Output is correct
5 Correct 9 ms 15956 KB Output is correct
6 Correct 8 ms 15992 KB Output is correct
7 Correct 8 ms 15956 KB Output is correct
8 Correct 6 ms 15956 KB Output is correct
9 Correct 6 ms 15876 KB Output is correct
10 Correct 6 ms 15956 KB Output is correct
11 Correct 9 ms 15956 KB Output is correct
12 Correct 8 ms 15916 KB Output is correct
13 Correct 8 ms 15956 KB Output is correct
14 Correct 7 ms 15956 KB Output is correct
15 Correct 7 ms 15956 KB Output is correct
16 Correct 7 ms 15956 KB Output is correct
17 Correct 8 ms 15992 KB Output is correct
18 Correct 7 ms 15956 KB Output is correct
19 Correct 8 ms 15956 KB Output is correct
20 Correct 7 ms 15964 KB Output is correct
21 Correct 7 ms 15992 KB Output is correct
22 Correct 8 ms 15996 KB Output is correct
23 Correct 8 ms 15956 KB Output is correct
24 Correct 8 ms 15956 KB Output is correct
25 Correct 7 ms 15956 KB Output is correct
26 Correct 106 ms 30912 KB Output is correct
27 Correct 131 ms 30560 KB Output is correct
28 Correct 10 ms 16468 KB Output is correct
29 Correct 8 ms 15988 KB Output is correct
30 Correct 9 ms 15952 KB Output is correct
31 Correct 152 ms 30668 KB Output is correct
32 Correct 11 ms 16468 KB Output is correct
33 Correct 140 ms 37620 KB Output is correct
34 Correct 132 ms 30540 KB Output is correct
35 Correct 11 ms 16336 KB Output is correct
36 Correct 159 ms 31432 KB Output is correct
37 Correct 10 ms 16392 KB Output is correct
38 Correct 10 ms 16468 KB Output is correct
39 Correct 113 ms 30812 KB Output is correct
40 Correct 12 ms 16560 KB Output is correct
41 Correct 118 ms 30488 KB Output is correct
42 Correct 156 ms 32856 KB Output is correct
43 Correct 8 ms 15956 KB Output is correct
44 Correct 175 ms 37936 KB Output is correct
45 Correct 136 ms 34300 KB Output is correct
46 Correct 10 ms 16392 KB Output is correct
47 Correct 11 ms 16384 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 92 ms 29472 KB Output is correct
2 Correct 131 ms 34708 KB Output is correct
3 Correct 12 ms 16536 KB Output is correct
4 Correct 12 ms 16468 KB Output is correct
5 Correct 8 ms 16084 KB Output is correct
6 Correct 9 ms 15956 KB Output is correct
7 Correct 10 ms 16380 KB Output is correct
8 Correct 150 ms 33424 KB Output is correct
9 Correct 14 ms 16500 KB Output is correct
10 Correct 129 ms 31144 KB Output is correct
11 Correct 8 ms 15988 KB Output is correct
12 Correct 139 ms 31068 KB Output is correct
13 Correct 139 ms 33340 KB Output is correct
14 Correct 163 ms 36208 KB Output is correct
15 Correct 119 ms 30840 KB Output is correct
16 Correct 14 ms 16508 KB Output is correct
17 Correct 9 ms 15992 KB Output is correct
18 Correct 126 ms 35940 KB Output is correct
19 Correct 196 ms 40140 KB Output is correct
20 Correct 14 ms 16468 KB Output is correct
21 Correct 8 ms 15988 KB Output is correct
22 Correct 131 ms 33632 KB Output is correct
23 Correct 12 ms 16388 KB Output is correct
24 Correct 128 ms 31680 KB Output is correct
25 Correct 172 ms 38764 KB Output is correct
26 Correct 11 ms 16608 KB Output is correct
27 Correct 12 ms 16608 KB Output is correct
28 Correct 10 ms 16380 KB Output is correct
29 Correct 12 ms 16400 KB Output is correct
30 Correct 11 ms 16544 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 9 ms 15956 KB Output is correct
2 Correct 7 ms 15956 KB Output is correct
3 Correct 7 ms 15928 KB Output is correct
4 Correct 9 ms 15976 KB Output is correct
5 Correct 9 ms 15956 KB Output is correct
6 Correct 8 ms 15992 KB Output is correct
7 Correct 8 ms 15956 KB Output is correct
8 Correct 6 ms 15956 KB Output is correct
9 Correct 6 ms 15876 KB Output is correct
10 Correct 6 ms 15956 KB Output is correct
11 Correct 9 ms 15956 KB Output is correct
12 Correct 8 ms 15916 KB Output is correct
13 Correct 8 ms 15956 KB Output is correct
14 Correct 7 ms 15956 KB Output is correct
15 Correct 7 ms 15956 KB Output is correct
16 Correct 7 ms 15956 KB Output is correct
17 Correct 8 ms 15992 KB Output is correct
18 Correct 7 ms 15956 KB Output is correct
19 Correct 8 ms 15956 KB Output is correct
20 Correct 7 ms 15964 KB Output is correct
21 Correct 7 ms 15992 KB Output is correct
22 Correct 8 ms 15996 KB Output is correct
23 Correct 8 ms 15956 KB Output is correct
24 Correct 8 ms 15956 KB Output is correct
25 Correct 8 ms 15956 KB Output is correct
26 Correct 10 ms 16524 KB Output is correct
27 Correct 9 ms 16436 KB Output is correct
28 Correct 12 ms 16644 KB Output is correct
29 Correct 12 ms 16552 KB Output is correct
30 Correct 10 ms 16348 KB Output is correct
31 Correct 9 ms 15992 KB Output is correct
32 Correct 11 ms 16672 KB Output is correct
33 Correct 7 ms 15956 KB Output is correct
34 Correct 10 ms 16464 KB Output is correct
35 Correct 10 ms 16596 KB Output is correct
36 Correct 14 ms 16388 KB Output is correct
37 Correct 11 ms 16512 KB Output is correct
38 Correct 8 ms 15956 KB Output is correct
39 Correct 12 ms 16468 KB Output is correct
40 Correct 11 ms 16384 KB Output is correct
41 Correct 12 ms 16464 KB Output is correct
42 Correct 10 ms 16428 KB Output is correct
43 Correct 11 ms 16548 KB Output is correct
44 Correct 8 ms 15956 KB Output is correct
45 Correct 14 ms 16648 KB Output is correct
46 Correct 13 ms 16516 KB Output is correct
47 Correct 9 ms 15912 KB Output is correct
48 Correct 10 ms 16468 KB Output is correct
49 Correct 11 ms 16596 KB Output is correct
50 Correct 11 ms 16596 KB Output is correct
51 Correct 12 ms 16468 KB Output is correct
52 Correct 11 ms 16412 KB Output is correct
53 Correct 11 ms 16468 KB Output is correct
54 Correct 13 ms 16332 KB Output is correct
55 Correct 7 ms 15956 KB Output is correct
56 Correct 106 ms 30912 KB Output is correct
57 Correct 131 ms 30560 KB Output is correct
58 Correct 10 ms 16468 KB Output is correct
59 Correct 8 ms 15988 KB Output is correct
60 Correct 9 ms 15952 KB Output is correct
61 Correct 152 ms 30668 KB Output is correct
62 Correct 11 ms 16468 KB Output is correct
63 Correct 140 ms 37620 KB Output is correct
64 Correct 132 ms 30540 KB Output is correct
65 Correct 11 ms 16336 KB Output is correct
66 Correct 159 ms 31432 KB Output is correct
67 Correct 10 ms 16392 KB Output is correct
68 Correct 10 ms 16468 KB Output is correct
69 Correct 113 ms 30812 KB Output is correct
70 Correct 12 ms 16560 KB Output is correct
71 Correct 118 ms 30488 KB Output is correct
72 Correct 156 ms 32856 KB Output is correct
73 Correct 8 ms 15956 KB Output is correct
74 Correct 175 ms 37936 KB Output is correct
75 Correct 136 ms 34300 KB Output is correct
76 Correct 10 ms 16392 KB Output is correct
77 Correct 11 ms 16384 KB Output is correct
78 Correct 92 ms 29472 KB Output is correct
79 Correct 131 ms 34708 KB Output is correct
80 Correct 12 ms 16536 KB Output is correct
81 Correct 12 ms 16468 KB Output is correct
82 Correct 8 ms 16084 KB Output is correct
83 Correct 9 ms 15956 KB Output is correct
84 Correct 10 ms 16380 KB Output is correct
85 Correct 150 ms 33424 KB Output is correct
86 Correct 14 ms 16500 KB Output is correct
87 Correct 129 ms 31144 KB Output is correct
88 Correct 8 ms 15988 KB Output is correct
89 Correct 139 ms 31068 KB Output is correct
90 Correct 139 ms 33340 KB Output is correct
91 Correct 163 ms 36208 KB Output is correct
92 Correct 119 ms 30840 KB Output is correct
93 Correct 14 ms 16508 KB Output is correct
94 Correct 9 ms 15992 KB Output is correct
95 Correct 126 ms 35940 KB Output is correct
96 Correct 196 ms 40140 KB Output is correct
97 Correct 14 ms 16468 KB Output is correct
98 Correct 8 ms 15988 KB Output is correct
99 Correct 131 ms 33632 KB Output is correct
100 Correct 12 ms 16388 KB Output is correct
101 Correct 128 ms 31680 KB Output is correct
102 Correct 172 ms 38764 KB Output is correct
103 Correct 11 ms 16608 KB Output is correct
104 Correct 12 ms 16608 KB Output is correct
105 Correct 10 ms 16380 KB Output is correct
106 Correct 12 ms 16400 KB Output is correct
107 Correct 11 ms 16544 KB Output is correct
108 Correct 781 ms 103544 KB Output is correct
109 Correct 1270 ms 106820 KB Output is correct
110 Correct 1083 ms 118024 KB Output is correct
111 Correct 1206 ms 141456 KB Output is correct
112 Correct 1150 ms 131684 KB Output is correct
113 Correct 720 ms 117192 KB Output is correct
114 Correct 774 ms 88816 KB Output is correct
115 Correct 751 ms 88924 KB Output is correct
116 Correct 932 ms 97464 KB Output is correct
117 Correct 911 ms 118344 KB Output is correct
118 Correct 791 ms 90860 KB Output is correct
119 Correct 938 ms 118348 KB Output is correct
120 Correct 1117 ms 132340 KB Output is correct
121 Correct 932 ms 118332 KB Output is correct
122 Correct 1000 ms 107372 KB Output is correct
123 Correct 661 ms 119824 KB Output is correct
124 Correct 1160 ms 116152 KB Output is correct