Submission #620036

# Submission time Handle Problem Language Result Execution time Memory
620036 2022-08-02T19:40:59 Z AbdelmagedNour Mergers (JOI19_mergers) C++17
0 / 100
67 ms 21756 KB
#include<bits/stdc++.h>
using namespace std;
const int N=500005;
int p[N][20],val[N],dep[N],st[N],id;
vector<vector<int>>adj;
void pre_dfs(int v,int par){
    p[v][0]=par;
    st[v]=id++;
    for(int i=1;i<20;i++)p[v][i]=p[p[v][i-1]][i-1];
    for(auto&u:adj[v]){
        if(u==par)continue;
        dep[u]=dep[v]+1;
        pre_dfs(u,v);
    }
}
void dfs_up(int v,int par){
    for(auto&u:adj[v]){
        if(u==par)continue;
        dfs_up(u,v);
        val[v]+=val[u];
    }
}
int LCA(int u,int v){
    if(u==v)return u;
    if(dep[u]<dep[v])swap(u,v);
    int k=dep[u]-dep[v];
    for(int i=__lg(k+1);i>=0;i--){
        if(k&(1<<i))u=p[u][i];
    }
    if(u==v)return u;
    for(int i=__lg(dep[u]);i>=0;i--){
        if(p[u][i]==p[v][i])continue;
        u=p[u][i];
        v=p[v][i];
    }
    return p[u][0];
}
int cnt(int v,int par){
    int res=0;
    for(auto&u:adj[v]){
        if(u==par)continue;
        res+=cnt(u,v);
    }
    return max(res,(val[v]==0?1:0));
}
int main()
{
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);
    int n,k;
    cin>>n>>k;
    adj.resize(n+1);
    for(int i=1;i<n;i++){
        int u,v;
        cin>>u>>v;
        adj[u].push_back(v);
        adj[v].push_back(u);
    }
    pre_dfs(1,0);
    vector<vector<int>>state(k+1);
    for(int i=1;i<=n;i++){
        int x;
        cin>>x;
        state[x].push_back(i);
    }
    for(int i=1;i<=k;i++){
        sort(state[i].begin(),state[i].end(),[](int i,int j){
                return st[i]<st[j];
             });
        int top=LCA(state[i][0], state[i].back());
		for(auto u:state[i]){
			val[top]--;
			val[u]++;
		}
        /*for(int j=1;j<state[i].size();j++){
            int u=state[i][j-1],v=state[i][j];
            val[u]++;val[v]++;
            val[LCA(u,v)]-=2;
        }*/
    }
    dfs_up(1,0);
    val[1]=1;
    cout<<(cnt(1,0)+1)/2;
    return 0;
}
# Verdict Execution time Memory Grader output
1 Correct 1 ms 340 KB Output is correct
2 Correct 1 ms 340 KB Output is correct
3 Correct 1 ms 328 KB Output is correct
4 Correct 0 ms 340 KB Output is correct
5 Correct 0 ms 340 KB Output is correct
6 Correct 1 ms 332 KB Output is correct
7 Correct 1 ms 340 KB Output is correct
8 Correct 1 ms 340 KB Output is correct
9 Correct 1 ms 340 KB Output is correct
10 Correct 1 ms 332 KB Output is correct
11 Correct 1 ms 340 KB Output is correct
12 Correct 1 ms 340 KB Output is correct
13 Correct 1 ms 328 KB Output is correct
14 Correct 1 ms 340 KB Output is correct
15 Correct 0 ms 324 KB Output is correct
16 Incorrect 1 ms 328 KB Output isn't correct
17 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 1 ms 340 KB Output is correct
2 Correct 1 ms 340 KB Output is correct
3 Correct 1 ms 328 KB Output is correct
4 Correct 0 ms 340 KB Output is correct
5 Correct 0 ms 340 KB Output is correct
6 Correct 1 ms 332 KB Output is correct
7 Correct 1 ms 340 KB Output is correct
8 Correct 1 ms 340 KB Output is correct
9 Correct 1 ms 340 KB Output is correct
10 Correct 1 ms 332 KB Output is correct
11 Correct 1 ms 340 KB Output is correct
12 Correct 1 ms 340 KB Output is correct
13 Correct 1 ms 328 KB Output is correct
14 Correct 1 ms 340 KB Output is correct
15 Correct 0 ms 324 KB Output is correct
16 Incorrect 1 ms 328 KB Output isn't correct
17 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 1 ms 340 KB Output is correct
2 Correct 1 ms 340 KB Output is correct
3 Correct 1 ms 328 KB Output is correct
4 Correct 0 ms 340 KB Output is correct
5 Correct 0 ms 340 KB Output is correct
6 Correct 1 ms 332 KB Output is correct
7 Correct 1 ms 340 KB Output is correct
8 Correct 1 ms 340 KB Output is correct
9 Correct 1 ms 340 KB Output is correct
10 Correct 1 ms 332 KB Output is correct
11 Correct 1 ms 340 KB Output is correct
12 Correct 1 ms 340 KB Output is correct
13 Correct 1 ms 328 KB Output is correct
14 Correct 1 ms 340 KB Output is correct
15 Correct 0 ms 324 KB Output is correct
16 Incorrect 1 ms 328 KB Output isn't correct
17 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 61 ms 16852 KB Output is correct
2 Incorrect 67 ms 21756 KB Output isn't correct
3 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 1 ms 340 KB Output is correct
2 Correct 1 ms 340 KB Output is correct
3 Correct 1 ms 328 KB Output is correct
4 Correct 0 ms 340 KB Output is correct
5 Correct 0 ms 340 KB Output is correct
6 Correct 1 ms 332 KB Output is correct
7 Correct 1 ms 340 KB Output is correct
8 Correct 1 ms 340 KB Output is correct
9 Correct 1 ms 340 KB Output is correct
10 Correct 1 ms 332 KB Output is correct
11 Correct 1 ms 340 KB Output is correct
12 Correct 1 ms 340 KB Output is correct
13 Correct 1 ms 328 KB Output is correct
14 Correct 1 ms 340 KB Output is correct
15 Correct 0 ms 324 KB Output is correct
16 Incorrect 1 ms 328 KB Output isn't correct
17 Halted 0 ms 0 KB -