Luogu P4116 Qtree3 [tree chain division]

Topic description

Given a tree with N points (N-1 edges), the nodes are white and black, and the initial is all white
There are two operations:
0 i : Change the color of a point (the original black becomes white, the original is White becomes black)
1 v : ask for the first black point on the path from 1 to v, if not, output -1

Input format:

The first line N, Q, representing N points and Q operations,
the second line to the Nth line N-1 undirected edges
and then Q lines, each line is an operation "0 i" or "1 v" (1 ≤ i, v ≤ N).

Output format:

output result for each 1 v operation

input sample

9 8
1 2
1 3
2 4
2 9
5 9
7 9
8 9
6 8
1 3
0 8
1 6
1 7
0 2
1 9
0 2
1 9

Sample output

-1
8
-1
2
-1

illustrate

For 1/3 of the test cases, N=5000, Q=400000.
For 1/3 of the test cases, N=10000, Q=300000.
For 1/3 of the test cases, N=100000, Q=100000.
***************************

Topic Analysis:

Use the point weight of 0 to represent white, and the point weight of 1 to represent black
. After the tree is cut,
find the point weight closest to node 1 and the chain from u to top[u] that is >=1.
Let ll=num[ top[ u] ], rr=num[u]
Then in this interval, binary search for the point with the smallest num value and the weight of the point with the weight of 1 output
****************

#include<iostream>
#include<vector>
#include<algorithm>
#include<queue>
#include<cstring>
#include<cstdio>
using namespace std;
 
int read()
{
    int f=1,x=0;
    char ss=getchar();
    while(ss<'0'||ss>'9'){if(ss=='-')f=-1;ss=getchar();}
    while(ss>='0'&&ss<='9'){x=x*10+ss-'0';ss=getchar();}
    return f*x;
}

int n,m;
struct node{int v,nxt;}E[300010];
int head[200010],tot;
int dep[200010],fa[200010],son[200010],size[200010];
int top[200010],num[500010],pos[500010],cnt;
int sum[500010];

void add(int u,int v)
{
    E[++tot].nxt=head[u];
    E[tot].v=v;
    head[u]=tot;
} 

void dfs1(int u,int pa)
{
    size[u]=1;
    for(int i=head[u];i;i=E[i].nxt)
    {
        int v=E[i].v;
        if(v==pa) continue;
        dep[v]=dep[u]+1; fa[v]=u;
        dfs1(v,u);
        size[u]+=size[v];
        if(size[v]>size[son[u]]) son[u]=v;
    }
}

void dfs2(int u,int tp) 
{
    top[u]=tp; num[u]=++cnt; pos[cnt]=u;
    if(son[u]) dfs2(son[u],tp);
    for(int i=head[u];i;i=E[i].nxt)
    {
        int v=E[i].v;
        if(v==fa[u]||v==son[u]) continue;
        dfs2(v,v);
    }
}

void update(int u,int s,int t,int p)
{
    if(s==t){sum[p]^=1; return;}//修改颜色直接取反
    int mid=s+t>>1;
    if(u<=mid) update(u,s,mid,p<<1);
    else update(u,mid+1,t,p<<1|1);
    sum[p]=sum[p<<1]+sum[p<<1|1];
}

int get(int ll,int rr,int s,int t,int p)
{
    if(ll<=s&&t<=rr) return sum[p];
    int mid=s+t>>1;
    int ans=0;
    if(ll<=mid) ans+=get(ll,rr,s,mid,p<<1);
    if(rr>mid) ans+=get(ll,rr,mid+1,t,p<<1|1);
    return ans; 
}

int check(int ll,int rr)
{
    if(ll==rr) return ll;
    int mid=ll+rr>>1;
    int tp=get(ll,mid,1,n,1);
    if(tp>=1) return check(ll,mid);//尽量靠近1号点
    else return check(mid+1,rr);
} 

int query(int u)
{
    int ll=0,rr=0;
    while(top[u]!=1)
    {
        int tp=get(num[top[u]],num[u],1,n,1);
        if(tp>=1) ll=num[top[u]],rr=num[u];//找到一个权值和>=1的区间先记录
        u=fa[top[u]];
    }
    int tp=get(1,num[u],1,n,1);//以1号点为链顶的最后一条链
    if(tp>=1) ll=1,rr=num[u];
    
    if(ll==0&&rr==0) return -1;//没有黑点
    else return pos[check(ll,rr)];//二分查找
} 

int main()
{
    n=read();m=read();
    for(int i=1;i<n;++i)
    {
        int u=read(),v=read();
        add(u,v);add(v,u);
    }
    
    dep[1]=1;
    dfs1(1,0);dfs2(1,1);
    
    while(m--)
    {
        int k=read(),u=read();
        if(k==0) update(num[u],1,n,1);
        else if(k==1) printf("%d\n",query(u));
    }
    return 0;
}

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324846187&siteId=291194637