Submission #786610

# Submission time Handle Problem Language Result Execution time Memory
786610 2023-07-18T10:03:05 Z winter0101 Mergers (JOI19_mergers) C++14
0 / 100
58 ms 26676 KB
#include<bits/stdc++.h>
using namespace std;
#define all(fl) fl.begin(),fl.end()
#define pb push_back
#define fi first
#define se second
#define for1(i,j,k) for(int i=j;i<=k;i++)
#define for2(i,j,k) for(int i=j;i>=k;i--)
#define for3(i,j,k,l) for(int i=j;i<=k;i+=l)
#define lb lower_bound
#define ub upper_bound
#define sz(a) (int)a.size()
#define pii pair<int,int>
#define pli pair<long long,int>
#define gcd __gcd
#define lcm(x,y) x*y/__gcd(x,y)
#define pil pair<int,long long>
const int maxn=5e5+9;
int st[maxn][20];
int dep[maxn];
vector<int>a[maxn];
int s[maxn];
int last[maxn];
int anc[maxn];
int ans=0;
void dfs(int u,int par){
for (auto v:a[u]){
    if (v==par)continue;
    dep[v]=dep[u]+1;
    st[v][0]=u;
    for1(i,1,19)st[v][i]=st[st[v][i-1]][i-1];
    dfs(v,u);
}
}
int lca(int u,int v){
if (u==0)return v;
if (v==0)return u;
if (dep[u]<dep[v])swap(u,v);
int k=dep[u]-dep[v];
for1(i,0,19){
if (k>>i&1)u=st[u][i];
}
if(u==v)return u;
for2(i,19,0){
if (!st[u][i]||!st[v][i])continue;
if (st[u][i]!=st[v][i]){
    u=st[u][i];
    v=st[v][i];
}
}
return st[u][0];
}

void redfs(int u,int par){
anc[u]=last[s[u]];
for (auto v:a[u]){
    if (v==par)continue;
    redfs(v,u);
    anc[u]=lca(anc[u],anc[v]);
}
if (anc[u]==u&&u!=1)ans++;
}
signed main(){
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    //freopen("temp.INP","r",stdin);
    //freopen("temp.OUT","w",stdout);
    int n,k;
    cin>>n>>k;
    for1(i,1,n-1){
    int u,v;
    cin>>u>>v;
    a[u].pb(v);
    a[v].pb(u);
    }
    dfs(1,0);
    for1(i,1,n){
    cin>>s[i];
    last[s[i]]=lca(last[s[i]],i);
    }
    redfs(1,0);
    cout<<(ans+1)/2;
}
# Verdict Execution time Memory Grader output
1 Correct 6 ms 12076 KB Output is correct
2 Correct 5 ms 12116 KB Output is correct
3 Correct 5 ms 12116 KB Output is correct
4 Correct 5 ms 11988 KB Output is correct
5 Correct 5 ms 12076 KB Output is correct
6 Correct 5 ms 12076 KB Output is correct
7 Correct 5 ms 11988 KB Output is correct
8 Correct 5 ms 12076 KB Output is correct
9 Incorrect 5 ms 12072 KB Output isn't correct
10 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 6 ms 12076 KB Output is correct
2 Correct 5 ms 12116 KB Output is correct
3 Correct 5 ms 12116 KB Output is correct
4 Correct 5 ms 11988 KB Output is correct
5 Correct 5 ms 12076 KB Output is correct
6 Correct 5 ms 12076 KB Output is correct
7 Correct 5 ms 11988 KB Output is correct
8 Correct 5 ms 12076 KB Output is correct
9 Incorrect 5 ms 12072 KB Output isn't correct
10 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 6 ms 12076 KB Output is correct
2 Correct 5 ms 12116 KB Output is correct
3 Correct 5 ms 12116 KB Output is correct
4 Correct 5 ms 11988 KB Output is correct
5 Correct 5 ms 12076 KB Output is correct
6 Correct 5 ms 12076 KB Output is correct
7 Correct 5 ms 11988 KB Output is correct
8 Correct 5 ms 12076 KB Output is correct
9 Incorrect 5 ms 12072 KB Output isn't correct
10 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 58 ms 26056 KB Output is correct
2 Correct 57 ms 26676 KB Output is correct
3 Incorrect 8 ms 12472 KB Output isn't correct
4 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 6 ms 12076 KB Output is correct
2 Correct 5 ms 12116 KB Output is correct
3 Correct 5 ms 12116 KB Output is correct
4 Correct 5 ms 11988 KB Output is correct
5 Correct 5 ms 12076 KB Output is correct
6 Correct 5 ms 12076 KB Output is correct
7 Correct 5 ms 11988 KB Output is correct
8 Correct 5 ms 12076 KB Output is correct
9 Incorrect 5 ms 12072 KB Output isn't correct
10 Halted 0 ms 0 KB -