// 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][21];
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<=20; 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=20; i>=0; i--)
if((k >> i) & 1)
u = f[u][i];
if(u == v) return u;
for(int i=20; 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();
}
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 |
26 ms |
55124 KB |
Output is correct |
2 |
Correct |
25 ms |
55124 KB |
Output is correct |
3 |
Correct |
26 ms |
55176 KB |
Output is correct |
4 |
Correct |
25 ms |
55124 KB |
Output is correct |
5 |
Correct |
26 ms |
55052 KB |
Output is correct |
6 |
Correct |
26 ms |
55180 KB |
Output is correct |
7 |
Correct |
27 ms |
55124 KB |
Output is correct |
8 |
Correct |
29 ms |
55132 KB |
Output is correct |
9 |
Correct |
26 ms |
55156 KB |
Output is correct |
10 |
Correct |
26 ms |
55080 KB |
Output is correct |
11 |
Correct |
26 ms |
55180 KB |
Output is correct |
12 |
Correct |
26 ms |
55112 KB |
Output is correct |
13 |
Correct |
28 ms |
55144 KB |
Output is correct |
14 |
Correct |
26 ms |
55168 KB |
Output is correct |
15 |
Correct |
28 ms |
55140 KB |
Output is correct |
16 |
Correct |
26 ms |
55056 KB |
Output is correct |
17 |
Correct |
31 ms |
55072 KB |
Output is correct |
18 |
Correct |
28 ms |
55180 KB |
Output is correct |
19 |
Correct |
26 ms |
55124 KB |
Output is correct |
20 |
Correct |
25 ms |
55124 KB |
Output is correct |
21 |
Correct |
25 ms |
55184 KB |
Output is correct |
22 |
Correct |
26 ms |
55128 KB |
Output is correct |
23 |
Correct |
26 ms |
55144 KB |
Output is correct |
24 |
Correct |
27 ms |
55124 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
26 ms |
55124 KB |
Output is correct |
2 |
Correct |
25 ms |
55124 KB |
Output is correct |
3 |
Correct |
26 ms |
55176 KB |
Output is correct |
4 |
Correct |
25 ms |
55124 KB |
Output is correct |
5 |
Correct |
26 ms |
55052 KB |
Output is correct |
6 |
Correct |
26 ms |
55180 KB |
Output is correct |
7 |
Correct |
27 ms |
55124 KB |
Output is correct |
8 |
Correct |
29 ms |
55132 KB |
Output is correct |
9 |
Correct |
26 ms |
55156 KB |
Output is correct |
10 |
Correct |
26 ms |
55080 KB |
Output is correct |
11 |
Correct |
26 ms |
55180 KB |
Output is correct |
12 |
Correct |
26 ms |
55112 KB |
Output is correct |
13 |
Correct |
28 ms |
55144 KB |
Output is correct |
14 |
Correct |
26 ms |
55168 KB |
Output is correct |
15 |
Correct |
28 ms |
55140 KB |
Output is correct |
16 |
Correct |
26 ms |
55056 KB |
Output is correct |
17 |
Correct |
31 ms |
55072 KB |
Output is correct |
18 |
Correct |
28 ms |
55180 KB |
Output is correct |
19 |
Correct |
26 ms |
55124 KB |
Output is correct |
20 |
Correct |
25 ms |
55124 KB |
Output is correct |
21 |
Correct |
25 ms |
55184 KB |
Output is correct |
22 |
Correct |
26 ms |
55128 KB |
Output is correct |
23 |
Correct |
26 ms |
55144 KB |
Output is correct |
24 |
Correct |
27 ms |
55124 KB |
Output is correct |
25 |
Correct |
26 ms |
55124 KB |
Output is correct |
26 |
Correct |
32 ms |
56060 KB |
Output is correct |
27 |
Correct |
27 ms |
55816 KB |
Output is correct |
28 |
Correct |
28 ms |
56224 KB |
Output is correct |
29 |
Correct |
29 ms |
56316 KB |
Output is correct |
30 |
Correct |
28 ms |
55876 KB |
Output is correct |
31 |
Correct |
27 ms |
55120 KB |
Output is correct |
32 |
Correct |
32 ms |
56112 KB |
Output is correct |
33 |
Correct |
29 ms |
55176 KB |
Output is correct |
34 |
Correct |
27 ms |
55864 KB |
Output is correct |
35 |
Correct |
30 ms |
56284 KB |
Output is correct |
36 |
Correct |
28 ms |
55896 KB |
Output is correct |
37 |
Correct |
34 ms |
55964 KB |
Output is correct |
38 |
Correct |
28 ms |
55124 KB |
Output is correct |
39 |
Correct |
29 ms |
55900 KB |
Output is correct |
40 |
Correct |
28 ms |
55892 KB |
Output is correct |
41 |
Correct |
32 ms |
55940 KB |
Output is correct |
42 |
Correct |
28 ms |
56088 KB |
Output is correct |
43 |
Correct |
28 ms |
56020 KB |
Output is correct |
44 |
Correct |
28 ms |
55112 KB |
Output is correct |
45 |
Correct |
29 ms |
56020 KB |
Output is correct |
46 |
Correct |
37 ms |
55892 KB |
Output is correct |
47 |
Correct |
30 ms |
55140 KB |
Output is correct |
48 |
Correct |
29 ms |
55892 KB |
Output is correct |
49 |
Correct |
34 ms |
56340 KB |
Output is correct |
50 |
Correct |
32 ms |
56376 KB |
Output is correct |
51 |
Correct |
27 ms |
55968 KB |
Output is correct |
52 |
Correct |
28 ms |
55920 KB |
Output is correct |
53 |
Correct |
34 ms |
55996 KB |
Output is correct |
54 |
Correct |
29 ms |
55892 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
26 ms |
55124 KB |
Output is correct |
2 |
Correct |
25 ms |
55124 KB |
Output is correct |
3 |
Correct |
26 ms |
55176 KB |
Output is correct |
4 |
Correct |
25 ms |
55124 KB |
Output is correct |
5 |
Correct |
26 ms |
55052 KB |
Output is correct |
6 |
Correct |
26 ms |
55180 KB |
Output is correct |
7 |
Correct |
27 ms |
55124 KB |
Output is correct |
8 |
Correct |
29 ms |
55132 KB |
Output is correct |
9 |
Correct |
26 ms |
55156 KB |
Output is correct |
10 |
Correct |
26 ms |
55080 KB |
Output is correct |
11 |
Correct |
26 ms |
55180 KB |
Output is correct |
12 |
Correct |
26 ms |
55112 KB |
Output is correct |
13 |
Correct |
28 ms |
55144 KB |
Output is correct |
14 |
Correct |
26 ms |
55168 KB |
Output is correct |
15 |
Correct |
28 ms |
55140 KB |
Output is correct |
16 |
Correct |
26 ms |
55056 KB |
Output is correct |
17 |
Correct |
31 ms |
55072 KB |
Output is correct |
18 |
Correct |
28 ms |
55180 KB |
Output is correct |
19 |
Correct |
26 ms |
55124 KB |
Output is correct |
20 |
Correct |
25 ms |
55124 KB |
Output is correct |
21 |
Correct |
25 ms |
55184 KB |
Output is correct |
22 |
Correct |
26 ms |
55128 KB |
Output is correct |
23 |
Correct |
26 ms |
55144 KB |
Output is correct |
24 |
Correct |
27 ms |
55124 KB |
Output is correct |
25 |
Correct |
27 ms |
55120 KB |
Output is correct |
26 |
Correct |
111 ms |
79700 KB |
Output is correct |
27 |
Correct |
135 ms |
79500 KB |
Output is correct |
28 |
Correct |
28 ms |
55916 KB |
Output is correct |
29 |
Correct |
27 ms |
55048 KB |
Output is correct |
30 |
Correct |
26 ms |
55164 KB |
Output is correct |
31 |
Correct |
156 ms |
79524 KB |
Output is correct |
32 |
Correct |
28 ms |
55808 KB |
Output is correct |
33 |
Correct |
155 ms |
84664 KB |
Output is correct |
34 |
Correct |
126 ms |
79440 KB |
Output is correct |
35 |
Correct |
27 ms |
55892 KB |
Output is correct |
36 |
Correct |
155 ms |
79604 KB |
Output is correct |
37 |
Correct |
34 ms |
55880 KB |
Output is correct |
38 |
Correct |
28 ms |
55880 KB |
Output is correct |
39 |
Correct |
93 ms |
79692 KB |
Output is correct |
40 |
Correct |
28 ms |
55956 KB |
Output is correct |
41 |
Correct |
115 ms |
79264 KB |
Output is correct |
42 |
Correct |
150 ms |
80724 KB |
Output is correct |
43 |
Correct |
27 ms |
55096 KB |
Output is correct |
44 |
Correct |
162 ms |
84780 KB |
Output is correct |
45 |
Correct |
152 ms |
83152 KB |
Output is correct |
46 |
Correct |
28 ms |
55884 KB |
Output is correct |
47 |
Correct |
28 ms |
55788 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
95 ms |
79872 KB |
Output is correct |
2 |
Correct |
213 ms |
95216 KB |
Output is correct |
3 |
Correct |
29 ms |
56148 KB |
Output is correct |
4 |
Correct |
28 ms |
55892 KB |
Output is correct |
5 |
Correct |
26 ms |
55068 KB |
Output is correct |
6 |
Correct |
28 ms |
55172 KB |
Output is correct |
7 |
Correct |
28 ms |
55892 KB |
Output is correct |
8 |
Correct |
170 ms |
82288 KB |
Output is correct |
9 |
Correct |
28 ms |
55884 KB |
Output is correct |
10 |
Correct |
134 ms |
80372 KB |
Output is correct |
11 |
Correct |
27 ms |
55136 KB |
Output is correct |
12 |
Correct |
138 ms |
79784 KB |
Output is correct |
13 |
Correct |
168 ms |
83496 KB |
Output is correct |
14 |
Correct |
234 ms |
94788 KB |
Output is correct |
15 |
Correct |
97 ms |
79580 KB |
Output is correct |
16 |
Correct |
28 ms |
55988 KB |
Output is correct |
17 |
Correct |
26 ms |
55084 KB |
Output is correct |
18 |
Correct |
204 ms |
92216 KB |
Output is correct |
19 |
Correct |
257 ms |
97504 KB |
Output is correct |
20 |
Correct |
28 ms |
55872 KB |
Output is correct |
21 |
Correct |
29 ms |
55148 KB |
Output is correct |
22 |
Correct |
133 ms |
83524 KB |
Output is correct |
23 |
Correct |
27 ms |
55884 KB |
Output is correct |
24 |
Correct |
158 ms |
81648 KB |
Output is correct |
25 |
Correct |
263 ms |
97152 KB |
Output is correct |
26 |
Correct |
29 ms |
56268 KB |
Output is correct |
27 |
Correct |
29 ms |
56268 KB |
Output is correct |
28 |
Correct |
29 ms |
55940 KB |
Output is correct |
29 |
Correct |
29 ms |
55892 KB |
Output is correct |
30 |
Correct |
27 ms |
55892 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
26 ms |
55124 KB |
Output is correct |
2 |
Correct |
25 ms |
55124 KB |
Output is correct |
3 |
Correct |
26 ms |
55176 KB |
Output is correct |
4 |
Correct |
25 ms |
55124 KB |
Output is correct |
5 |
Correct |
26 ms |
55052 KB |
Output is correct |
6 |
Correct |
26 ms |
55180 KB |
Output is correct |
7 |
Correct |
27 ms |
55124 KB |
Output is correct |
8 |
Correct |
29 ms |
55132 KB |
Output is correct |
9 |
Correct |
26 ms |
55156 KB |
Output is correct |
10 |
Correct |
26 ms |
55080 KB |
Output is correct |
11 |
Correct |
26 ms |
55180 KB |
Output is correct |
12 |
Correct |
26 ms |
55112 KB |
Output is correct |
13 |
Correct |
28 ms |
55144 KB |
Output is correct |
14 |
Correct |
26 ms |
55168 KB |
Output is correct |
15 |
Correct |
28 ms |
55140 KB |
Output is correct |
16 |
Correct |
26 ms |
55056 KB |
Output is correct |
17 |
Correct |
31 ms |
55072 KB |
Output is correct |
18 |
Correct |
28 ms |
55180 KB |
Output is correct |
19 |
Correct |
26 ms |
55124 KB |
Output is correct |
20 |
Correct |
25 ms |
55124 KB |
Output is correct |
21 |
Correct |
25 ms |
55184 KB |
Output is correct |
22 |
Correct |
26 ms |
55128 KB |
Output is correct |
23 |
Correct |
26 ms |
55144 KB |
Output is correct |
24 |
Correct |
27 ms |
55124 KB |
Output is correct |
25 |
Correct |
26 ms |
55124 KB |
Output is correct |
26 |
Correct |
32 ms |
56060 KB |
Output is correct |
27 |
Correct |
27 ms |
55816 KB |
Output is correct |
28 |
Correct |
28 ms |
56224 KB |
Output is correct |
29 |
Correct |
29 ms |
56316 KB |
Output is correct |
30 |
Correct |
28 ms |
55876 KB |
Output is correct |
31 |
Correct |
27 ms |
55120 KB |
Output is correct |
32 |
Correct |
32 ms |
56112 KB |
Output is correct |
33 |
Correct |
29 ms |
55176 KB |
Output is correct |
34 |
Correct |
27 ms |
55864 KB |
Output is correct |
35 |
Correct |
30 ms |
56284 KB |
Output is correct |
36 |
Correct |
28 ms |
55896 KB |
Output is correct |
37 |
Correct |
34 ms |
55964 KB |
Output is correct |
38 |
Correct |
28 ms |
55124 KB |
Output is correct |
39 |
Correct |
29 ms |
55900 KB |
Output is correct |
40 |
Correct |
28 ms |
55892 KB |
Output is correct |
41 |
Correct |
32 ms |
55940 KB |
Output is correct |
42 |
Correct |
28 ms |
56088 KB |
Output is correct |
43 |
Correct |
28 ms |
56020 KB |
Output is correct |
44 |
Correct |
28 ms |
55112 KB |
Output is correct |
45 |
Correct |
29 ms |
56020 KB |
Output is correct |
46 |
Correct |
37 ms |
55892 KB |
Output is correct |
47 |
Correct |
30 ms |
55140 KB |
Output is correct |
48 |
Correct |
29 ms |
55892 KB |
Output is correct |
49 |
Correct |
34 ms |
56340 KB |
Output is correct |
50 |
Correct |
32 ms |
56376 KB |
Output is correct |
51 |
Correct |
27 ms |
55968 KB |
Output is correct |
52 |
Correct |
28 ms |
55920 KB |
Output is correct |
53 |
Correct |
34 ms |
55996 KB |
Output is correct |
54 |
Correct |
29 ms |
55892 KB |
Output is correct |
55 |
Correct |
27 ms |
55120 KB |
Output is correct |
56 |
Correct |
111 ms |
79700 KB |
Output is correct |
57 |
Correct |
135 ms |
79500 KB |
Output is correct |
58 |
Correct |
28 ms |
55916 KB |
Output is correct |
59 |
Correct |
27 ms |
55048 KB |
Output is correct |
60 |
Correct |
26 ms |
55164 KB |
Output is correct |
61 |
Correct |
156 ms |
79524 KB |
Output is correct |
62 |
Correct |
28 ms |
55808 KB |
Output is correct |
63 |
Correct |
155 ms |
84664 KB |
Output is correct |
64 |
Correct |
126 ms |
79440 KB |
Output is correct |
65 |
Correct |
27 ms |
55892 KB |
Output is correct |
66 |
Correct |
155 ms |
79604 KB |
Output is correct |
67 |
Correct |
34 ms |
55880 KB |
Output is correct |
68 |
Correct |
28 ms |
55880 KB |
Output is correct |
69 |
Correct |
93 ms |
79692 KB |
Output is correct |
70 |
Correct |
28 ms |
55956 KB |
Output is correct |
71 |
Correct |
115 ms |
79264 KB |
Output is correct |
72 |
Correct |
150 ms |
80724 KB |
Output is correct |
73 |
Correct |
27 ms |
55096 KB |
Output is correct |
74 |
Correct |
162 ms |
84780 KB |
Output is correct |
75 |
Correct |
152 ms |
83152 KB |
Output is correct |
76 |
Correct |
28 ms |
55884 KB |
Output is correct |
77 |
Correct |
28 ms |
55788 KB |
Output is correct |
78 |
Correct |
95 ms |
79872 KB |
Output is correct |
79 |
Correct |
213 ms |
95216 KB |
Output is correct |
80 |
Correct |
29 ms |
56148 KB |
Output is correct |
81 |
Correct |
28 ms |
55892 KB |
Output is correct |
82 |
Correct |
26 ms |
55068 KB |
Output is correct |
83 |
Correct |
28 ms |
55172 KB |
Output is correct |
84 |
Correct |
28 ms |
55892 KB |
Output is correct |
85 |
Correct |
170 ms |
82288 KB |
Output is correct |
86 |
Correct |
28 ms |
55884 KB |
Output is correct |
87 |
Correct |
134 ms |
80372 KB |
Output is correct |
88 |
Correct |
27 ms |
55136 KB |
Output is correct |
89 |
Correct |
138 ms |
79784 KB |
Output is correct |
90 |
Correct |
168 ms |
83496 KB |
Output is correct |
91 |
Correct |
234 ms |
94788 KB |
Output is correct |
92 |
Correct |
97 ms |
79580 KB |
Output is correct |
93 |
Correct |
28 ms |
55988 KB |
Output is correct |
94 |
Correct |
26 ms |
55084 KB |
Output is correct |
95 |
Correct |
204 ms |
92216 KB |
Output is correct |
96 |
Correct |
257 ms |
97504 KB |
Output is correct |
97 |
Correct |
28 ms |
55872 KB |
Output is correct |
98 |
Correct |
29 ms |
55148 KB |
Output is correct |
99 |
Correct |
133 ms |
83524 KB |
Output is correct |
100 |
Correct |
27 ms |
55884 KB |
Output is correct |
101 |
Correct |
158 ms |
81648 KB |
Output is correct |
102 |
Correct |
263 ms |
97152 KB |
Output is correct |
103 |
Correct |
29 ms |
56268 KB |
Output is correct |
104 |
Correct |
29 ms |
56268 KB |
Output is correct |
105 |
Correct |
29 ms |
55940 KB |
Output is correct |
106 |
Correct |
29 ms |
55892 KB |
Output is correct |
107 |
Correct |
27 ms |
55892 KB |
Output is correct |
108 |
Correct |
677 ms |
190836 KB |
Output is correct |
109 |
Correct |
980 ms |
187716 KB |
Output is correct |
110 |
Correct |
978 ms |
196612 KB |
Output is correct |
111 |
Runtime error |
1627 ms |
262144 KB |
Execution killed with signal 9 |
112 |
Halted |
0 ms |
0 KB |
- |