http://codeforces.com/contest/342/problem/E

如果把询问1存起来,每到sqrt(m)的时候再处理一次。

那么总复杂度就是msqrt(m)的。

把要变颜色的节点存起来,可以同时一次O(n)的bfs

然后就是LCA了。LCA需要倍增的做法。这题真的是个好题。。

#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <assert.h>
#define IOS ios::sync_with_stdio(false)
using namespace std;
#define inf (0x3f3f3f3f)
typedef long long int LL; #include <iostream>
#include <sstream>
#include <vector>
#include <set>
#include <map>
#include <queue>
#include <string>
const int maxn = 2e5 + ;
int col[maxn];
struct node {
int u, v;
int tonext;
}e[maxn];
int first[maxn];
int num;
void add(int u, int v) {
++num;
e[num].u = u;
e[num].v = v;
e[num].tonext = first[u];
first[u] = num;
}
int tot[maxn];
int lentot;
int dp[maxn];
struct bfsnode {
int cur, cnt;
bfsnode(int a, int b) : cur(a), cnt(b) {}
};
queue<struct bfsnode>que;
bool vis[maxn];
void bfs() {
memset(vis, false, sizeof vis);
for (int i = ; i <= lentot; ++i) {
que.push(bfsnode(tot[i], ));
dp[tot[i]] = ;
}
while (!que.empty()) {
struct bfsnode t = que.front();
que.pop();
for (int i = first[t.cur]; i; i = e[i].tonext) {
int v = e[i].v;
if (vis[v]) continue;
if (dp[v] <= t.cnt + ) continue;
vis[v] = true;
dp[v] = t.cnt + ;
que.push(bfsnode(v, t.cnt + ));
}
}
}
int ansc[maxn][], deep[maxn], fa[maxn];
void init_LCA(int cur) {
ansc[cur][] = fa[cur]; //跳1步,那么祖先就是爸爸
for (int i = ; i <= ; ++i) { //倍增思路,递归处理
ansc[cur][i] = ansc[ansc[cur][i - ]][i - ];
}
for (int i = first[cur]; i; i = e[i].tonext) {
int v = e[i].v;
if (v == fa[cur]) continue;
fa[v] = cur;
deep[v] = deep[cur] + ;
init_LCA(v);
}
}
int LCA(int x, int y) {
if (deep[x] < deep[y]) swap(x, y); //需要x是最深的
for (int i = ; i >= ; --i) { //从大到小枚举,因为小的更灵活
if (deep[ansc[x][i]] >= deep[y]) { //深度相同,走进去就对了。
x = ansc[x][i];
}
}
if (x == y) return x;
for (int i = ; i >= ; --i) {
if (ansc[x][i] != ansc[y][i]) { //走到第一个不等的地方,
x = ansc[x][i];
y = ansc[y][i];
}
}
return ansc[x][]; //再跳一步就是答案
}
int dis[maxn];
void dfs(int cur, int step) {
dis[cur] = min(dis[cur], step);
for (int i = first[cur]; i; i = e[i].tonext) {
int v = e[i].v;
if (vis[v]) continue;
vis[v] = true;
dfs(v, step + );
}
}
void work() {
int n, m;
scanf("%d%d", &n, &m);
for (int i = ; i <= n - ; ++i) {
int u, v;
scanf("%d%d", &u, &v);
add(u, v);
add(v, u);
} memset(dis, 0x3f, sizeof dis);
dis[] = ;
vis[] = true;
dfs(, ); memset(dp, 0x3f, sizeof dp);
col[] = ;
tot[++lentot] = ;
bfs();
lentot = ; fa[] = ;
deep[] = ;
init_LCA(); int magic = (int)sqrt(m);
for (int i = ; i <= m; ++i) {
int flag, which;
scanf("%d%d", &flag, &which);
if (flag == ) {
tot[++lentot] = which;
} else {
int ans = dp[which];
for (int i = ; i <= lentot; ++i) {
int haha = LCA(which, tot[i]);
ans = min(ans, dis[which] + dis[tot[i]] - * dis[haha]);
}
printf("%d\n", ans);
}
if (lentot >= magic) {
bfs();
lentot = ;
}
}
} int main() {
#ifdef local
freopen("data.txt", "r", stdin);
// freopen("data.txt", "w", stdout);
#endif
work();
return ;
}

