[CC-ADJLEAF2]Adjacent Leaves
[CC-ADJLEAF2]Adjacent Leaves
题目大意:
给定一棵有根树,考虑从根开始进行DFS,将所有叶子按照被遍历到的顺序排列得到一个序列。
定义一个叶子集合合法,当且仅当存在一种DFS的方式使得这个叶子集合在序列中的出现位置是一个连续子串。
给出一个\(n(n\le5\times10^5)\)个点的无根树,\(m(m\le5\times10^5)\)次询问,求以\(R\)为根的情况下叶子集合\(S(\sum|S|\le5\times10^5)\)是否合法。
思路:
定义\(leaf[x]\)为以\(x\)为根的子树内叶子结点的个数,\(cnt[x]\)为以\(x\)为根的子树内属于\(S\)的点的个数。
定义以\(x\)为根的子树是“不满的”,当且仅当\(cnt[x]\ne0\)且\(cnt[x]\ne leaf[x]\)。
对于一个结点\(x\),若其有\(\ge3\)个子树是不满的,或其有\(2\)个子树不满,且\(cnt[x]\ne|S|\),则\(S\)不合法。
对于每次询问,用一次\(\mathcal O(n)\)的树形DP求解,我们就得到了一个\(\mathcal O(nm)\)的做法。
由于\(\sum|S|\le5\times10^5\),我们可以建立虚树,将每次DFS的点数缩小为\(|S|\),此时一次DP的时间复杂度为\(\mathcal O(|S|\log n)\),总时间复杂度\(\mathcal O(\sum|S|\log n)\)。
源代码:
#include<cstdio>
#include<cctype>
#include<vector>
#include<algorithm>
inline int getint() {
register char ch;
while(!isdigit(ch=getchar()));
register int x=ch^'0';
while(isdigit(ch=getchar())) x=(((x<<2)+x)<<1)+(ch^'0');
return x;
}
const int N=5e5+1,logN=19;
std::vector<int> e[N],node;
inline void add_edge(const int &u,const int &v) {
e[u].push_back(v);
e[v].push_back(u);
}
int r,dep[N],anc[N][logN],dfn[N],s[N],leaf[N],tot;
inline int lg2(const float &x) {
return ((unsigned&)x>>23&255)-127;
}
void dfs(const int &x,const int &par) {
anc[x][0]=par;
dfn[x]=++tot;
dep[x]=dep[par]+1;
leaf[x]=e[x].size()==1;
for(register int i=1;i<=lg2(dep[x]);i++) {
anc[x][i]=anc[anc[x][i-1]][i-1];
}
for(auto &y:e[x]) {
if(y==par) continue;
dfs(y,x);
leaf[x]+=leaf[y];
}
}
inline int lca(int x,int y) {
if(dep[x]<dep[y]) std::swap(x,y);
for(register int i=lg2(dep[x]-dep[y]);i>=0;i--) {
if(dep[anc[x][i]]>=dep[y]) {
x=anc[x][i];
}
}
for(register int i=lg2(dep[x]);i>=0;i--) {
if(anc[x][i]!=anc[y][i]) {
x=anc[x][i];
y=anc[y][i];
}
}
return x==y?x:anc[x][0];
}
inline bool is_anc(const int &x,int y) {
//x shi y de zuxian
if(dep[x]>dep[y]) return false;
for(register int i=lg2(dep[y]-dep[x]);i>=0;i--) {
if(dep[anc[y][i]]>=dep[x]) {
y=anc[y][i];
}
}
return x==y;
}
inline int jump(int x,const int &d) {
//zhaodao x shendu wei d de zuxian
for(register int i=lg2(dep[x]-d);i>=0;i--) {
if(dep[anc[x][i]]>=d) {
x=anc[x][i];
}
}
return x;
}
inline int near_ch(const int &x,const int &y) {
//zhaodao x zuijin de zijiedian, bingqie shi y de zuxian
if(is_anc(x,y)) {
return jump(y,dep[x]+1);
} else {
return anc[x][0];
}
}
inline int count_leaves(const int &x) {
if(x==r) return leaf[1];
if(is_anc(x,r)) {
return leaf[1]-leaf[jump(r,dep[x]+1)];
} else {
return leaf[x];
}
}
class AuxTree {
//auxiliary tree
private:
int stk[N],top,cnt[N];
std::vector<int> e[N];
void add_edge(const int &u,const int &v) {
e[u].push_back(v);
e[v].push_back(u);
}
public:
void build(int s[],const int &r) {
const int n=s[0]+1;
s[n]=r;
std::sort(&s[1],&s[n]+1,
[](const int &i,const int &j) {
return dfn[i]<dfn[j];
}
);
stk[top=1]=s[1];
node.push_back(s[1]);
for(register int i=2;i<=n;i++) {
const int &x=s[i],p=lca(x,stk[top]);
while(dfn[p]<dfn[stk[top]]) {
if(dfn[p]>=dfn[stk[top-1]]) {
add_edge(p,stk[top--]);
if(stk[top]!=p) {
stk[++top]=p;
node.push_back(p);
}
break;
} else {
add_edge(stk[top],stk[top-1]);
top--;
}
}
stk[++top]=x;
node.push_back(x);
}
for(;top>1;top--) {
add_edge(stk[top],stk[top-1]);
}
}
void dfs(const int &x,const int &par) {
int tmp=0;
cnt[x]=x!=r&&e[x].size()==1;
for(auto &y:e[x]) {
if(y==par) continue;
dfs(y,x);
cnt[x]+=cnt[y];
tmp+=cnt[y]<count_leaves(near_ch(x,y));
}
if(tmp>2||(tmp==2&&cnt[x]!=s[0])) throw 0;
}
void clear() {
for(auto &x:node) {
e[x].clear();
}
node.clear();
}
};
AuxTree t;
int main() {
const int n=getint(),m=getint();
for(register int i=1;i<n;i++) {
add_edge(getint(),getint());
}
dfs(1,0);
for(register int i=0;i<m;i++) {
r=getint();
for(register int i=0;i<=s[0];i++) {
s[i]=getint();
}
t.build(s,r);
try {
t.dfs(r,0);
puts("YES");
} catch(...) {
puts("NO");
}
t.clear();
}
return 0;
}
[CC-ADJLEAF2]Adjacent Leaves的更多相关文章
- TOJ 4008 The Leaf Eaters(容斥定理)
Description As we all know caterpillars love to eat leaves. Usually, a caterpillar sits on leaf, eat ...
- TOJ 4008 The Leaf Eaters
|A∪B∪C|=|A|+|B|+|C|-|A∩B|-|A∩C|-|B∩C|+|A∩B∩C| 这个是集合的容斥,交集差集什么的,这个在概率论经常用到吧 4008: The Leaf Eaters T ...
- Tarjan算法求解桥和边双连通分量(附POJ 3352 Road Construction解题报告)
http://blog.csdn.net/geniusluzh/article/details/6619575 在说Tarjan算法解决桥和边双连通分量问题之前我们先来回顾一下Tarjan算法是如何 ...
- 03-树2 List Leaves
二叉树及其遍历 一遍AC,挺开心的hhh~ 简单讲下思路:叶子,顾名思义就是没有左右子树的结点.由于题目要求,叶子结点的输出顺序是从上往下,从左往右.所以用层序遍历法. 当然,这里先找到root树的根 ...
- 03-树1. List Leaves (25)
Given a tree, you are supposed to list all the leaves in the order of top down, and left to right. I ...
- List Leaves
Given a tree, you are supposed to list all the leaves in the order of top down, and left to right. I ...
- 366. Find Leaves of Binary Tree输出层数相同的叶子节点
[抄题]: Given a binary tree, collect a tree's nodes as if you were doing this: Collect and remove all ...
- CF&&CC百套计划4 Codeforces Round #276 (Div. 1) E. Sign on Fence
http://codeforces.com/contest/484/problem/E 题意: 给出n个数,查询最大的在区间[l,r]内,长为w的子区间的最小值 第i棵线段树表示>=i的数 维护 ...
- 7-8 List Leaves(25 分)
Given a tree, you are supposed to list all the leaves in the order of top down, and left to right. I ...
随机推荐
- Wowza 相关
下载: 1.https://www.wowza.com/pricing/installer 安装: https://www.wowza.com/docs/how-to-install-and-conf ...
- 阿里云人脸识别测试接口出错 返回Body:{ "errno": 1031, "err_msg": "Invalid Image URL.", "request_id": "cdbe2927-e1bb-4eb1-a603-8fcd4b0b7fc8" }
错误信息如下 返回Body:{ "errno": 1031, "err_msg": "Invalid Image URL.", " ...
- windows server 2012 R2 远程桌面授权模式尚未配置
windows server 2012 R2 远程桌面授权模式尚未配置,远程桌面服务将在120天内停止工作.如何破解这个宽限期,目前企业7位协议号码均不包含2012 R2以上授权. 那么只能蛋疼的“破 ...
- Centos+Redis 集群
Redis 3.2.6集群搭建 Redis3.0版本之后支持Cluster. 1.1.redis cluster的现状 目前redis支持的cluster特性: 1):节点自动发现 2):slave- ...
- webpack学习笔记--多种配置类型
除了通过导出一个 Object 来描述 Webpack 所需的配置外,还有其它更灵活的方式,以简化不同场景的配置. 下面来一一介绍它们. 导出一个 Function 在大多数时候你需要从同一份源代码中 ...
- Java连接MySQL数据库三种方法
好久没有更新博客了!今天利用周目时学习了一下数据库mysql.介绍一下数据库的三种连接方式! 开发工具:Myeclipse MySQL5.6 MySQL连接驱动:mysql-connector-jav ...
- P4715 「英语」Z 语言
题解: 平衡树维护hash值 为了支持加入删除操作 x*base^y 其中y为他是第k大 同一般的维护方法,我们不用对每个节点维护他的hash值 而是只用记录他的x值(他的位置) 然后通过updata ...
- js中的new Option默认选中
new Option("文本","值",true,true).后面两个true分别表示默认被选中和有效! //js默认选中 var sel = document ...
- PHP算法排序之快速排序、冒泡排序、选择排序、插入排序性能对比
<?php //冒泡排序 //原理:从倒数第一个数开始,相邻的两个数比较,后面比前面的小,则交换位置,一直到比较第一个数之后则最小的会排在第一位,以此类推 function bubble_sor ...
- SQL注入——SQL Injection
本文部分内容转自:https://www.cnblogs.com/rush/archive/2011/12/31/2309203.html http://www.diybl.com/course/7_ ...