【题解】CF#983 E-NN country
首先,我们从 u -> v 有一个明显的贪心,即能向上跳的时候尽量向深度最浅的节点跳。这个我们可以用树上倍增来维护。我们可以认为 u 贪心向上跳后不超过 lca 能跳到 u' 的位置, v 跳到 v' 的位置,这时只需要查询一下是否有 u' -> v' 的直达公交线路就可以确定出答案了。
如果 u 和 v 在一条链上,我们可以直接倍增获得答案,所以以下讨论均为 u != lca && v != lca 的情况。 若在节点 u 和 v 之间存在一条直达线路,说明有一条线路的起点在以 u 为根的子树内,重点在以 v 为根的子树内。即起点的dfs序 >= dfn[u] && <= dfn[u] + size[u] - 1, 终点的 dfs序 >= dfn[v] && <= dfn[v] + size[v] -1;这是一个二维限制的问题,暴力主席树就可以搞定……
之后看题解发现其实有一种妙妙的做法并不需要主席树。我们可以离线处理。要查询是否有起点在 u 的子树内且终点在 v 的子树内的路线,就是dfs在进入 u 的子树内之前记录下 v 的子树内此时记录的路径数,然后进入子树,遇到路线则标记终点所在的位置。返回之后两相对比,如果此时 v 的子树内记录的路径数有增长的话说明这样的路线是存在的。
#include <bits/stdc++.h>
using namespace std;
#define maxn 450000
#define INF 99999999
#define CNST 20
int n, m, q, Mul[maxn][CNST], gra[maxn][CNST];
int dfn[maxn], dep[maxn], bits[];
int tot, timer, root[maxn], fa[maxn], size[maxn]; int read()
{
int x = , k = ;
char c; c = getchar();
while(c < '' || c > '') { if(c == '-') k = -; c = getchar(); }
while(c >= '' && c <= '') x = x * + c - '', c = getchar();
return x * k;
} struct edge
{
int cnp, to[maxn], last[maxn], head[maxn];
edge() { cnp = ; }
void add(int u, int v)
{ to[cnp] = v, last[cnp] = head[u], head[u] = cnp ++; }
}E1, G; struct edge1
{
int u, v;
edge1(int _u = , int _v = ) { u = _u, v = _v; }
friend bool operator <(const edge1& a, const edge1& b)
{ return (dfn[a.u] != dfn[b.u]) ? dfn[a.u] < dfn[b.u] : dfn[a.v] < dfn[b.v]; }
}e[maxn]; struct node
{
int u, num;
node(int _u = , int _num = )
{ u = _u, num = _num; }
}; struct node1
{
int sum, ls, rs;
}T[maxn * ]; void dfs(int u)
{
gra[u][] = fa[u], dfn[u] = ++ timer; size[u] = ;
for(int i = ; i < CNST; i ++) gra[u][i] = gra[gra[u][i - ]][i - ];
for(int i = E1.head[u]; i; i = E1.last[i])
{
int v = E1.to[i];
dep[v] = dep[u] + ; dfs(v);
size[u] += size[v];
}
} int dfs2(int u)
{
int lim = ;
for(int i = G.head[u]; i; i = G.last[i])
{
if(u != G.to[i]) lim = (dep[lim] < dep[G.to[i]]) ? lim : G.to[i];
if(!lim && u != G.to[i]) lim = G.to[i];
}
for(int i = E1.head[u]; i; i = E1.last[i])
{
int v = E1.to[i];
int t = dfs2(v);
if(u != t && t) lim = (dep[lim] < dep[t]) ? lim : t;
if(!lim && u != t) lim = t;
}
Mul[u][] = lim;
return lim;
} void dfs3(int u)
{
for(int i = ; i < CNST; i ++)
Mul[u][i] = Mul[Mul[u][i - ]][i - ];
for(int i = E1.head[u]; i; i = E1.last[i])
dfs3(E1.to[i]);
} int LCA(int u, int v)
{
if(dep[u] < dep[v]) swap(u, v);
for(int i = CNST - ; ~i; i --)
if(dep[gra[u][i]] >= dep[v]) u = gra[u][i];
for(int i = CNST - ; ~i; i --)
if(gra[u][i] != gra[v][i]) u = gra[u][i], v = gra[v][i];
return (u == v) ? u : gra[u][];
} node Jump(int u, int fu)
{
bool flag = ; int cnt = ;
if(dep[u] == dep[fu]) flag = ;
for(int i = CNST - ; ~i; i --)
{
if(!Mul[u][i]) continue;
if(dep[Mul[u][i]] <= dep[fu]) flag = ;
if(dep[Mul[u][i]] > dep[fu]) u = Mul[u][i], cnt += bits[i];
}
if(!flag) return node(-, );
else return node(u, cnt);
} void Ins(int &now, int pre, int l, int r, int x)
{
now = ++ tot; T[now] = T[pre]; T[now].sum ++;
if(l == r) return;
int mid = (l + r) >> ;
if(x <= mid) Ins(T[now].ls, T[pre].ls, l, mid, x);
else Ins(T[now].rs, T[pre].rs, mid + , r, x);
} bool Query(int now, int pre, int l, int r, int L, int R)
{
if(!now) return ;
if(L == l && R == r) return (T[now].sum - T[pre].sum);
if(L > r || R < l) return ;
int mid = (l + r) >> ;
if(R <= mid) return Query(T[now].ls, T[pre].ls, l, mid, L, R);
else if(L > mid) return Query(T[now].rs, T[pre].rs, mid + , r, L, R);
else
{
if(Query(T[now].ls, T[pre].ls, l, mid, L, mid)) return ;
else return Query(T[now].rs, T[pre].rs, mid + , r, mid + , R);
}
} int main()
{
n = read();
bits[] = ; for(int i = ; i < CNST; i ++) bits[i] = bits[i - ] << ;
for(int i = ; i <= n; i ++)
{
fa[i] = read();
E1.add(fa[i], i);
}
dep[] = ; dfs();
m = read();
for(int i = ; i <= m; i ++)
{
int u = read(), v = read(), lca = LCA(u, v);
G.add(u, lca); G.add(v, lca);
e[i] = edge1(u, v);
}
dfs2(); dfs3();
sort(e + , e + + m); int last = ; root[] = tot = ;
for(int i = ; i <= m; i ++)
{
while(last < dfn[e[i].u] - ) T[root[last + ] = ++ tot] = T[root[last]], last ++;
Ins(root[dfn[e[i].u]], root[last], , n, dfn[e[i].v]);
if(last != dfn[e[i].u]) last ++;
}
while(last + <= n) T[root[last + ] = ++ tot] = T[root[last]], last ++;
q = read();
for(int i = ; i <= q; i ++)
{
int u = read(), v = read(), lca = LCA(u, v);
node a = Jump(u, lca), b = Jump(v, lca);
int fu = a.u, fv = b.u;
if(fu == - || fv == -) { printf("-1\n"); continue; }
if(u == lca || v == lca) { printf("%d\n", a.num + b.num + ); continue; }
int l1 = dfn[fu], r1 = dfn[fu] + size[fu] - ;
int l2 = dfn[fv], r2 = dfn[fv] + size[fv] - ;
int x = Query(root[r1], root[l1 - ], , n, l2, r2);
int y = Query(root[r2], root[l2 - ], , n, l1, r1); printf("%d\n", a.num + b.num + + !(x || y));
}
return ;
}
【题解】CF#983 E-NN country的更多相关文章
- 【CodeForces】983 E. NN country 树上倍增+二维数点
[题目]E. NN country [题意]给定n个点的树和m条链,q次询问一条链(a,b)最少被多少条给定的链覆盖.\(n,m,q \leq 2*10^5\). [算法]树上倍增+二维数点(树状数组 ...
- 【codeforces 983E】NN country
Description In the NN country, there are n cities, numbered from 1 to n, and n−1 roads, connecting t ...
- 竞赛题解 - CF Round #524 Div.2
CF Round #524 Div.2 - 竞赛题解 不容易CF有一场下午的比赛,开心的和一个神犇一起报了名 被虐爆--前两题水过去,第三题卡了好久,第四题毫无头绪QwQ Codeforces 传送门 ...
- 题解——CF Manthan, Codefest 18 (rated, Div. 1 + Div. 2) T5(思维)
还是dfs? 好像自己写的有锅 过不去 看了题解修改了才过qwq #include <cstdio> #include <algorithm> #include <cst ...
- 竞赛题解 - [CF 1080D]Olya and magical square
Olya and magical square - 竞赛题解 借鉴了一下神犇tly的博客QwQ(还是打一下广告) 终于弄懂了 Codeforces 传送门 『题目』(直接上翻译了) 给一个边长为 \( ...
- Codeforces983E. NN country
新鲜出炉! $n \leq 200000$的树,给$m \leq 200000$条链,$q \leq 200000$个询问,每次问一条询问链最少用m条中的几条给定链覆盖其所有边,可能无解. 首先确定一 ...
- CF983E NN country(倍增,差分)
题意 给定一棵树和若干条路线,每条路线相当于树上 x,y 之间的路径,途径路径上的每个点 给出若干个询问,每次询问从 u 到 v 至少需要利用几条路线 N,M,Q≤200000 题解 构建倍增数组g[ ...
- [题解] [CF 1250J] The Parade
题面 题目大意: 给定一个 \(n\) , 所有军人的数量均在 \([1, n]\) 给定 \(a_i\) 代表高度为 \(i\) 的军人的个数 你要将这些军人分成 \(k\) 行, 满足下面两个条件 ...
- 题解 CF 1372 B
题目 传送门 题意 给出 \(n\),输出 \(a\) ,\(b\) (\(0 < a \leq b < n\)),使\(a+b=n\)且 \(\operatorname{lcm}(a,b ...
随机推荐
- Mybatis简单入门
前言 之前一直有直接使用Mybatis,但是没有细致的整理出来.长时间没有使用,细致的内容都忘记了.因此借此机会,从头开始整理,以后可以直接查看此次记录的内容. Mybatis的介绍 MyBatis是 ...
- Centos7 下安装以及使用mssql
Centos7下安装以及使用Mssql,在这下面玩,主要是发现linux环境下的mysql非常的小,小到只有169M,这在windows上面,动撤几个G的安装文件,会让你直接打消使用MSSQL的勇气, ...
- Hive支持行级update、delete时遇到的问题
Hive从0.14版本开始支持事务和行级更新,但缺省是不支持的,需要一些附加的配置.要想支持行级insert.update.delete,需要配置Hive支持事务.(行级的insert好像不配置也能运 ...
- 获取项目中.txt 文件的内容
package com.fh.controller.ruitai.util; import java.io.BufferedInputStream; import java.io.File; impo ...
- dva 路由跳转
1.从props取出并传递history 取 const { history } = this.props 用 <button onClick={ () => history.push(' ...
- 微信小程序如何性能测试?
背景: 微信小程序作为手机页面的一种,相比传统的网站和应用来说存在比较特殊的地方: 1. 开发者往往对程序做了限制,只能通过微信客户端访问 2. 通过微信的Oauth进行认证 这样往往会导致我们的 ...
- Kotlin的密封(Sealed)类:超强的枚举(KAD 28)
作者:Antonio Leiva 时间:Jun 27, 2017 原文链接:https://antonioleiva.com/sealed-classes-kotlin/ Kotlin的封装类是Jav ...
- Micro:bit 硬件架构介绍
Micro:bit做为当红的少儿编程工具,这两年在编程教育领域越来越火.今天就从硬件架构开始,分享Micro:bit的相关主题. Microbit 硬件设计是根据ARM mbed技术所开发的应用IC及 ...
- 利用人脸特征提取DeepID--解读世纪晟人脸识别
概述:DeepID的目标是人脸验证(判断两张图片是否是一个人),同时衍生出人脸识别(多次人脸验证). DeepID采用增大数据集的方法: 增加新的数据,celebFaces(87628张图片,5436 ...
- php面试的那些“黑话”
以下是一些常见的面试暗语,求职者一定要弄清楚其中蕴含的深意,不然可能“躺着也中枪”,最后只能铩羽而归. (1)请把简历先放在这,有消息我们会通知你的 面试官说出这句话,则表明他对你已经“兴趣不大”,为 ...