Submission #1349198

#TimeUsernameProblemLanguageResultExecution timeMemory
1349198iamhereforfunMergers (JOI19_mergers)C++20
0 / 100
43 ms19648 KiB
// Starcraft 2 enjoyer //

#include <bits/stdc++.h>

// #pragma GCC target("avx2")
// #pragma GCC optimize("O3")
// #pragma GCC optimize("unroll-loops")

using namespace std;

#define LSOne(X) ((X) & -(X))

const int N = 5e5 + 5;
const int M = 1 << 5;
const int K = 19;
const int LG = 20;
const long long INF = 3e18;
const int C = 26;
const int B = 1000;
const int MOD = 1e9 + 7;

int n, k, sz[N], p[N], par[N], st[N], en[N], cid, num, tot[N];
vector<int> adj[N], state[N], thing[N];
bool gud[N];

void dfs1(int c, int p)
{
    sz[c] = 1;
    par[c] = p;
    st[c] = cid++;
    for (int i : adj[c])
    {
        if (i == p)
            continue;
        dfs1(i, c);
        sz[c] += sz[i];
    }
    en[c] = cid - 1;
}

void dfs2(int c, int p)
{
    int cnt = 0;
    for (int i : adj[c])
    {
        if (i == p)
            continue;
        dfs2(i, c);
        gud[c] |= gud[i];
        cnt += gud[i];
        tot[c] += tot[i];
    }
    for (int i : thing[c])
        tot[c] += state[i].size();
    if (!gud[c] && c != 1)
    {
        if (tot[c] == sz[c])
        {
            num++;
            gud[c] = 1;
        }
    }
    if (c == 1)
    {
        if (cnt == 1 && adj[c].size() > 1)
            num++;
    }
    // cout << c << " " << gud[c] << "\n";
}

bool in(int a, int b)
{
    return st[a] <= st[b] && en[b] <= en[a];
}

int parent(int i)
{
    return p[i] == i ? i : p[i] = parent(p[i]);
}

inline void solve()
{
    cin >> n >> k;
    for (int x = 0; x < n - 1; x++)
    {
        int a, b;
        cin >> a >> b;
        adj[a].push_back(b);
        adj[b].push_back(a);
    }
    for (int x = 1; x <= n; x++)
    {
        int i;
        cin >> i;
        state[i].push_back(x);
        p[x] = x;
    }
    cid = 1;
    dfs1(1, 0);
    for (int x = 1; x <= k; x++)
    {
        for (int y = 0; y < state[x].size() - 1; y++)
        {
            int i = state[x][y], j = state[x][y + 1];
            while (!in(i, j))
            {
                int k = parent(par[i]);
                p[i] = k;
                i = k;
            }
            while (!in(j, i))
            {
                int k = parent(par[j]);
                p[j] = k;
                j = k;
            }
        }
        thing[parent(state[x].back())].push_back(x);
    }
    num = 0;
    dfs2(1, 0);
    // cout << num << "\n";
    cout << (num + 1) / 2 << "\n";
}

signed main()
{
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);
    int t = 1;
    // cin >> t;
    for (int x = 1; x <= t; x++)
    {
        solve();
    }
    return 0;
}
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...