CF825G Tree Queries

[Title] Italy

A tree has n nodes are initially white, there are two modes of operation:

1. 1 x representative of the node x to black

2. 2 x representing query x the smallest number of nodes on the path to the tree at any of a simple black node number

Input t and Z , wherein t represents the type of operation, X = (Z + Last) MOD + n-1 , L A S t representative of the answer to the query once, initially 0, to ensure that the first operation is a

【data range】

n,q<=1e6.

【answer】

The first operation is guaranteed discovery 1, the first point may be dyed black root, each point on the path to its minimum dis [i] may be pretreated out.

I found a point to all the black dots in the path with the smallest number in the minimum number for each root node and the black point.

code show as below:

 

#include<bits/stdc++.h>
using namespace std;
const int N=1e6+5;
int n,q,last[N],dis[N],size,last1,opt,xx,zx;
struct pigu
{
    int dao,ne;
}a[N<<1];
inline void lingjiebiao(int x,int y)
{
    a[++size].dao=y;
    a[size].ne=last[x];
    last[x]=size;
}
inline void dfs(int now,int fa)
{
    dis[now]=min(dis[now],now);
    for(int i=last[now];i;i=a[i].ne)
    {
        if(a[i].dao==fa) continue;
        dis[a[i].dao]=dis[now];
        dfs(a[i].dao,now);
    }
}
inline int read()
{
    char c=getchar();
    int x=0;
    while(!isdigit(c)) c=getchar();
    while(isdigit(c)) {x=(x<<3)+(x<<1)+c-'0';c=getchar();}
    return x;
}
int main()
{
    memset(dis,0x3f,sizeof(dis));
    n=read();q=read();
    for(int i=1,x,y;i<=n-1;i++)
    {
        x=read();y=read();
        lingjiebiao(x,y);
        lingjiebiao(y,x);
    }
    opt=read();xx=read();
    int rt=xx%n+1;
    dfs(rt,0);
    zx=dis[rt];
    for(int i=1;i<=q-1;i++)
    {
        opt=read();xx=read();
        xx=(last1+xx)%n+1;
        if(opt==1)
        {
            zx=min(zx,dis[xx]);
        }
        else
        {
            cout<<min(zx,dis[xx])<<"\n";
            xx=min(zx,dis[xx]);
            last1=xx;
        }
    }
}
View Code

 

 

 

Guess you like

Origin www.cnblogs.com/betablewaloot/p/12169078.html