LCA是用在树中的,图的不行

LCA[u][v]表示节点u和v的最近公共祖先,也是深度最大祖先,深度越大的话,证明离u和v越近嘛。这个算法基于dfs的回溯和并查集实现。dfs的时候,搜索到叶子节点(没有儿子)的时候,得到LCA[u][u]=u和LCA[u][fa]=fa,然后,返回到他爸爸那里,并查集合并,f[u]=fa;表明u的爸爸是fa,所以这个时候并查集是向左看齐的,merge(u,v),u只能是爸爸。复杂度O(n²)的算法,能求出整棵树的所有LCA[i][j]值。并查集那里有点奇葩,它也用作了标记数组的作用,所以一开始的并查集,全部是0。

void dfs(int u) {

f[u] = u; //首先自己是一个集合

for (int i = first[u]; i; i = e[i].next) {

int v = e[i].v;

if (f[v] == 0) {

dfs(v);

merge(u, v);

}

}

for (int i = 1; i <= n; i++) { //遍历每一个点

if (f[i]) { //已经确定过的,就更新LCA

LCA[u][i] = LCA[i][u] = find(i);

}

}

return ;

}

O(n+Q)算法,用邻接表存取所有询问,要询问的再处理即可,注意去重操作。

void dfs(int u) {

f[u] = u; //首先自己是一个集合

for (int i = first[u]; i; i = e[i].next) {

int v = e[i].v;

if (f[v] == 0) {

dfs(v);

merge(u, v);

}

}

for (int i = first_query[u]; i; i = query[i].next) {

int v = query[i].v;

if (f[v]) { //确定过的话,并且有要求查询

//要求查询的话这个query保存着,first_query[u]就证明有没了

//因为插边插了两次,这里要去重。用id保存答案即可

ans[query[i].id] = find(v);

}

}

return ;

}

LCA倍增算法。

设ansc[cur][i]表示从cur这个节点跳2i步到达的祖先是谁。记录深度数组deep[cur]。深度从0开始,然后算LCA的时候就先把他们弄到同一深度,然后一起倍增。

Hint:ans[root][3]是自己,都是root。开始的时候fa[root] = root。deep[root] = 0;

一般这课树是双向的,因为可能结合bfs来做题,所以需要判断不能走到爸爸那里。

int ansc[maxn][25], deep[maxn], fa[maxn];

void init_LCA(int cur) {

ansc[cur][0] = fa[cur]; //跳1步,那么祖先就是爸爸

for (int i = 1; i <= 24; ++i) { //倍增思路,递归处理

ansc[cur][i] = ansc[ansc[cur][i - 1]][i - 1];

}

for (int i = first[cur]; i; i = e[i].tonext) {

int v = e[i].v;

if (v == fa[cur]) continue;

fa[v] = cur;

deep[v] = deep[cur] + 1;

init_LCA(v);

}

}

int LCA(int x, int y) {

if (deep[x] < deep[y]) swap(x, y); //需要x是最深的

for (int i = 24; i >= 0; --i) { //从大到小枚举,因为小的更灵活

if (deep[ansc[x][i]] >= deep[y]) { //深度相同,走进去就对了。

x = ansc[x][i];

}

}

if (x == y) return x;

for (int i = 24; i >= 0; --i) {

if (ansc[x][i] != ansc[y][i]) { //走到第一个不等的地方,

x = ansc[x][i];

y = ansc[y][i];

}

}

return ansc[x][0]; //再跳一步就是答案

}

