链接
题解
这题可真是神奇
对询问进行分块,影响到当前询问的修改要么在当前块内,要么在当前块外
块内的直接暴力
每处理完一个块就把这根号个修改的影响通过一次 b f s bfs bfs传递给整个树
那么块外的也就可以直接查询了
代码
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#define iinf 0x3f3f3f3f
#define linf (1ll<<60)
#define eps 1e-8
#define maxn 100010
#define maxe 200010
#define maxk 17
#define cl(x) memset(x,0,sizeof(x))
#define rep(_,__) for(_=1;_<=(__);_++)
#define em(x) emplace(x)
#define emb(x) emplace_back(x)
#define emf(x) emplace_front(x)
using namespace std;
using namespace __gnu_pbds;
typedef long long ll;
typedef pair<int,int> pii;
typedef pair<ll,ll> pll;
ll read(ll x=0)
{
ll c, f(1);
for(c=getchar();!isdigit(c);c=getchar())if(c=='-')f=-f;
for(;isdigit(c);c=getchar())x=x*10+c-0x30;
return f*x;
}
struct Graph
{
int etot, head[maxn], to[maxe], next[maxe], w[maxe];
void clear(int N)
{
for(int i=1;i<=N;i++)head[i]=0;
etot=0;
}
void adde(int a, int b, int c=0){to[++etot]=b;w[etot]=c;next[etot]=head[a];head[a]=etot;}
}G;
struct Doubling_LCA
{
int f[maxn][maxk+2], depth[maxn];
void clear(int n){for(int i=1;i<=n;i++)depth[i]=0, cl(f[i]);}
void dfs(Graph &G, int pos, int pre)
{
for(auto k=1;(1<<k)<=depth[pos];k++)f[pos][k]=f[f[pos][k-1]][k-1];
for(auto p(G.head[pos]);p;p=G.next[p])
if(G.to[p]!=pre)
{
f[G.to[p]][0]=pos;
depth[G.to[p]]=depth[pos]+1;
dfs(G,G.to[p],pos);
}
}
void run(Graph &G, int root)
{
depth[root]=1;
dfs(G,root,0);
}
int q(int x, int y)
{
if(depth[x]<depth[y])swap(x,y);
for(auto k(maxk);~k;k--)
if(depth[f[x][k]]>=depth[y])
x=f[x][k];
if(x==y)return x;
for(auto k(maxk);~k;k--)
if(f[x][k]!=f[y][k])
x=f[x][k], y=f[y][k];
return f[x][0];
}
}db;
int n, m, mark[maxn], d[maxn], sz=320;
int main()
{
int u, v, i, j, x, y;
n=read(), m=read();
rep(i,n-1)
{
u=read(), v=read();
G.adde(u,v), G.adde(v,u);
}
db.run(G,1);
rep(i,n)d[i]=db.depth[i]-1;
for(i=1;i<=m;i+=sz)
{
deque<int> q;
for(j=0;j<sz and i+j<=m;j++)
{
x=read(), y=read();
if(x==1)q.emb(y);
else
{
int ans=d[y];
for(auto id:q)
ans=min(ans,db.depth[id]+db.depth[y]-2*db.depth[db.q(y,id)]);
printf("%d\n",ans);
}
}
for(auto id:q)d[id]=0;
while(!q.empty())
{
auto x=q.front(); q.pop_front();
for(auto p=G.head[x];p;p=G.next[p])
if(d[G.to[p]]>d[x]+1)
{
d[G.to[p]]=d[x]+1;
q.emb(G.to[p]);
}
}
}
return 0;
}