Xenia the programmer has a tree consisting of n nodes. We will consider the tree nodes indexed from 1 to n. We will also consider the first node to be initially painted red, and the other nodes — to be painted blue.
The distance between two tree nodes v and u is the number of edges in the shortest path between v and u.
Xenia needs to learn how to quickly execute queries of two types:
- paint a specified blue node in red;
- calculate which red node is the closest to the given one and print the shortest distance to the closest red node.
Your task is to write a program which will execute the described queries.
The first line contains two integers n and m (2 ≤ n ≤ 105, 1 ≤ m ≤ 105) — the number of nodes in the tree and the number of queries. Next n - 1 lines contain the tree edges, the i-th line contains a pair of integers ai, bi (1 ≤ ai, bi ≤ n, ai ≠ bi) — an edge of the tree.
Next m lines contain queries. Each query is specified as a pair of integers ti, vi (1 ≤ ti ≤ 2, 1 ≤ vi ≤ n). If ti = 1, then as a reply to the query we need to paint a blue node vi in red. If ti = 2, then we should reply to the query by printing the shortest distance from some red node to node vi.
It is guaranteed that the given graph is a tree and that all queries are correct.
For each second type query print the reply in a single line.
题目大意:给定一棵树,第一个结点初始为红色,其余点为蓝色,现在有m个询问,输入a,b,当a=1时,把结点b涂为红色,a=2时,求离
结点b最近的红色结点与b点的距离。
题意分析:简单分析一波发现可以用vector存储所有红色结点,然后对于询问2直接暴力LCA就有解了,这样做显示是会TLE的,看了官方题解后 发现可以很巧妙地对询问进行分块,即sqrt(m)*n做法。对于询问1,可以直接把修改结点Push_back进vector,直到vector.size()==sqrt(m)后直接对vector所有点进行bfs,用d[i]记录与i点最近的红色的结点的距离,只有当d[i]发现更新(有贡献)时才把结点i压入队列中。这样做最坏情况下是O(m*n)的,但由于有限制地把结点压入队列,很显然是到不了最坏情况的,注意bfs后对vector进行clear。当vector中有点但vector.size()<sqrt(m)时,发生询问2时直接暴力把vector中的点与询问点LCA即可,显然时间爆不了
#include<bits/stdc++.h>
using namespace std;
const int maxn = 1e5+100,Log =20;
int n,m,color[maxn],dp[maxn],dis[maxn];
int par[maxn][Log],dep[maxn];
vector<int> G[maxn],ac;
void Dep(int u,int fa,int dep){
dis[u] = dep;
for(int i=0;i<G[u].size();i++){
int v =G[u][i];
if(v==fa)
continue;
Dep(v,u,dep+1);
}
}
void LcaDfs(int u,int fa,int d){
dep[u] = d;
if(u==1){
for(int i = 0;i<Log;i++)
par[u][i] = u;
}
else{
par[u][0] = fa;
for(int i=1;i<Log;i++)
par[u][i] = par[par[u][i-1]][i-1];
}
for(int i=0;i<G[u].size();i++){
int v = G[u][i];
if(v==fa)
continue;
LcaDfs(v,u,d+1);
}
}
int up(int x,int step){
for(int i=0;i<Log;i++)
if(step&(1<<i))
x = par[x][i];
return x;
}
int Lca(int u,int v){
if(dep[u]<dep[v])
swap(u,v);
u = up(u,dep[u]-dep[v]);
if(u==v)
return u;
for(int i=Log-1;i>=0;i--)
if(par[u][i]!=par[v][i]){
u = par[u][i];
v = par[v][i];
}
return par[u][0];
}
void bfs(){
queue<int> Q;
for(int i=0;i<ac.size();i++){
Q.push(ac[i]);
dp[ac[i]]=0;
}
while(!Q.empty()){
int u = Q.front();
Q.pop();
for(int i=0;i<G[u].size();i++){
int v = G[u][i];
if(dp[v]>dp[u]+1){
dp[v] = dp[u]+1;
Q.push(v);
}
}
}
ac.clear();
}
int main(){
scanf("%d%d",&n,&m);
for(int i=1;i<=n;i++)
dp[i] = maxn*10;
for(int i=0;i<n-1;i++){
int a,b;
scanf("%d%d",&a,&b);
G[a].push_back(b);
G[b].push_back(a);
}
ac.push_back(1);
bfs();
Dep(1,0,0);
LcaDfs(1,0,1);
int block = sqrt(m);
while(m--){
int op,a;
scanf("%d",&op);
if(op==1){
scanf("%d",&a);
ac.push_back(a);
if(ac.size()==block)
bfs();
}
else{
scanf("%d",&a);
int ans = dp[a];
for(int i=0;i<ac.size();i++){
ans = min(ans,dis[a]+dis[ac[i]]-2*dis[Lca(a,ac[i])]);
}
printf("%d\n",ans);
}
}
return 0;
}