E. Xenia and Tree 分块 + LCA的更多相关文章

  1. E. Xenia and Tree 解析(思維、重心剖分)

    Codeforce 342 E. Xenia and Tree 解析(思維.重心剖分) 今天我們來看看CF342E 題目連結 題目 給你一棵樹,有兩種操作,把某點標成紅色或者查詢離某點最近的紅點有多遠 ...

  2. codeforces 342E :Xenia and Tree

    Description Xenia the programmer has a tree consisting of n nodes. We will consider the tree nodes i ...

  3. Codeforces Round #329 (Div. 2) D. Happy Tree Party LCA/树链剖分

    D. Happy Tree Party     Bogdan has a birthday today and mom gave him a tree consisting of n vertecie ...

  4. 【SPOJ】10628. Count on a tree(lca+主席树+dfs序)

    http://www.spoj.com/problems/COT/ (速度很快,排到了rank6) 这题让我明白了人生T_T 我知道我为什么那么sb了. 调试一早上都在想人生. 唉. 太弱. 太弱. ...

  5. LeetCode Lowest Common Ancestor of a Binary Search Tree (LCA最近公共祖先)

    题意: 给一棵二叉排序树,找p和q的LCA. 思路: 给的是BST(无相同节点),那么每个节点肯定大于左子树中的最大,小于右子树种的最小.根据这个特性,找LCA就简单多了. 分三种情况: (1)p和q ...

  6. 2018 ICPC青岛网络赛 B. Red Black Tree(倍增lca好题)

    BaoBao has just found a rooted tree with n vertices and (n-1) weighted edges in his backyard. Among ...

  7. HDU 4912 Paths on the tree(LCA+贪心)

    题目链接 Paths on the tree 来源  2014 多校联合训练第5场 Problem B 题意就是给出m条树上的路径,让你求出可以同时选择的互不相交的路径最大数目. 我们先求出每一条路径 ...

  8. HDU 5274 Dylans loves tree(LCA+dfs时间戳+成段更新 OR 树链剖分+单点更新)

    Problem Description Dylans is given a tree with N nodes. All nodes have a value A[i].Nodes on tree i ...

  9. HDU 6394 Tree 分块 || lct

    Tree 题意: 给你一颗树, 每一个节点都有一个权值, 如果一个石头落在某个节点上, 他就会往上跳这个的点的权值步. 现在有2种操作, 1 把一个石头放在 x 的位置 询问有跳几次才跳出这棵树, 2 ...

随机推荐

  1. To verify Hadoop releases using GPG

    To verify Hadoop releases using GPG http://hadoop.apache.org/releases.html To verify Hadoop releases ...

  2. 最长公共上升子序列 (poj 2127) (Greatest Common Increasing Subsequence)

    \(Greatest Common Increasing Subsequence\) 大致题意:给出两个长度不一定相等的数列,求其中最长的公共的且单调递增的子序列(需要具体方案) \(solution ...

  3. Cats transport(codeforces311B)(斜率优化)

    \(Cats Transport\) 感觉这道题题面不好讲,就自翻了一个新的,希望有助于大家理解其思路: 大致题意: \(wch\) 的家里有 \(N\) 座山(山呈直线分布,第 \(i-1\) 座山 ...

  4. swift的String处理

    import UIKit import CoreText class ViewController: UIViewController { override func viewDidLoad() { ...

  5. 更改scroll样式

    /*定义滚动条高宽及背景 高宽分别对应横竖滚动条的尺寸*/ ::-webkit-scrollbar { width: 2px; height: 80%; background: #fff; } /*定 ...

  6. web中使用svg失量图形及ie8以下浏览器的处理方式

    直接上代码: <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <me ...

  7. webpack 构建多页面应用

    如何使用webpack构建多页面应用,这是一个我一直在想和解决的问题.网上也给出了很多的例子,很多想法.猛一看,觉得有那么点儿意思,但仔细看也就那样. 使用webpack这个构建工具,可以使我们少考虑 ...

  8. IDEA maven dependency自动提示

    通过File->setting->maven->repositories,选择本地仓库,点击右上角更新,更新maven仓库索引 在pom.xml编写引入依赖的jar包时,已经下载到本 ...

  9. 动态的添加ImageView到LinearLayout中并居中显示

    ImageView imageView = new ImageView(mActivity); imageView.setImageResource(R.mipmap.gengduo); Linear ...

  10. wukong引擎源码分析之索引——part 3 文档评分 无非就是将docid对应的fields信息存储起来,为搜索结果rank评分用

    之前的文章分析过,接受索引请求处理的代码在segmenter_worker.go里: func (engine *Engine) segmenterWorker() { for { request : ...