답안 #919215

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
919215 2024-01-31T13:05:20 Z 12345678 Mergers (JOI19_mergers) C++17
0 / 100
67 ms 33636 KB
#include <bits/stdc++.h>

using namespace std;

const int nx=5e5+5, kx=19;
int lvl[nx], pa[nx][kx], n, k, u, v, s[nx], l[nx], cnt, dp[nx], ed[nx], ans, t[nx];
vector<int> d[nx];

void dfs(int u, int p)
{
    lvl[u]=lvl[p+1]; pa[u][0]=p;
    for (int i=1; i<kx; i++) pa[u][i]=pa[pa[u][i-1]][i-1];
    for (auto v:d[u]) if (v!=p) dfs(v, u);
}

int lca(int u, int v)
{
    if (u==0) return v;
    if (lvl[u]>lvl[v]) swap(u, v);
    for (int i=kx-1; i>=0; i--) if (lvl[pa[v][i]]>=lvl[u]) v=pa[v][i];
    if (u==v) return u;
    for (int i=kx-1; i>=0; i--) if (pa[u][i]!=pa[v][i]) u=pa[u][i], v=pa[v][i];
    return pa[u][0];
}

void dfs2(int u, int p)
{
    for (auto v:d[u]) if (v!=p) dfs2(v, u), dp[u]+=dp[v];
    if (dp[u]==0) ed[u]=1;
}

void dfs3(int u, int p, int c)
{
    for (auto v:d[u])
    {
        if (v==p) continue;
        if (ed[v]) t[u]++, dfs3(v, u, v), ans+=(t[v]==0);
        else dfs3(v, u, c);
    }
}

int main()
{
    cin.tie(NULL)->sync_with_stdio(false);
    cin>>n>>k;
    for (int i=1; i<n; i++) cin>>u>>v, d[u].push_back(v), d[v].push_back(u);
    for (int i=1; i<=n; i++) cin>>s[i];
    dfs(1, 1);
    for (int i=1; i<=n; i++) l[s[i]]=lca(l[s[i]], i);
    for (int i=1; i<=n; i++) dp[l[s[i]]]--, dp[i]++;
    dfs2(1, 1);
    dfs3(1, 1, 1);
    ans+=(t[1]==1);
    cout<<(ans+1)/2;
}
# 결과 실행 시간 메모리 Grader output
1 Correct 6 ms 21080 KB Output is correct
2 Incorrect 5 ms 21080 KB Output isn't correct
3 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 6 ms 21080 KB Output is correct
2 Incorrect 5 ms 21080 KB Output isn't correct
3 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 6 ms 21080 KB Output is correct
2 Incorrect 5 ms 21080 KB Output isn't correct
3 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 67 ms 33276 KB Output is correct
2 Correct 60 ms 33636 KB Output is correct
3 Incorrect 7 ms 21080 KB Output isn't correct
4 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 6 ms 21080 KB Output is correct
2 Incorrect 5 ms 21080 KB Output isn't correct
3 Halted 0 ms 0 KB -