Compound 2798 Qtree3

A node tree as black or white, the original is white. There are two modes of operation 

1 will change color a node
2 inquiry from the root to the node u first black dots above the path, not the output of -1
the Input
the In The First Line there are integers N and Q. TWO
the In The Next N-1 Lines DESCRIBE The Edges in The Tree: A Line with TWO integers ab & Denotes AN Edge
BETWEEN A and B of The Next Q Lines Contain Instructions "0 I" or ". 1 V" (. 1 ≤ I, V ≤ N)..
the Output
the For each ". 1 . V "Operation, Write One Integer Representing ITS Result
the Sample the Input
. 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
the Sample the Output
- . 1
. 8
-1
2
-1

 

 

#include<cstdio>
#include<algorithm>
#include<cstring> 
using namespace std;const int N=200001;
int n,m,sum,cnt,now,pre[N],f[N],nxt[N],h[N],top[N],id[N],size[N],dep[N],ans,maxx,di[N];
struct oo{int a,b,dp,now;bool v;}s[N*2-20000];
void dfs(int x)
{
    size[x]=1;
    for(int i=h[x];i;i=nxt[i])
    {
        if(pre[i]==f[x])continue;
        dep[pre[i]]=dep[x]+1;
        f[pre[i]]=x;
        dfs(pre[i]);
        size[x]+=size[pre[i]];
    }
}
void dfs2(int x,int f)
{
    int k=0;
    id[x]=++cnt;
    di[cnt]=x; //dfs序中第cnt个点是x 
    top[x]=f;
    for(int i=h[x];i;i=nxt[i])
        if(size[pre[i]]>size[k]&&dep[pre[i]]>dep[x])k=pre[i];
    if(!k)return ;
    dfs2(k,f);
    for(int i=h[x];i;i=nxt[i])
        if(dep[pre[i]]>dep[x]&&pre[i]!=k)
            dfs2(pre[i],pre[i]);
}
void ins(int x,int y)
{
    pre[++now]=y;
    nxt[now]=h[x];
    h[x]=now;
}
void build(int x,int l,int r)
{
    s[x].a=l,s[x].b=r;
	s[x].dp=1e9;
    if(l==r)
	 {
			return ;
	 }
    build(x<<1,l,l+r>>1);
    build(x<<1|1,(l+r>>1)+1,r);
}
void get(int x,int l,int r)
//求出线段树上[l,r] which point is black, and the minimum depth of 
{ 
    IF (S [X] II.A> L = R & lt &&> = S [X] .B) 
    { 
        IF (S [X] .dp <Maxx) 
        { 
            Maxx = S [X] .dp; // find the minimum depth 
            sum = s [x] .now; // point corresponding 
        } 
        return; 
    } 
    the else 
    { 
        int MID = S [X] + II.A S [X] .B >>. 1; 
        IF (L <= MID) GET (<<. 1 X, L, R & lt); 
        IF (R & lt> MID) GET (X <<. 1 |. 1, L, R & lt); 
    } 
} 
void Qmax (int X, Y int) 
{ 
    Maxx = 1E9, SUM = -1; 
    the while (! Top [X] = Top [Y]) 
    { 
        IF (DEP [Top [X]] <DEP [Top [Y] ]) 
		   the swap (X, Y); 
        GET (. 1, ID [Top [X]], ID [X]); 
        X = F [Top [X]];
    } 
    IF (ID [X]> ID [Y]) 
	    the swap (X, Y); 
    GET (. 1, ID [X], ID [Y]); 
    IF (SUM == 0) 
	   Change (<<. 1 X, L);
	   -1 = SUM; 
} 
void Change (X int, int L) 
// in which X means a current tree 
// l refers to a segment in which position 
{ 
    IF (S [X] II.A == S [X] .B ) 
    { 
        s [X] = ^ .v. 1; 
        IF (s [X] .v) // If colored black 
        { 
            s [X] .dp DEP = [DI [L]]; // determined depth 
            s [ X] .now DI = [l];   
			// DI [l] means a sequence dfs l-th point of the original tree which point 
        } 
        the else // restore the white 
		     s [x] .dp = 1e9, s [x] = 0 .now; 
        return; 
    } 
    int MID = S [X] II.A + S [X] .B >>. 1; 
    IF (L <= MID) 
    { 
        S [X] .dp = S [X <<. 1] .dp;
    else 
	    change(x<<1|1,l);
    if(s[x<<1].dp<s[x<<1|1].dp)//求出最小的深度 
        s[x].now=s[x<<1].now;
    }
    else
    {
        s[x].dp=s[x<<1|1].dp;
        s[x].now=s[x<<1|1].now;
    }
}
int main()
{
    scanf("%d%d",&n,&m);
    for(int i=1,x,y;i<n;i++)
    {
        scanf("%d%d",&x,&y);
        ins(x,y);
		ins(y,x);
    }
    dfs(1);
	dfs2(1,1); 
    build(1,1,n);
    int c;
    for(int i=1;i<=m;i++)
    {
        int a,b;
        IF (C) // query operation
        Scanf ( "% D", & C);
        {
            scanf("%d",&b);
            qmax(1,b);
            printf("%d\n",sum);
        } 
        IF (! C) // change the color of a node 
		   Scanf ( "% D", & B), Change (. 1, ID [B]); 
    } 
}

  

 

  

Guess you like

Origin www.cnblogs.com/cutemush/p/11857851.html