codeforce 342E Xenia and Tree(分块 + LCA)

本文介绍了一种利用树链剖分和LCA算法解决特定树形结构查询问题的方法。通过分块技术和倍增法实现高效查询最近红色节点及其距离。

题意: 

           一棵树,结点1为红,其他点为蓝。

           操作1:某结点变红;

          操作2:查询离这个点最近的红色结点,输出两点距离。

分析:另一个解法是树链剖分,并不会。。(滚去学一发。。)


我的lca直接是挑战里倍增的模板, 然后分块是达到数量再去更新dp数组(每个结点离红点最近的距离),直接bfs更新,然后查询的时候dp[u]不一定是最近的,因为还有可能block里操作1未更新,用lca算下就好。具体看代码。


#include<iostream>
#include<algorithm>
#include<cstdio>
#include<cmath>
#include<cstring>
#include<string>
#include<vector>
#include<cctype>
#include<set>
#include<map>
#include<queue>
#include<stack>
#include<iomanip>
#include<sstream>
#include<limits>
#define ll long long
#define inf 0x3f3f3f3f
using namespace std;
const ll INF = 1e18;
const int maxn = 2e5+10;
const ll MOD = 1000000007;
const double EPS = 1e-10;
const double Pi = acos(-1.0);

const int MAX_V = 1e5+10;
const int MAX_LOG_V = 18; // 一般20足够     一开始这里是20TLE了  改成18卡过
vector<int> G[MAX_V];
int root;    //根节点编号

int parent[MAX_LOG_V][MAX_V]; //向上走2^k步所到的节点(超过根为-1)
int depth[MAX_V];             //每个节点深度

void dfs(int v,int p, int d)
{
   parent[0][v] = p;
   depth[v] = d;
   for (int i = 0; i < G[v].size();i++)
      if (G[v][i] != p) dfs(G[v][i],v, d+1);
}

void init(int V)
{
   dfs(root, -1, 0);
   for(int k = 0; k +1 < MAX_LOG_V; k++)
     for(int v = 1; v <= V; v++)  //0..n-1  直接修改这里就可以
        if (parent[k][v] < 0) parent[k+1][v] = -1;
        else parent[k+1][v] = parent[k][parent[k][v]];
}

int lca(int u,int v)
{
  if (depth[u] > depth[v]) swap(u,v);
  for(int k = 0; k < MAX_LOG_V; k++)    //u,v走到同一深度
    if ((depth[v] - depth[u]) >> k & 1) v = parent[k][v];

  if (u == v) return u;

   for(int k = MAX_LOG_V-1; k >= 0; k--)  //二分
    if (parent[k][u] != parent[k][v])
       u = parent[k][u], v = parent[k][v];
  return parent[0][u];
}

vector<int>block;
int dp[maxn];    //每个结点离红点最近的距离
void bfs()   //更新dp数组
{
    queue<int> Q;
    for(int i = 0; i < block.size(); i++)
        Q.push(block[i]),dp[block[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);
        }
    }
    block.clear();
}
int main(){
#ifdef LOCAL
	freopen("C:\\Users\\lanjiaming\\Desktop\\acm\\in.txt","r",stdin);
	//freopen("output.txt","w",stdout);
#endif
//ios_base::sync_with_stdio(0);
    int n,m;
    while(scanf("%d%d",&n,&m)!=EOF)
    {
        for(int i = 0; i <= n; i++) G[i].clear();
        for(int i = 1; i < n; i++)
        {
            int u,v;
            scanf("%d%d",&u,&v);
            G[u].push_back(v);
            G[v].push_back(u);
        }
        root = 1;
        init(n);

        for(int i = 1; i <= n; i++) dp[i] = inf;
        block.clear();
        block.push_back(1);
        int mq = sqrt(m);   //块数
        for(int i = 0; i < m; i++)
        {
            int op,u;
            scanf("%d%d",&op,&u);
            if (op == 1)
            {
                block.push_back(u);
                if (block.size()== mq) bfs();
            }else
             {
                 int ans = dp[u];
                 for(int i = 0; i < block.size(); i++)
                 {
                     int v = block[i];
                     ans = min(ans,depth[u]+depth[v]-2*depth[lca(u,v)]);
                 }
                 printf("%d\n",ans);
             }
        }
    }
    return 0;
}


### Codeforces Problem or Contest 998 Information For the specific details on Codeforces problem or contest numbered 998, direct references were not provided within the available citations. However, based on similar structures observed in other contests such as those described where configurations often include constraints like `n` representing numbers of elements with defined ranges[^1], it can be inferred that contest or problem 998 would follow a comparable format. Typically, each Codeforces contest includes several problems labeled from A to F or beyond depending on the round size. Each problem comes with its own set of rules, input/output formats, and constraint descriptions. For instance, some problems specify conditions involving integer inputs for calculations or logical deductions, while others might involve more complex algorithms or data processing tasks[^3]. To find detailed information regarding contest or problem 998 specifically: - Visit the official Codeforces website. - Navigate through past contests until reaching contest 998. - Review individual problem statements under this contest for precise requirements and examples. Additionally, competitive programming platforms usually provide comprehensive documentation alongside community discussions which serve valuable resources when exploring particular challenges or learning algorithmic solutions[^2]. ```cpp // Example C++ code snippet demonstrating how contestants interact with input/output during competitions #include <iostream> using namespace std; int main() { int n; cin >> n; // Process according to problem statement specifics } ```
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值