Submission #441648

# Submission time Handle Problem Language Result Execution time Memory
441648 2021-07-05T16:36:20 Z JovanB Mergers (JOI19_mergers) C++17
100 / 100
1314 ms 132312 KB
#include <bits/stdc++.h>
using namespace std;

using ll = long long;
using ld = long double;

const int MAXN = 500000;
const int MXLOG = 19;

int in[MAXN+5];
int out[MAXN+5];
int svih[MAXN+5];
int kome[MAXN+5];
vector <int> graf[MAXN+5];
int par[MAXN+5][MXLOG+1];

int tjm;

int depth[MAXN+5];

void dfs_calc(int v, int p){
    in[v] = ++tjm;
    par[v][0] = p;
    depth[v] = depth[p] + 1;
    for(int j=1; j<=MXLOG; j++) par[v][j] = par[par[v][j-1]][j-1];
    for(auto c : graf[v]) if(c != p) dfs_calc(c, v);
    out[v] = tjm;
}

bool is_parent(int a, int b){
    return (a == 0) || (in[a] <= in[b] && out[b] <= out[a]);
}

int lca(int a, int b){
    if(is_parent(a, b)) return a;
    for(int j=MXLOG; j>=0; j--) if(!is_parent(par[a][j], b)) a = par[a][j];
    return par[a][0];
}

bool deli[MAXN+5];

int dfs_traverse(int v, int p){
    int mnh = depth[svih[kome[v]]];
    for(auto c : graf[v]) if(c != p) mnh = min(mnh, dfs_traverse(c, v));
    if(mnh >= depth[v]) deli[v] = 1;
    return mnh;
}

vector <int> dgraf[MAXN+5];

void dfs_final(int v, int p, int lst){
    int sd = lst;
    if(deli[v]){
        if(lst){
            dgraf[lst].push_back(v);
            dgraf[v].push_back(lst);
        }
        sd = v;
    }
    for(auto c : graf[v]) if(c != p) dfs_final(c, v, sd);
}

