Submission #128732

# Submission time Handle Problem Language Result Execution time Memory
128732 2019-07-11T08:45:45 Z kuroni Mergers (JOI19_mergers) C++17
0 / 100
86 ms 18996 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];
int cur = 0, sum[K], cnt[K];
bool f[N];
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)
        {
            bool ch = DFS_2(v, u, false);
            if (ch && f[u])
                ans--;
            f[u] ^= ch;
        }
    if (mc != 0)
    {
        bool ch = DFS_2(mc, u, true);
        if (ch && f[u])
            ans--;
        f[u] ^= ch;
    }
    add(u, p, 1, mc);
    if (cur == 0 && u != 1)
    {
        if (!f[u])
            ans++;
        f[u] = true;
    }
    if (!big)
        add(u, p, -1);
    return f[u];
}

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;
}
# Verdict Execution time Memory Grader output
1 Correct 12 ms 12152 KB Output is correct
2 Correct 12 ms 12152 KB Output is correct
3 Correct 12 ms 12152 KB Output is correct
4 Correct 12 ms 12152 KB Output is correct
5 Correct 12 ms 12152 KB Output is correct
6 Correct 12 ms 12152 KB Output is correct
7 Correct 12 ms 12152 KB Output is correct
8 Correct 12 ms 12152 KB Output is correct
9 Correct 12 ms 12152 KB Output is correct
10 Correct 12 ms 12152 KB Output is correct
11 Correct 12 ms 12152 KB Output is correct
12 Incorrect 12 ms 12152 KB Output isn't correct
13 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 12 ms 12152 KB Output is correct
2 Correct 12 ms 12152 KB Output is correct
3 Correct 12 ms 12152 KB Output is correct
4 Correct 12 ms 12152 KB Output is correct
5 Correct 12 ms 12152 KB Output is correct
6 Correct 12 ms 12152 KB Output is correct
7 Correct 12 ms 12152 KB Output is correct
8 Correct 12 ms 12152 KB Output is correct
9 Correct 12 ms 12152 KB Output is correct
10 Correct 12 ms 12152 KB Output is correct
11 Correct 12 ms 12152 KB Output is correct
12 Incorrect 12 ms 12152 KB Output isn't correct
13 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 12 ms 12152 KB Output is correct
2 Correct 12 ms 12152 KB Output is correct
3 Correct 12 ms 12152 KB Output is correct
4 Correct 12 ms 12152 KB Output is correct
5 Correct 12 ms 12152 KB Output is correct
6 Correct 12 ms 12152 KB Output is correct
7 Correct 12 ms 12152 KB Output is correct
8 Correct 12 ms 12152 KB Output is correct
9 Correct 12 ms 12152 KB Output is correct
10 Correct 12 ms 12152 KB Output is correct
11 Correct 12 ms 12152 KB Output is correct
12 Incorrect 12 ms 12152 KB Output isn't correct
13 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 86 ms 17816 KB Output is correct
2 Correct 77 ms 18996 KB Output is correct
3 Incorrect 15 ms 12252 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 Correct 12 ms 12152 KB Output is correct
3 Correct 12 ms 12152 KB Output is correct
4 Correct 12 ms 12152 KB Output is correct
5 Correct 12 ms 12152 KB Output is correct
6 Correct 12 ms 12152 KB Output is correct
7 Correct 12 ms 12152 KB Output is correct
8 Correct 12 ms 12152 KB Output is correct
9 Correct 12 ms 12152 KB Output is correct
10 Correct 12 ms 12152 KB Output is correct
11 Correct 12 ms 12152 KB Output is correct
12 Incorrect 12 ms 12152 KB Output isn't correct
13 Halted 0 ms 0 KB -