# |
Submission time |
Handle |
Problem |
Language |
Result |
Execution time |
Memory |
128900 |
2019-07-11T10:49:18 Z |
kuroni |
Mergers (JOI19_mergers) |
C++17 |
|
78 ms |
17304 KB |
#include <bits/stdc++.h>
using namespace std;
const int N = 500005, K = 500005;
int n, k, u, v, ans = 0, a[N], sub[N], f[N];
int cur = 0, sum[K], cnt[K];
vector<int> adj[N];
void add(int u, int p, int val, int av = 0)
{
cur -= (cnt[a[u]] == sum[a[u]] || cnt[a[u]] == 0);
cnt[a[u]] += val;
cur += (cnt[a[u]] == sum[a[u]] || cnt[a[u]] == 0);
for (int &v : adj[u])
if (v != p && v != av)
add(v, u, val, av);
}
int DFS_1(int u, int p = 0)
{
sub[u] = 1;
for (int &v : adj[u])
if (v != p)
sub[u] += DFS_1(v, u);
return sub[u];
}
bool DFS_2(int u, int p, bool big)
{
int mc = 0;
for (int &v : adj[u])
if (v != p && sub[v] > sub[mc])
mc = v;
for (int &v : adj[u])
if (v != p && v != mc)
{
if (DFS_2(v, u, false))
f[u]++;
else
f[u] += f[v];
}
if (mc != 0)
{
if (DFS_2(mc, u, true))
f[u]++;
else
f[u] += f[v];
}
add(u, p, 1, mc);
if (cur == 0)
ans += (f[u] == (u == 1));
if (!big)
add(u, p, -1);
return cur == 0;
}
int main()
{
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
cin >> n >> k;
for (int i = 1; i < n; i++)
{
cin >> u >> v;
adj[u].push_back(v);
adj[v].push_back(u);
}
for (int i = 1; i <= n; i++)
{
cin >> a[i];
sum[a[i]]++;
}
DFS_1(1);
DFS_2(1, 0, true);
cout << (ans + 1) / 2;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
12 ms |
12152 KB |
Output is correct |
2 |
Incorrect |
13 ms |
12152 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
12 ms |
12152 KB |
Output is correct |
2 |
Incorrect |
13 ms |
12152 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
12 ms |
12152 KB |
Output is correct |
2 |
Incorrect |
13 ms |
12152 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
68 ms |
16364 KB |
Output is correct |
2 |
Correct |
78 ms |
17304 KB |
Output is correct |
3 |
Incorrect |
14 ms |
12280 KB |
Output isn't correct |
4 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
12 ms |
12152 KB |
Output is correct |
2 |
Incorrect |
13 ms |
12152 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |