codeforces 342 E. Xenia and Tree(最短路+分块+lca)

题目链接:https://codeforces.com/contest/342/problem/E

思路:存下红点暴力lca复杂度会炸,所以每sqrt(m)个点用最短路更新一下到各个点距离,保证只和sqrt(m)个点求lca。

#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
using namespace std;
typedef long long ll;
typedef vector VI;
typedef unsigned long long ull;
const ll inff = 0x3f3f3f3f3f3f3f3f;
#define FOR(i,a,b) for(int i(a);i<=(b);++i)
#define FOL(i,a,b) for(int i(a);i>=(b);--i)
#define SZ(x) ((long long)(x).size())
#define REW(a,b) memset(a,b,sizeof(a))
#define inf int(0x3f3f3f3f)
#define si(a) scanf("%d",&a)
#define sl(a) scanf("%lld",&a)
#define sd(a) scanf("%lf",&a)
#define ss(a) scanf("%s",a)
#define mod ll(1e9+7)
#define pb push_back
#define eps 1e-6
#define lc d<<1
#define rc d<<1|1
#define Pll pair
#define P pair
#define pi acos(-1)
int n,m,x,y,fa[200008][20],d[200008],ans,deep[200008];
vectorg[200008],sd;
void dfs(int u,int faa)
{
    for(int i=0;i,greater

> q; for(int i=0;id[v]+1) d[e]=d[v]+1,q.push(P(d[e],e)); } } } int main() { cin.tie(0); cout.tie(0); cin>>n>>m; FOR(i,1,n-1) { si(x),si(y); g[x].pb(y); g[y].pb(x); } REW(d,inf);dfs(1,0),bz(); sd.pb(1);n=sqrt(m); while(m--) { si(x); if(x==1) { si(y),sd.pb(y); if(sd.size()==n) djk(),sd.clear(); } else { si(y);ans=d[y]; for(int i=0;i

 

你可能感兴趣的:(LCA,数据结构,lca,分块)