Submission #128897

# Submission time Handle Problem Language Result Execution time Memory
128897 2019-07-11T10:47:30 Z kuroni Mergers (JOI19_mergers) C++17
0 / 100
99 ms 17264 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] == 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 Incorrect 13 ms 12152 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 13 ms 12152 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 13 ms 12152 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 71 ms 16496 KB Output is correct
2 Incorrect 99 ms 17264 KB Output isn't correct
3 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 13 ms 12152 KB Output isn't correct
2 Halted 0 ms 0 KB -