Submission #86784

# Submission time Handle Problem Language Result Execution time Memory
86784 2018-11-27T12:35:19 Z duy_tran Synchronization (JOI13_synchronization) C++14
0 / 100
524 ms 15892 KB
#include<bits/stdc++.h>
using namespace std;

typedef pair<int,int> pii;
const int maxn=(int)1e5+50;
int n,m,q,in[maxn],out[maxn],pos[maxn],cnt,on[maxn],res[maxn],last[maxn];
vector<int> a[maxn];
pii ed[maxn];
int L[4*maxn],R[4*maxn],maxx[4*maxn],leaf[maxn];

void DFS(int u)
{
    in[u]=++cnt;
    pos[cnt]=u;

    for(int v:a[u])
        if(!in[v])DFS(v);

    out[u]=cnt;
}

void START()
{
    cin>>n>>m>>q;

    for(int i=1;i<n;++i)
    {
        cin>>ed[i].first>>ed[i].second;

        a[ed[i].first].push_back(ed[i].second);
        a[ed[i].second].push_back(ed[i].first);
    }

    cnt=0;
    DFS(1);

    for(int i=1;i<=n;++i)res[i]=1;
}

void BuildTree(int id,int l,int r)
{
    L[id]=l;
    R[id]=r;

    if(l==r)
    {
        maxx[id]=out[pos[l]];
        leaf[l]=id;
        return;
    }

    int mid=(l+r)/2;

    BuildTree(id*2,l,mid);
    BuildTree(id*2+1,mid+1,r);

    maxx[id]=max(maxx[id*2],maxx[id*2+1]);
}

void Up(int id,int gt)
{
    maxx[id]=gt;
    id/=2;

    while(id>0)
    {
        maxx[id]=max(maxx[id*2],maxx[id*2+1]);
        id/=2;
    }
}

int Get(int id,int u,int p)
{
    if(L[id]>u || maxx[id]<p)return 0;

    if(L[id]==R[id])return pos[L[id]];

    int p2=Get(id*2+1,u,p);

    if(p2)return p2;

    return Get(id*2,u,p);
}

void NEXT()
{
    BuildTree(1,1,n);

    for(int i=1;i<=m;++i)
    {
        int id;
        cin>>id;

        int u=ed[id].first;
        int v=ed[id].second;

        if(in[u]>in[v])swap(u,v);

        int root=Get(1,in[u],out[u]);

        //cout<<u<<" "<<v<<" "<<root<<'\n';

        if(!on[id])
        {
            on[id]=1;

            res[root]+=res[v]-last[v];
            Up(leaf[in[v]],0);
        }

        else
        {
            on[id]=1;

            res[v]=last[v]=res[root];

            Up(leaf[in[v]],out[v]);
        }
    }

    for(int i=1;i<=q;++i)
    {
        int u;
        cin>>u;

        int root=Get(1,in[u],out[u]);

        cout<<res[root]<<'\n';
    }
}

int main()
{
    START();
    NEXT();
}
# Verdict Execution time Memory Grader output
1 Correct 4 ms 2680 KB Output is correct
2 Correct 4 ms 2936 KB Output is correct
3 Incorrect 4 ms 2936 KB Output isn't correct
4 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 194 ms 14116 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 4 ms 14116 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 524 ms 15892 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 4 ms 15892 KB Output is correct
2 Incorrect 4 ms 15892 KB Output isn't correct
3 Halted 0 ms 0 KB -