int main(){
    ios_base::sync_with_stdio(false), cin.tie(0);
    cout.precision(10);
    cout << fixed;

    int n, k;
    cin >> n >> k;
    for(int i=1; i<n; i++){
        int a, b;
        cin >> a >> b;
        graf[a].push_back(b);
        graf[b].push_back(a);
    }
    dfs_calc(1, 0);
    for(int i=1; i<=n; i++){
        cin >> kome[i];
        if(!svih[kome[i]]) svih[kome[i]] = i;
        else svih[kome[i]] = lca(svih[kome[i]], i);
    }
    dfs_traverse(1, 0);
    dfs_final(1, 0, 0);
    int leaves = 0;
    for(int i=1; i<=n; i++) leaves += (dgraf[i].size() == 1);
    cout << (leaves+1)/2 << "\n";
    return 0;
}
# Verdict Execution time Memory Grader output
1 Correct 16 ms 23804 KB Output is correct
2 Correct 15 ms 23756 KB Output is correct
3 Correct 15 ms 23776 KB Output is correct
4 Correct 14 ms 23756 KB Output is correct
5 Correct 15 ms 23844 KB Output is correct
6 Correct 14 ms 23848 KB Output is correct
7 Correct 15 ms 23816 KB Output is correct
8 Correct 15 ms 23756 KB Output is correct
9 Correct 15 ms 23884 KB Output is correct
10 Correct 17 ms 23812 KB Output is correct
11 Correct 16 ms 23808 KB Output is correct
12 Correct 15 ms 23816 KB Output is correct
13 Correct 15 ms 23848 KB Output is correct
14 Correct 16 ms 23760 KB Output is correct
15 Correct 15 ms 23756 KB Output is correct
16 Correct 16 ms 23736 KB Output is correct
17 Correct 15 ms 23828 KB Output is correct
18 Correct 15 ms 23756 KB Output is correct
19 Correct 15 ms 23768 KB Output is correct
20 Correct 16 ms 23756 KB Output is correct
21 Correct 15 ms 23860 KB Output is correct
22 Correct 15 ms 23756 KB Output is correct
23 Correct 15 ms 23756 KB Output is correct
24 Correct 16 ms 23820 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 16 ms 23804 KB Output is correct
2 Correct 15 ms 23756 KB Output is correct
3 Correct 15 ms 23776 KB Output is correct
4 Correct 14 ms 23756 KB Output is correct
5 Correct 15 ms 23844 KB Output is correct
6 Correct 14 ms 23848 KB Output is correct
7 Correct 15 ms 23816 KB Output is correct
8 Correct 15 ms 23756 KB Output is correct
9 Correct 15 ms 23884 KB Output is correct
10 Correct 17 ms 23812 KB Output is correct
11 Correct 16 ms 23808 KB Output is correct
12 Correct 15 ms 23816 KB Output is correct
13 Correct 15 ms 23848 KB Output is correct
14 Correct 16 ms 23760 KB Output is correct
15 Correct 15 ms 23756 KB Output is correct
16 Correct 16 ms 23736 KB Output is correct
17 Correct 15 ms 23828 KB Output is correct
18 Correct 15 ms 23756 KB Output is correct
19 Correct 15 ms 23768 KB Output is correct
20 Correct 16 ms 23756 KB Output is correct
21 Correct 15 ms 23860 KB Output is correct
22 Correct 15 ms 23756 KB Output is correct
23 Correct 15 ms 23756 KB Output is correct
24 Correct 16 ms 23820 KB Output is correct
25 Correct 14 ms 23756 KB Output is correct
26 Correct 18 ms 24336 KB Output is correct
27 Correct 18 ms 24268 KB Output is correct
28 Correct 18 ms 24440 KB Output is correct
29 Correct 18 ms 24376 KB Output is correct
30 Correct 18 ms 24268 KB Output is correct
31 Correct 16 ms 23756 KB Output is correct
32 Correct 18 ms 24360 KB Output is correct
33 Correct 16 ms 23756 KB Output is correct
34 Correct 18 ms 24160 KB Output is correct
35 Correct 18 ms 24396 KB Output is correct
36 Correct 18 ms 24204 KB Output is correct
37 Correct 19 ms 24204 KB Output is correct
38 Correct 15 ms 23756 KB Output is correct
39 Correct 18 ms 24208 KB Output is correct
40 Correct 17 ms 24208 KB Output is correct
41 Correct 17 ms 24268 KB Output is correct
42 Correct 17 ms 24308 KB Output is correct
43 Correct 18 ms 24336 KB Output is correct
44 Correct 16 ms 23756 KB Output is correct
45 Correct 18 ms 24208 KB Output is correct
46 Correct 18 ms 24376 KB Output is correct
47 Correct 16 ms 23848 KB Output is correct
48 Correct 18 ms 24268 KB Output is correct
49 Correct 20 ms 24320 KB Output is correct
50 Correct 19 ms 24396 KB Output is correct
51 Correct 18 ms 24260 KB Output is correct
52 Correct 17 ms 24268 KB Output is correct
53 Correct 18 ms 24300 KB Output is correct
54 Correct 19 ms 24176 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 16 ms 23804 KB Output is correct
2 Correct 15 ms 23756 KB Output is correct
3 Correct 15 ms 23776 KB Output is correct
4 Correct 14 ms 23756 KB Output is correct
5 Correct 15 ms 23844 KB Output is correct
6 Correct 14 ms 23848 KB Output is correct
7 Correct 15 ms 23816 KB Output is correct
8 Correct 15 ms 23756 KB Output is correct
9 Correct 15 ms 23884 KB Output is correct
10 Correct 17 ms 23812 KB Output is correct
11 Correct 16 ms 23808 KB Output is correct
12 Correct 15 ms 23816 KB Output is correct
13 Correct 15 ms 23848 KB Output is correct
14 Correct 16 ms 23760 KB Output is correct
15 Correct 15 ms 23756 KB Output is correct
16 Correct 16 ms 23736 KB Output is correct
17 Correct 15 ms 23828 KB Output is correct
18 Correct 15 ms 23756 KB Output is correct
19 Correct 15 ms 23768 KB Output is correct
20 Correct 16 ms 23756 KB Output is correct
21 Correct 15 ms 23860 KB Output is correct
22 Correct 15 ms 23756 KB Output is correct
23 Correct 15 ms 23756 KB Output is correct
24 Correct 16 ms 23820 KB Output is correct
25 Correct 16 ms 23820 KB Output is correct
26 Correct 87 ms 38204 KB Output is correct
27 Correct 120 ms 37816 KB Output is correct
28 Correct 17 ms 24232 KB Output is correct
29 Correct 16 ms 23812 KB Output is correct
30 Correct 16 ms 23784 KB Output is correct
31 Correct 119 ms 37960 KB Output is correct
32 Correct 18 ms 24204 KB Output is correct
33 Correct 154 ms 43664 KB Output is correct
34 Correct 112 ms 37856 KB Output is correct
35 Correct 18 ms 24268 KB Output is correct
36 Correct 144 ms 38492 KB Output is correct
37 Correct 18 ms 24268 KB Output is correct
38 Correct 19 ms 24140 KB Output is correct
39 Correct 85 ms 38076 KB Output is correct
40 Correct 17 ms 24396 KB Output is correct
41 Correct 115 ms 37700 KB Output is correct
42 Correct 158 ms 39748 KB Output is correct
43 Correct 16 ms 23780 KB Output is correct
44 Correct 193 ms 43716 KB Output is correct
45 Correct 175 ms 40844 KB Output is correct
46 Correct 18 ms 24268 KB Output is correct
47 Correct 17 ms 24196 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 79 ms 38076 KB Output is correct
2 Correct 97 ms 42468 KB Output is correct
3 Correct 18 ms 24296 KB Output is correct
4 Correct 18 ms 24232 KB Output is correct
5 Correct 16 ms 23816 KB Output is correct
6 Correct 16 ms 23756 KB Output is correct
7 Correct 17 ms 24208 KB Output is correct
8 Correct 131 ms 38852 KB Output is correct
9 Correct 18 ms 24204 KB Output is correct
10 Correct 121 ms 38084 KB Output is correct
11 Correct 16 ms 23820 KB Output is correct
12 Correct 146 ms 38120 KB Output is correct
13 Correct 132 ms 38984 KB Output is correct
14 Correct 140 ms 41856 KB Output is correct
15 Correct 80 ms 38076 KB Output is correct
16 Correct 16 ms 24276 KB Output is correct
17 Correct 16 ms 23756 KB Output is correct
18 Correct 99 ms 41760 KB Output is correct
19 Correct 164 ms 44868 KB Output is correct
20 Correct 17 ms 24204 KB Output is correct
21 Correct 16 ms 23860 KB Output is correct
22 Correct 96 ms 39212 KB Output is correct
23 Correct 16 ms 24268 KB Output is correct
24 Correct 131 ms 38448 KB Output is correct
25 Correct 162 ms 43808 KB Output is correct
26 Correct 19 ms 24396 KB Output is correct
27 Correct 17 ms 24424 KB Output is correct
28 Correct 17 ms 24264 KB Output is correct
29 Correct 16 ms 24216 KB Output is correct
30 Correct 17 ms 24212 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 16 ms 23804 KB Output is correct
2 Correct 15 ms 23756 KB Output is correct
3 Correct 15 ms 23776 KB Output is correct
4 Correct 14 ms 23756 KB Output is correct
5 Correct 15 ms 23844 KB Output is correct
6 Correct 14 ms 23848 KB Output is correct
7 Correct 15 ms 23816 KB Output is correct
8 Correct 15 ms 23756 KB Output is correct
9 Correct 15 ms 23884 KB Output is correct
10 Correct 17 ms 23812 KB Output is correct
11 Correct 16 ms 23808 KB Output is correct
12 Correct 15 ms 23816 KB Output is correct
13 Correct 15 ms 23848 KB Output is correct
14 Correct 16 ms 23760 KB Output is correct
15 Correct 15 ms 23756 KB Output is correct
16 Correct 16 ms 23736 KB Output is correct
17 Correct 15 ms 23828 KB Output is correct
18 Correct 15 ms 23756 KB Output is correct
19 Correct 15 ms 23768 KB Output is correct
20 Correct 16 ms 23756 KB Output is correct
21 Correct 15 ms 23860 KB Output is correct
22 Correct 15 ms 23756 KB Output is correct
23 Correct 15 ms 23756 KB Output is correct
24 Correct 16 ms 23820 KB Output is correct
25 Correct 14 ms 23756 KB Output is correct
26 Correct 18 ms 24336 KB Output is correct
27 Correct 18 ms 24268 KB Output is correct
28 Correct 18 ms 24440 KB Output is correct
29 Correct 18 ms 24376 KB Output is correct
30 Correct 18 ms 24268 KB Output is correct
31 Correct 16 ms 23756 KB Output is correct
32 Correct 18 ms 24360 KB Output is correct
33 Correct 16 ms 23756 KB Output is correct
34 Correct 18 ms 24160 KB Output is correct
35 Correct 18 ms 24396 KB Output is correct
36 Correct 18 ms 24204 KB Output is correct
37 Correct 19 ms 24204 KB Output is correct
38 Correct 15 ms 23756 KB Output is correct
39 Correct 18 ms 24208 KB Output is correct
40 Correct 17 ms 24208 KB Output is correct
41 Correct 17 ms 24268 KB Output is correct
42 Correct 17 ms 24308 KB Output is correct
43 Correct 18 ms 24336 KB Output is correct
44 Correct 16 ms 23756 KB Output is correct
45 Correct 18 ms 24208 KB Output is correct
46 Correct 18 ms 24376 KB Output is correct
47 Correct 16 ms 23848 KB Output is correct
48 Correct 18 ms 24268 KB Output is correct
49 Correct 20 ms 24320 KB Output is correct
50 Correct 19 ms 24396 KB Output is correct
51 Correct 18 ms 24260 KB Output is correct
52 Correct 17 ms 24268 KB Output is correct
53 Correct 18 ms 24300 KB Output is correct
54 Correct 19 ms 24176 KB Output is correct
55 Correct 16 ms 23820 KB Output is correct
56 Correct 87 ms 38204 KB Output is correct
57 Correct 120 ms 37816 KB Output is correct
58 Correct 17 ms 24232 KB Output is correct
59 Correct 16 ms 23812 KB Output is correct
60 Correct 16 ms 23784 KB Output is correct
61 Correct 119 ms 37960 KB Output is correct
62 Correct 18 ms 24204 KB Output is correct
63 Correct 154 ms 43664 KB Output is correct
64 Correct 112 ms 37856 KB Output is correct
65 Correct 18 ms 24268 KB Output is correct
66 Correct 144 ms 38492 KB Output is correct
67 Correct 18 ms 24268 KB Output is correct
68 Correct 19 ms 24140 KB Output is correct
69 Correct 85 ms 38076 KB Output is correct
70 Correct 17 ms 24396 KB Output is correct
71 Correct 115 ms 37700 KB Output is correct
72 Correct 158 ms 39748 KB Output is correct
73 Correct 16 ms 23780 KB Output is correct
74 Correct 193 ms 43716 KB Output is correct
75 Correct 175 ms 40844 KB Output is correct
76 Correct 18 ms 24268 KB Output is correct
77 Correct 17 ms 24196 KB Output is correct
78 Correct 79 ms 38076 KB Output is correct
79 Correct 97 ms 42468 KB Output is correct
80 Correct 18 ms 24296 KB Output is correct
81 Correct 18 ms 24232 KB Output is correct
82 Correct 16 ms 23816 KB Output is correct
83 Correct 16 ms 23756 KB Output is correct
84 Correct 17 ms 24208 KB Output is correct
85 Correct 131 ms 38852 KB Output is correct
86 Correct 18 ms 24204 KB Output is correct
87 Correct 121 ms 38084 KB Output is correct
88 Correct 16 ms 23820 KB Output is correct
89 Correct 146 ms 38120 KB Output is correct
90 Correct 132 ms 38984 KB Output is correct
91 Correct 140 ms 41856 KB Output is correct
92 Correct 80 ms 38076 KB Output is correct
93 Correct 16 ms 24276 KB Output is correct
94 Correct 16 ms 23756 KB Output is correct
95 Correct 99 ms 41760 KB Output is correct
96 Correct 164 ms 44868 KB Output is correct
97 Correct 17 ms 24204 KB Output is correct
98 Correct 16 ms 23860 KB Output is correct
99 Correct 96 ms 39212 KB Output is correct
100 Correct 16 ms 24268 KB Output is correct
101 Correct 131 ms 38448 KB Output is correct
102 Correct 162 ms 43808 KB Output is correct
103 Correct 19 ms 24396 KB Output is correct
104 Correct 17 ms 24424 KB Output is correct
105 Correct 17 ms 24264 KB Output is correct
106 Correct 16 ms 24216 KB Output is correct
107 Correct 17 ms 24212 KB Output is correct
108 Correct 542 ms 101040 KB Output is correct
109 Correct 1047 ms 108920 KB Output is correct
110 Correct 1066 ms 117896 KB Output is correct
111 Correct 1237 ms 132312 KB Output is correct
112 Correct 1122 ms 119148 KB Output is correct
113 Correct 625 ms 114844 KB Output is correct
114 Correct 750 ms 94572 KB Output is correct
115 Correct 751 ms 94508 KB Output is correct
116 Correct 861 ms 97304 KB Output is correct
117 Correct 1033 ms 115484 KB Output is correct
118 Correct 758 ms 94548 KB Output is correct
119 Correct 1024 ms 115524 KB Output is correct
120 Correct 1314 ms 125932 KB Output is correct
121 Correct 1067 ms 115384 KB Output is correct
122 Correct 923 ms 101520 KB Output is correct
123 Correct 669 ms 118452 KB Output is correct
124 Correct 1085 ms 107608 KB Output is correct