HDU 6031 Innumerable Ancestors(LCA,树链剖分)
来源:互联网 发布:mac safari下载速度慢 编辑:程序博客网 时间:2024/05/28 04:53
题意: 对于给定的一棵树,树上点的数目为
思路: 先对树进行剖分,然后将所有
总的时间复杂度为
#include<iostream>#include<map>#include<string>#include<cstring>#include <vector>#include <algorithm>#include <string.h>#include <cstdlib>#include <cctype>#include <set>#include <set>#include <queue>#include <stack>#include <cstdio>#define EPS 1e-6using namespace std;typedef long long ll;typedef pair<int, int > pii;#define sp system("pause")const int MAXN = 110010;struct Edge{ int to, next;}edge[MAXN * 2];int head[MAXN], tot;int top[MAXN];int fa[MAXN];int deep[MAXN];int num[MAXN];int son[MAXN];int pos;int mrk[MAXN];int mxdep[MAXN];int v[MAXN],ttt=0;void init(){ tot = 0; memset(head, -1, sizeof head); pos = 1; memset(son, -1, sizeof son);}void addedge(int u, int v){ edge[tot].to = v; edge[tot].next = head[u]; head[u] = tot++;}void dfs1(int u, int pre, int d){ deep[u] = d; fa[u] = pre; num[u] = 1; for (int i = head[u]; i != -1; i = edge[i].next) { int v = edge[i].to; if (v == pre)continue; dfs1(v, u, d + 1); num[u] += num[v]; if (son[u] == -1 || num[v] > num[son[u]])son[u] = v; }}void dfs2(int u, int po){ top[u] = po; if (son[u] == -1)return; dfs2(son[u], po); for (int i = head[u]; i != -1; i = edge[i].next) { int v = edge[i].to; if (v != son[u] && v != fa[u]) dfs2(v, v); }}int ans = 0;void lca(int x){ while (x) { if (mrk[top[x]] == 1 && mxdep[top[x]] == deep[x])break; mrk[top[x]] = 1; mxdep[top[x]] = max(mxdep[top[x]], deep[x]); x = fa[top[x]]; }}int lcb(int x){ while (!mrk[top[x]])x = fa[top[x]]; return min(mxdep[top[x]], deep[x]);}void clr(int x){ while (x) { if (mrk[top[x]] == 0)break; mrk[top[x]] = 0; mxdep[top[x]] =0; x = fa[top[x]]; }}int main(){ int n, m; int cot = 0; while (scanf("%d%d", &n, &m) != EOF) { init(); for (int i = 0; i < n - 1; i++) { int x, y; scanf("%d%d", &x, &y); addedge(x, y); addedge(y, x); } dfs1(1, 0, 0); dfs2(1, 1); int mm = 0; memset(mrk, 0, sizeof mrk); memset(mxdep, 0, sizeof mxdep); while (m--) { //mm++; //if (mm >=1)break; ttt = 0; int x; scanf("%d", &x); for (int i = 0; i < x; i++) { int y; scanf("%d", &y); v[ttt++] = y; lca(y); } scanf("%d", &x); ans = 0; for (int i = 0; i < x; i++) { int y; scanf("%d", &y); ans = max(ans, lcb(y)); } for (int i = 0; i < ttt; i++)clr(v[i]); printf("%d\n", ans + 1); } }}
0 0
- HDU 6031 Innumerable Ancestors(LCA,树链剖分)
- HDU 6031 Innumerable Ancestors【LCA】
- HDU 6031 Innumerable Ancestors (LCA)
- hdu 6031 Innumerable Ancestors(LCA+二分)
- hdu 6031 Innumerable Ancestors lca + 二分
- HDU 6031 Innumerable Ancestors[树链剖分]
- hdu 6031 Innumerable Ancestors
- 【HDU6031】Innumerable Ancestors(二分+LCA)
- 2017CCPC女生赛 hdu 6031 Innumerable Ancestors
- hdu6031 Innumerable Ancestors
- HDU6031 Innumerable Ancestors 倍增
- hdu1470Closest Common Ancestors LCA
- Nearest Common Ancestors--LCA
- 1330 Nearest Common Ancestors //LCA
- 1470 Closest Common Ancestors //LCA
- poj1330Nearest Common Ancestors(LCA小结)
- POJ1470 Closest Common Ancestors LCA
- poj1470-Closest Common Ancestors(LCA)
- 欢迎使用CSDN-markdown编辑器
- JAVA导出excel(JFinal)
- Git 常用命令
- mysql查询5分钟内的数据
- nfs3方式挂载hdfs实现高可用存储
- HDU 6031 Innumerable Ancestors(LCA,树链剖分)
- linux下学习正则的一些小结
- 大都会系统MetLife小记
- java实现重建二叉树
- Python进阶之装饰器
- Ubuntu下安装MySQL及简单操作
- Edraw Office Viewer component教程(二):将Excel嵌入VB 6并使之自动化
- win10+anaconda3-4.3.1+tesorflow1.0安装过程(配置Spyder)
- 港中大教授James招收机器学习理论方向全奖博士生