// Make the best become better
// No room for laziness
#include<bits/stdc++.h>
#define int long long
#define pb push_back
#define fi first
#define se second
using namespace std;
using ll = long long;
using ld = long double;
using ull = unsigned long long;
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
const int maxN = 1e6 + 5;
const int mod = 1e9 + 7;
const ll oo = 1e18;
int n, m;
vector<int> adj[maxN];
vector<int> vc[maxN];
int f[maxN][20];
int depth[maxN];
pair<int, int> e[maxN];
int sum[maxN];
int lab[maxN];
int deg[maxN];
void ReadInput()
{
cin >> n >> m;
for(int i=1; i<n; i++)
{
int u, v;
cin >> u >> v;
e[i] = {u, v};
adj[u].pb(v);
adj[v].pb(u);
}
for(int i=1; i<=n; i++)
{
int x;
cin >> x;
vc[x].pb(i);
}
}
int Findset(int u)
{
return lab[u] < 0 ? u : lab[u] = Findset(lab[u]);
}
void Unite(int u, int v)
{
int r = Findset(u), s = Findset(v);
if(r == s) return;
if(lab[r] > lab[s]) swap(r, s);
lab[r] += lab[s];
lab[s] = r;
}
void dfs(int u, int par)
{
for(int v : adj[u])
{
if(v == par) continue;
f[v][0] = u;
depth[v] = depth[u] + 1;
for(int i=1; i<=18; i++)
f[v][i] = f[f[v][i - 1]][i - 1];
dfs(v, u);
}
}
int lca(int u, int v)
{
if(depth[u] < depth[v]) swap(u, v);
int k = depth[u] - depth[v];
for(int i=18; i>=0; i--)
if((k >> i) & 1)
u = f[u][i];
if(u == v) return u;
for(int i=18; i>=0; i--)
if(f[u][i] != f[v][i])
{
u = f[u][i];
v = f[v][i];
}
return f[u][0];
}
void dfs1(int u, int par)
{
for(int v : adj[u])
{
if(v == par) continue;
dfs1(v, u);
sum[u] += sum[v];
}
if(sum[u]) Unite(u, par);
}
void Solve()
{
dfs(1, 0);
for(int i=1; i<=m; i++)
{
if(vc[i].empty()) continue;
int tmp = vc[i][0];
for(int v : vc[i])
{
sum[v] += -1;
tmp = lca(tmp, v);
}
sum[tmp] += vc[i].size();
// cout << tmp << " ";
}
memset(lab, -1, sizeof lab);
dfs1(1, 0);
map<pair<int, int>, int> mp;
for(int i=1; i<n; i++)
{
int u = Findset(e[i].fi), v = Findset(e[i].se);
if(u == v || mp[{u, v}]) continue;
deg[u]++;
deg[v]++;
mp[{u, v}] = mp[{v, u}] = 1;
}
int cnt = 0;
for(int i=1; i<=n; i++)
{
// cout << Findset(i) << " ";
if(lab[i] > 0) continue;
if(deg[i] == 1) cnt++;
}
cout << (cnt + 1) / 2;
}
int32_t main()
{
//freopen("sol.inp", "r", stdin);
//freopen("sol.out", "w", stdout);
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
ReadInput();
Solve();
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
27 ms |
55124 KB |
Output is correct |
2 |
Correct |
29 ms |
55124 KB |
Output is correct |
3 |
Correct |
30 ms |
55124 KB |
Output is correct |
4 |
Correct |
26 ms |
55124 KB |
Output is correct |
5 |
Correct |
31 ms |
55072 KB |
Output is correct |
6 |
Correct |
27 ms |
55108 KB |
Output is correct |
7 |
Correct |
25 ms |
55104 KB |
Output is correct |
8 |
Correct |
26 ms |
55116 KB |
Output is correct |
9 |
Correct |
28 ms |
55088 KB |
Output is correct |
10 |
Correct |
27 ms |
55140 KB |
Output is correct |
11 |
Correct |
26 ms |
55136 KB |
Output is correct |
12 |
Correct |
27 ms |
55108 KB |
Output is correct |
13 |
Correct |
26 ms |
55088 KB |
Output is correct |
14 |
Correct |
28 ms |
55112 KB |
Output is correct |
15 |
Correct |
25 ms |
55148 KB |
Output is correct |
16 |
Correct |
27 ms |
55124 KB |
Output is correct |
17 |
Correct |
30 ms |
55112 KB |
Output is correct |
18 |
Correct |
25 ms |
55092 KB |
Output is correct |
19 |
Correct |
26 ms |
55124 KB |
Output is correct |
20 |
Correct |
28 ms |
55116 KB |
Output is correct |
21 |
Correct |
27 ms |
55124 KB |
Output is correct |
22 |
Correct |
26 ms |
55140 KB |
Output is correct |
23 |
Correct |
26 ms |
55064 KB |
Output is correct |
24 |
Correct |
26 ms |
55160 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
27 ms |
55124 KB |
Output is correct |
2 |
Correct |
29 ms |
55124 KB |
Output is correct |
3 |
Correct |
30 ms |
55124 KB |
Output is correct |
4 |
Correct |
26 ms |
55124 KB |
Output is correct |
5 |
Correct |
31 ms |
55072 KB |
Output is correct |
6 |
Correct |
27 ms |
55108 KB |
Output is correct |
7 |
Correct |
25 ms |
55104 KB |
Output is correct |
8 |
Correct |
26 ms |
55116 KB |
Output is correct |
9 |
Correct |
28 ms |
55088 KB |
Output is correct |
10 |
Correct |
27 ms |
55140 KB |
Output is correct |
11 |
Correct |
26 ms |
55136 KB |
Output is correct |
12 |
Correct |
27 ms |
55108 KB |
Output is correct |
13 |
Correct |
26 ms |
55088 KB |
Output is correct |
14 |
Correct |
28 ms |
55112 KB |
Output is correct |
15 |
Correct |
25 ms |
55148 KB |
Output is correct |
16 |
Correct |
27 ms |
55124 KB |
Output is correct |
17 |
Correct |
30 ms |
55112 KB |
Output is correct |
18 |
Correct |
25 ms |
55092 KB |
Output is correct |
19 |
Correct |
26 ms |
55124 KB |
Output is correct |
20 |
Correct |
28 ms |
55116 KB |
Output is correct |
21 |
Correct |
27 ms |
55124 KB |
Output is correct |
22 |
Correct |
26 ms |
55140 KB |
Output is correct |
23 |
Correct |
26 ms |
55064 KB |
Output is correct |
24 |
Correct |
26 ms |
55160 KB |
Output is correct |
25 |
Correct |
33 ms |
55040 KB |
Output is correct |
26 |
Correct |
32 ms |
56156 KB |
Output is correct |
27 |
Correct |
28 ms |
55880 KB |
Output is correct |
28 |
Correct |
29 ms |
56280 KB |
Output is correct |
29 |
Correct |
28 ms |
56276 KB |
Output is correct |
30 |
Correct |
28 ms |
55892 KB |
Output is correct |
31 |
Correct |
27 ms |
55124 KB |
Output is correct |
32 |
Correct |
28 ms |
56092 KB |
Output is correct |
33 |
Correct |
27 ms |
55124 KB |
Output is correct |
34 |
Correct |
27 ms |
55892 KB |
Output is correct |
35 |
Correct |
29 ms |
56276 KB |
Output is correct |
36 |
Correct |
29 ms |
55864 KB |
Output is correct |
37 |
Correct |
28 ms |
55892 KB |
Output is correct |
38 |
Correct |
30 ms |
55088 KB |
Output is correct |
39 |
Correct |
28 ms |
55888 KB |
Output is correct |
40 |
Correct |
29 ms |
55820 KB |
Output is correct |
41 |
Correct |
28 ms |
55892 KB |
Output is correct |
42 |
Correct |
28 ms |
56020 KB |
Output is correct |
43 |
Correct |
29 ms |
56012 KB |
Output is correct |
44 |
Correct |
28 ms |
55096 KB |
Output is correct |
45 |
Correct |
28 ms |
56008 KB |
Output is correct |
46 |
Correct |
29 ms |
55956 KB |
Output is correct |
47 |
Correct |
28 ms |
55096 KB |
Output is correct |
48 |
Correct |
28 ms |
55888 KB |
Output is correct |
49 |
Correct |
34 ms |
56348 KB |
Output is correct |
50 |
Correct |
36 ms |
56396 KB |
Output is correct |
51 |
Correct |
33 ms |
55860 KB |
Output is correct |
52 |
Correct |
27 ms |
55868 KB |
Output is correct |
53 |
Correct |
28 ms |
56012 KB |
Output is correct |
54 |
Correct |
27 ms |
55892 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
27 ms |
55124 KB |
Output is correct |
2 |
Correct |
29 ms |
55124 KB |
Output is correct |
3 |
Correct |
30 ms |
55124 KB |
Output is correct |
4 |
Correct |
26 ms |
55124 KB |
Output is correct |
5 |
Correct |
31 ms |
55072 KB |
Output is correct |
6 |
Correct |
27 ms |
55108 KB |
Output is correct |
7 |
Correct |
25 ms |
55104 KB |
Output is correct |
8 |
Correct |
26 ms |
55116 KB |
Output is correct |
9 |
Correct |
28 ms |
55088 KB |
Output is correct |
10 |
Correct |
27 ms |
55140 KB |
Output is correct |
11 |
Correct |
26 ms |
55136 KB |
Output is correct |
12 |
Correct |
27 ms |
55108 KB |
Output is correct |
13 |
Correct |
26 ms |
55088 KB |
Output is correct |
14 |
Correct |
28 ms |
55112 KB |
Output is correct |
15 |
Correct |
25 ms |
55148 KB |
Output is correct |
16 |
Correct |
27 ms |
55124 KB |
Output is correct |
17 |
Correct |
30 ms |
55112 KB |
Output is correct |
18 |
Correct |
25 ms |
55092 KB |
Output is correct |
19 |
Correct |
26 ms |
55124 KB |
Output is correct |
20 |
Correct |
28 ms |
55116 KB |
Output is correct |
21 |
Correct |
27 ms |
55124 KB |
Output is correct |
22 |
Correct |
26 ms |
55140 KB |
Output is correct |
23 |
Correct |
26 ms |
55064 KB |
Output is correct |
24 |
Correct |
26 ms |
55160 KB |
Output is correct |
25 |
Correct |
32 ms |
55116 KB |
Output is correct |
26 |
Correct |
100 ms |
80352 KB |
Output is correct |
27 |
Correct |
118 ms |
80192 KB |
Output is correct |
28 |
Correct |
32 ms |
55884 KB |
Output is correct |
29 |
Correct |
26 ms |
55052 KB |
Output is correct |
30 |
Correct |
29 ms |
55104 KB |
Output is correct |
31 |
Correct |
134 ms |
80244 KB |
Output is correct |
32 |
Correct |
28 ms |
55904 KB |
Output is correct |
33 |
Correct |
155 ms |
85308 KB |
Output is correct |
34 |
Correct |
121 ms |
80124 KB |
Output is correct |
35 |
Correct |
28 ms |
55908 KB |
Output is correct |
36 |
Correct |
141 ms |
80244 KB |
Output is correct |
37 |
Correct |
32 ms |
55860 KB |
Output is correct |
38 |
Correct |
27 ms |
55864 KB |
Output is correct |
39 |
Correct |
94 ms |
80292 KB |
Output is correct |
40 |
Correct |
28 ms |
56012 KB |
Output is correct |
41 |
Correct |
112 ms |
79852 KB |
Output is correct |
42 |
Correct |
142 ms |
81452 KB |
Output is correct |
43 |
Correct |
26 ms |
55096 KB |
Output is correct |
44 |
Correct |
152 ms |
85292 KB |
Output is correct |
45 |
Correct |
150 ms |
83832 KB |
Output is correct |
46 |
Correct |
28 ms |
55884 KB |
Output is correct |
47 |
Correct |
28 ms |
55892 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
90 ms |
79020 KB |
Output is correct |
2 |
Correct |
187 ms |
94316 KB |
Output is correct |
3 |
Correct |
34 ms |
56092 KB |
Output is correct |
4 |
Correct |
27 ms |
55876 KB |
Output is correct |
5 |
Correct |
26 ms |
55164 KB |
Output is correct |
6 |
Correct |
25 ms |
55072 KB |
Output is correct |
7 |
Correct |
27 ms |
55912 KB |
Output is correct |
8 |
Correct |
148 ms |
83248 KB |
Output is correct |
9 |
Correct |
28 ms |
55828 KB |
Output is correct |
10 |
Correct |
126 ms |
81048 KB |
Output is correct |
11 |
Correct |
27 ms |
55104 KB |
Output is correct |
12 |
Correct |
135 ms |
80652 KB |
Output is correct |
13 |
Correct |
151 ms |
84372 KB |
Output is correct |
14 |
Correct |
231 ms |
95836 KB |
Output is correct |
15 |
Correct |
105 ms |
80368 KB |
Output is correct |
16 |
Correct |
28 ms |
56028 KB |
Output is correct |
17 |
Correct |
28 ms |
55160 KB |
Output is correct |
18 |
Correct |
169 ms |
93276 KB |
Output is correct |
19 |
Correct |
245 ms |
98380 KB |
Output is correct |
20 |
Correct |
29 ms |
55984 KB |
Output is correct |
21 |
Correct |
28 ms |
55124 KB |
Output is correct |
22 |
Correct |
120 ms |
84328 KB |
Output is correct |
23 |
Correct |
27 ms |
55884 KB |
Output is correct |
24 |
Correct |
164 ms |
82500 KB |
Output is correct |
25 |
Correct |
251 ms |
98128 KB |
Output is correct |
26 |
Correct |
29 ms |
56292 KB |
Output is correct |
27 |
Correct |
31 ms |
56312 KB |
Output is correct |
28 |
Correct |
28 ms |
55892 KB |
Output is correct |
29 |
Correct |
28 ms |
56004 KB |
Output is correct |
30 |
Correct |
28 ms |
55964 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
27 ms |
55124 KB |
Output is correct |
2 |
Correct |
29 ms |
55124 KB |
Output is correct |
3 |
Correct |
30 ms |
55124 KB |
Output is correct |
4 |
Correct |
26 ms |
55124 KB |
Output is correct |
5 |
Correct |
31 ms |
55072 KB |
Output is correct |
6 |
Correct |
27 ms |
55108 KB |
Output is correct |
7 |
Correct |
25 ms |
55104 KB |
Output is correct |
8 |
Correct |
26 ms |
55116 KB |
Output is correct |
9 |
Correct |
28 ms |
55088 KB |
Output is correct |
10 |
Correct |
27 ms |
55140 KB |
Output is correct |
11 |
Correct |
26 ms |
55136 KB |
Output is correct |
12 |
Correct |
27 ms |
55108 KB |
Output is correct |
13 |
Correct |
26 ms |
55088 KB |
Output is correct |
14 |
Correct |
28 ms |
55112 KB |
Output is correct |
15 |
Correct |
25 ms |
55148 KB |
Output is correct |
16 |
Correct |
27 ms |
55124 KB |
Output is correct |
17 |
Correct |
30 ms |
55112 KB |
Output is correct |
18 |
Correct |
25 ms |
55092 KB |
Output is correct |
19 |
Correct |
26 ms |
55124 KB |
Output is correct |
20 |
Correct |
28 ms |
55116 KB |
Output is correct |
21 |
Correct |
27 ms |
55124 KB |
Output is correct |
22 |
Correct |
26 ms |
55140 KB |
Output is correct |
23 |
Correct |
26 ms |
55064 KB |
Output is correct |
24 |
Correct |
26 ms |
55160 KB |
Output is correct |
25 |
Correct |
33 ms |
55040 KB |
Output is correct |
26 |
Correct |
32 ms |
56156 KB |
Output is correct |
27 |
Correct |
28 ms |
55880 KB |
Output is correct |
28 |
Correct |
29 ms |
56280 KB |
Output is correct |
29 |
Correct |
28 ms |
56276 KB |
Output is correct |
30 |
Correct |
28 ms |
55892 KB |
Output is correct |
31 |
Correct |
27 ms |
55124 KB |
Output is correct |
32 |
Correct |
28 ms |
56092 KB |
Output is correct |
33 |
Correct |
27 ms |
55124 KB |
Output is correct |
34 |
Correct |
27 ms |
55892 KB |
Output is correct |
35 |
Correct |
29 ms |
56276 KB |
Output is correct |
36 |
Correct |
29 ms |
55864 KB |
Output is correct |
37 |
Correct |
28 ms |
55892 KB |
Output is correct |
38 |
Correct |
30 ms |
55088 KB |
Output is correct |
39 |
Correct |
28 ms |
55888 KB |
Output is correct |
40 |
Correct |
29 ms |
55820 KB |
Output is correct |
41 |
Correct |
28 ms |
55892 KB |
Output is correct |
42 |
Correct |
28 ms |
56020 KB |
Output is correct |
43 |
Correct |
29 ms |
56012 KB |
Output is correct |
44 |
Correct |
28 ms |
55096 KB |
Output is correct |
45 |
Correct |
28 ms |
56008 KB |
Output is correct |
46 |
Correct |
29 ms |
55956 KB |
Output is correct |
47 |
Correct |
28 ms |
55096 KB |
Output is correct |
48 |
Correct |
28 ms |
55888 KB |
Output is correct |
49 |
Correct |
34 ms |
56348 KB |
Output is correct |
50 |
Correct |
36 ms |
56396 KB |
Output is correct |
51 |
Correct |
33 ms |
55860 KB |
Output is correct |
52 |
Correct |
27 ms |
55868 KB |
Output is correct |
53 |
Correct |
28 ms |
56012 KB |
Output is correct |
54 |
Correct |
27 ms |
55892 KB |
Output is correct |
55 |
Correct |
32 ms |
55116 KB |
Output is correct |
56 |
Correct |
100 ms |
80352 KB |
Output is correct |
57 |
Correct |
118 ms |
80192 KB |
Output is correct |
58 |
Correct |
32 ms |
55884 KB |
Output is correct |
59 |
Correct |
26 ms |
55052 KB |
Output is correct |
60 |
Correct |
29 ms |
55104 KB |
Output is correct |
61 |
Correct |
134 ms |
80244 KB |
Output is correct |
62 |
Correct |
28 ms |
55904 KB |
Output is correct |
63 |
Correct |
155 ms |
85308 KB |
Output is correct |
64 |
Correct |
121 ms |
80124 KB |
Output is correct |
65 |
Correct |
28 ms |
55908 KB |
Output is correct |
66 |
Correct |
141 ms |
80244 KB |
Output is correct |
67 |
Correct |
32 ms |
55860 KB |
Output is correct |
68 |
Correct |
27 ms |
55864 KB |
Output is correct |
69 |
Correct |
94 ms |
80292 KB |
Output is correct |
70 |
Correct |
28 ms |
56012 KB |
Output is correct |
71 |
Correct |
112 ms |
79852 KB |
Output is correct |
72 |
Correct |
142 ms |
81452 KB |
Output is correct |
73 |
Correct |
26 ms |
55096 KB |
Output is correct |
74 |
Correct |
152 ms |
85292 KB |
Output is correct |
75 |
Correct |
150 ms |
83832 KB |
Output is correct |
76 |
Correct |
28 ms |
55884 KB |
Output is correct |
77 |
Correct |
28 ms |
55892 KB |
Output is correct |
78 |
Correct |
90 ms |
79020 KB |
Output is correct |
79 |
Correct |
187 ms |
94316 KB |
Output is correct |
80 |
Correct |
34 ms |
56092 KB |
Output is correct |
81 |
Correct |
27 ms |
55876 KB |
Output is correct |
82 |
Correct |
26 ms |
55164 KB |
Output is correct |
83 |
Correct |
25 ms |
55072 KB |
Output is correct |
84 |
Correct |
27 ms |
55912 KB |
Output is correct |
85 |
Correct |
148 ms |
83248 KB |
Output is correct |
86 |
Correct |
28 ms |
55828 KB |
Output is correct |
87 |
Correct |
126 ms |
81048 KB |
Output is correct |
88 |
Correct |
27 ms |
55104 KB |
Output is correct |
89 |
Correct |
135 ms |
80652 KB |
Output is correct |
90 |
Correct |
151 ms |
84372 KB |
Output is correct |
91 |
Correct |
231 ms |
95836 KB |
Output is correct |
92 |
Correct |
105 ms |
80368 KB |
Output is correct |
93 |
Correct |
28 ms |
56028 KB |
Output is correct |
94 |
Correct |
28 ms |
55160 KB |
Output is correct |
95 |
Correct |
169 ms |
93276 KB |
Output is correct |
96 |
Correct |
245 ms |
98380 KB |
Output is correct |
97 |
Correct |
29 ms |
55984 KB |
Output is correct |
98 |
Correct |
28 ms |
55124 KB |
Output is correct |
99 |
Correct |
120 ms |
84328 KB |
Output is correct |
100 |
Correct |
27 ms |
55884 KB |
Output is correct |
101 |
Correct |
164 ms |
82500 KB |
Output is correct |
102 |
Correct |
251 ms |
98128 KB |
Output is correct |
103 |
Correct |
29 ms |
56292 KB |
Output is correct |
104 |
Correct |
31 ms |
56312 KB |
Output is correct |
105 |
Correct |
28 ms |
55892 KB |
Output is correct |
106 |
Correct |
28 ms |
56004 KB |
Output is correct |
107 |
Correct |
28 ms |
55964 KB |
Output is correct |
108 |
Correct |
679 ms |
196700 KB |
Output is correct |
109 |
Correct |
937 ms |
191164 KB |
Output is correct |
110 |
Correct |
937 ms |
200108 KB |
Output is correct |
111 |
Correct |
1936 ms |
262144 KB |
Output is correct |
112 |
Correct |
1399 ms |
242908 KB |
Output is correct |
113 |
Correct |
1249 ms |
247184 KB |
Output is correct |
114 |
Correct |
683 ms |
179472 KB |
Output is correct |
115 |
Correct |
673 ms |
179508 KB |
Output is correct |
116 |
Correct |
859 ms |
187284 KB |
Output is correct |
117 |
Correct |
1836 ms |
260012 KB |
Output is correct |
118 |
Correct |
730 ms |
182120 KB |
Output is correct |
119 |
Correct |
1822 ms |
259960 KB |
Output is correct |
120 |
Runtime error |
1815 ms |
262144 KB |
Execution killed with signal 9 |
121 |
Halted |
0 ms |
0 KB |
- |