//0 1 1 0 1
//0 1 0 0 1
//1 0 0 1 1
//0 1 1 0 1
#include <bits/stdc++.h>
#pragma GCC optimize("Ofast,unroll-loops")
#pragma GCC target("avx2")
using namespace std;
#define F first
#define S second
#define pb push_back
#define sze size()
#define all(x) x.begin() , x.end()
#define wall__ cout << "--------------------------------------\n";
#define kids int mid = (tl + tr) >> 1, cl = v << 1, cr = v << 1 | 1
#define file_io freopen("input.cpp", "r", stdin); freopen("output.cpp", "w", stdout);
typedef long long ll;
typedef long double dl;
typedef pair < int , int > pii;
typedef pair < int , ll > pil;
typedef pair < ll , int > pli;
typedef pair < ll , ll > pll;
typedef pair < int , pii > piii;
typedef pair < ll, pll > plll;
const ll N = 1e5 + 10;
const ll mod = 1e9 + 7;
const ll inf = 2e16;
const ll INF = 1e9 + 10;
const ll lg = 32;
int n, k, a[N], timer, c[N], sub[N], ans, b[N];
int root, cnt[N], h[N], st[N], en[N], ord[N];
int sum;
vector < int > g[N];
bitset < N > mark;
void dfs0 (int v, int p) {
sub[v] = 1; st[v] = ++timer; ord[st[v]] = v;
h[v] = 0;
for (auto u : g[v]) {
if (p == u) continue;
dfs0(u, v);
if (sub[u] > sub[h[v]]) h[v] = u;
sub[v] += sub[u];
}
en[v] = timer;
}
void dfs (int v, int p) {
for (auto u : g[v]) {
if (u == p || u == h[v]) continue;
dfs(u, v);
for (int i = st[u]; i <= en[u]; i++) mark[a[ord[i]]] = 0;
sum = 0;
cnt[v] += cnt[u];
}
if (h[v] != 0) {
dfs(h[v], v);
for (int i = st[v]; i < st[h[v]]; i++) {
if (mark[a[ord[i]]] == 0) {
mark[a[ord[i]]] = 1;
sum += c[a[ord[i]]];
}
}
for (int i = en[h[v]] + 1; i <= en[v]; i++) {
if (mark[a[ord[i]]] == 0) {
mark[a[ord[i]]] = 1;
sum += c[a[ord[i]]];
}
}
}
if (mark[a[v]] == 0) {
mark[a[v]] = 1;
sum += c[a[v]];
}
if ((sum == sub[v] && v != root)) {
b[v] = 1;
++cnt[v];
}
}
void dfs1(int v, int p) {
if (b[v] == 1) {
if (cnt[v] == 1 || cnt[v] == cnt[root]) ++ans;
}
for (auto u : g[v]) {
if (u == p) continue;
dfs1(u, v);
}
}
void solve () {
cin >> n >> k;
for (int i = 1; i < n; i++) {
int v, u; cin >> v >> u;
g[v].pb(u); g[u].pb(v);
}
for (int i = 1; i <= n; i++) {
cin >> a[i]; ++c[a[i]];
}
root = 1;
for (int i = 2; i <= n; i++) {
if (g[i].sze > g[root].sze) root = i;
}
dfs0(root, 0);
dfs(root, 0);
dfs1(root, 0);
cout << (ans + 1) / 2 << '\n';
}
int main() {
ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
int t = 1;
// cin >> t;
while (t--) {solve();}
return 0;
}
/*
*/
//shrek will AC this;
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
2644 KB |
Output is correct |
2 |
Correct |
1 ms |
2644 KB |
Output is correct |
3 |
Correct |
1 ms |
2644 KB |
Output is correct |
4 |
Correct |
2 ms |
2680 KB |
Output is correct |
5 |
Correct |
1 ms |
2644 KB |
Output is correct |
6 |
Correct |
1 ms |
2644 KB |
Output is correct |
7 |
Correct |
1 ms |
2644 KB |
Output is correct |
8 |
Correct |
1 ms |
2680 KB |
Output is correct |
9 |
Incorrect |
1 ms |
2644 KB |
Output isn't correct |
10 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
2644 KB |
Output is correct |
2 |
Correct |
1 ms |
2644 KB |
Output is correct |
3 |
Correct |
1 ms |
2644 KB |
Output is correct |
4 |
Correct |
2 ms |
2680 KB |
Output is correct |
5 |
Correct |
1 ms |
2644 KB |
Output is correct |
6 |
Correct |
1 ms |
2644 KB |
Output is correct |
7 |
Correct |
1 ms |
2644 KB |
Output is correct |
8 |
Correct |
1 ms |
2680 KB |
Output is correct |
9 |
Incorrect |
1 ms |
2644 KB |
Output isn't correct |
10 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
2644 KB |
Output is correct |
2 |
Correct |
1 ms |
2644 KB |
Output is correct |
3 |
Correct |
1 ms |
2644 KB |
Output is correct |
4 |
Correct |
2 ms |
2680 KB |
Output is correct |
5 |
Correct |
1 ms |
2644 KB |
Output is correct |
6 |
Correct |
1 ms |
2644 KB |
Output is correct |
7 |
Correct |
1 ms |
2644 KB |
Output is correct |
8 |
Correct |
1 ms |
2680 KB |
Output is correct |
9 |
Incorrect |
1 ms |
2644 KB |
Output isn't correct |
10 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
33 ms |
9140 KB |
Output is correct |
2 |
Correct |
32 ms |
11372 KB |
Output is correct |
3 |
Incorrect |
2 ms |
2956 KB |
Output isn't correct |
4 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
2644 KB |
Output is correct |
2 |
Correct |
1 ms |
2644 KB |
Output is correct |
3 |
Correct |
1 ms |
2644 KB |
Output is correct |
4 |
Correct |
2 ms |
2680 KB |
Output is correct |
5 |
Correct |
1 ms |
2644 KB |
Output is correct |
6 |
Correct |
1 ms |
2644 KB |
Output is correct |
7 |
Correct |
1 ms |
2644 KB |
Output is correct |
8 |
Correct |
1 ms |
2680 KB |
Output is correct |
9 |
Incorrect |
1 ms |
2644 KB |
Output isn't correct |
10 |
Halted |
0 ms |
0 KB |
- |