# 倍增lca的另类做法(以空间换取小常数)

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <iomanip>
#include <cmath>
#include <queue>
#include <map>
using namespace std;
#define reg register
#define LL long long
LL res=0;char ch=getchar();bool fu=0;
while(!isdigit(ch)) fu|=(ch=='-'), ch=getchar();
while(isdigit(ch)) res=(res<<3)+(res<<1)+(ch^48),ch=getchar();
return fu ? -res : res;
}
#define mkp make_pair
#define pii pair<int, int>

namespace BriMon
{
#define N 500005
int n, m, root;
struct edge {
int nxt, to;
}ed[N<<1];
inline void add(int x, int y) {
}
int fa[N][21], dep[N];
int in[N], out[N], dfsc;

bool isancestor(int x, int y)  //x是不是y的祖先
{
return (in[x] <= in[y]) and (out[x] >= out[y]);
}

inline int lca(int x, int y) {
if (dep[x] < dep[y]) swap(x, y);
if (isancestor(y, x)) return y;
for (reg int i = 20 ; i >= 0 ; i --) if (fa[y][i] and !isancestor(fa[y][i], x)) y = fa[y][i];
return fa[y][0];
}

void dfs(int x)
{
in[x] = ++dfsc;
for (reg int i = head[x] ; i ; i = ed[i].nxt) {
int to = ed[i].to;
if (dep[to]) continue;
dep[to] = dep[x] + 1;
fa[to][0] = x;
for (reg int j = 1 ; j <= 20 ; j ++) fa[to][j] = fa[fa[to][j-1]][j - 1];
dfs(to);
}
out[x] = ++dfsc;
}

signed main()
{
for (reg int i = 1 ; i < n ; i ++)
{
}
dep[root] = 1;
dfs(root);
return 0;
}

}

signed main()
{
BriMon :: main();
return 0;
}

posted @ 2020-10-02 11:43  zZhBr  阅读(160)  评论(0编辑  收藏  举报