#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#define IOS ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
#define stdI freopen("input.txt", "r", stdin);
#define stdO freopen("output.txt", "w", stdout);
#define all(x) x.begin(), x.end()
#define mp(x, y) make_pair(x, y)
#define int long long
#define F first
#define S second
using namespace std;
using namespace __gnu_pbds;
template<typename T, template<typename> class order = less>
using ordered_set = tree<T,
null_type, order<T>, rb_tree_tag,
tree_order_statistics_node_update>;
typedef pair<int, int> pii;
const int MAXN = 5e5 + 100, MAXLOG = 18, MAXSQ = 500, INF = 1e18, MOD = 1e9 + 7;
vector<int> adj[MAXN];
int state[MAXN], cnts[MAXN], jaleb[MAXN];
map<int, int> dalli[MAXN];
int n, k, ans;
void un(int u, int v)
{
if(dalli[u].size() < dalli[v].size())
{
dalli[u].swap(dalli[v]);
}
for(auto [q, w] : dalli[v])
{
dalli[u][q] += w;
}
}
void dfs(int v, int p)
{
dalli[v][state[v]]++;
for(auto u : adj[v])
{
if(u != p)
{
dfs(u, v);
un(v, u);
dalli[u].clear();
}
}
if(v == 1)
{
return;
}
bool bendaz = 1;
for(int i = 1; i <= k; i++)
{
if(!(cnts[i] == dalli[v][i] || dalli[v][i] == 0))
{
bendaz = 0;
break;
}
}
if(bendaz)
{
jaleb[v]++;
}
}
void solve()
{
cin >> n >> k;
for(int i = 1; i < n; i++)
{
int u, v; cin >> u >> v;
adj[u].push_back(v); adj[v].push_back(u);
}
for(int i = 1; i <= n; i++)
{
cin >> state[i]; cnts[state[i]]++;
}
dfs(1, 0);
for(int i = 2; i <= n; i++)
{
ans += ((jaleb[i] == 1) ? 1 : 0);
}
cout << (ans + 1) / 2 << "\n";
}
int32_t main()
{
IOS;
int t = 1;
//cin >> t;
while(t--)
{
solve();
}
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
8 ms |
41040 KB |
Output is correct |
2 |
Correct |
9 ms |
41040 KB |
Output is correct |
3 |
Correct |
8 ms |
41040 KB |
Output is correct |
4 |
Correct |
9 ms |
41068 KB |
Output is correct |
5 |
Correct |
9 ms |
41040 KB |
Output is correct |
6 |
Correct |
10 ms |
41040 KB |
Output is correct |
7 |
Correct |
9 ms |
41040 KB |
Output is correct |
8 |
Correct |
8 ms |
41040 KB |
Output is correct |
9 |
Incorrect |
8 ms |
41040 KB |
Output isn't correct |
10 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
8 ms |
41040 KB |
Output is correct |
2 |
Correct |
9 ms |
41040 KB |
Output is correct |
3 |
Correct |
8 ms |
41040 KB |
Output is correct |
4 |
Correct |
9 ms |
41068 KB |
Output is correct |
5 |
Correct |
9 ms |
41040 KB |
Output is correct |
6 |
Correct |
10 ms |
41040 KB |
Output is correct |
7 |
Correct |
9 ms |
41040 KB |
Output is correct |
8 |
Correct |
8 ms |
41040 KB |
Output is correct |
9 |
Incorrect |
8 ms |
41040 KB |
Output isn't correct |
10 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
8 ms |
41040 KB |
Output is correct |
2 |
Correct |
9 ms |
41040 KB |
Output is correct |
3 |
Correct |
8 ms |
41040 KB |
Output is correct |
4 |
Correct |
9 ms |
41068 KB |
Output is correct |
5 |
Correct |
9 ms |
41040 KB |
Output is correct |
6 |
Correct |
10 ms |
41040 KB |
Output is correct |
7 |
Correct |
9 ms |
41040 KB |
Output is correct |
8 |
Correct |
8 ms |
41040 KB |
Output is correct |
9 |
Incorrect |
8 ms |
41040 KB |
Output isn't correct |
10 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
230 ms |
45012 KB |
Output is correct |
2 |
Execution timed out |
3055 ms |
57480 KB |
Time limit exceeded |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
8 ms |
41040 KB |
Output is correct |
2 |
Correct |
9 ms |
41040 KB |
Output is correct |
3 |
Correct |
8 ms |
41040 KB |
Output is correct |
4 |
Correct |
9 ms |
41068 KB |
Output is correct |
5 |
Correct |
9 ms |
41040 KB |
Output is correct |
6 |
Correct |
10 ms |
41040 KB |
Output is correct |
7 |
Correct |
9 ms |
41040 KB |
Output is correct |
8 |
Correct |
8 ms |
41040 KB |
Output is correct |
9 |
Incorrect |
8 ms |
41040 KB |
Output isn't correct |
10 |
Halted |
0 ms |
0 KB |
- |