[CF403E]Two Rooted Trees
Two Rooted Trees
题面翻译
题目描述
你有两棵有根树,每棵树都有 \(n\) 个结点。不妨将这两棵树上的点都用 \(1\) 到 \(n\) 之间的整数编号。每棵树的根结点都是 \(1\)。第一棵树上的边都是蓝色,第二课树上的边都是红色。我们也称第一棵树是蓝色的,第二棵树是红色的。
对于一条边 \((p, q)\),当以下条件满足时,我们认为 \((x, y)\) 是一条坏边:
- 边 \((x, y)\) 的颜色与 \((p, q)\) 的颜色不同。
- 考虑与 \((p, q)\) 颜色相同的那棵树。在 \(x\) 和 \(y\) 中有且仅有其中一个点同时位于 \(p\) 和 \(q\) 的子树。(注意这里的 \(x, y\) 和上面的 \((x, y)\) 不在同一棵树上)
在本题中,你的任务是模拟下述过程。该过程包含几个阶段:
- 在每个阶段,有且仅有一种颜色的边可以被删除。
- 在第 \(1\) 个阶段,有且仅有一条蓝色的边被删除。
- 假设在第 \(i\) 个阶段我们删除了 \((u_1, v_1), (u_2, v_2), ..., (u_k, v_k)\)。在第 \(i+1\) 个阶段,我们会先删除所有对于 \((u_1, v_1)\) 的没有删除的坏边,然后删除所有对于 \((u_2, v_2)\) 的没有删除的坏边,然后一直进行下去,直到 \((u_k, v_k)\) 结束。
对于每一个阶段,输出哪些边会被删除。注意,对于一条边的坏边的定义,我们总是只考虑初始的那两棵树。
输入格式
第一行包含一个整数 \(n\) \((2 \leq n \leq 2 \times 10^5)\),表示一棵树上点的个数。
接下来一行包含 \(n-1\) 个正整数 \(a_2, a_3, ..., a_n (1 \leq a_i \leq n; a_i \leq i)\),用来描述第一棵树。其中,\(a_i\) 表示第一棵树上存在一条边连接了 \(a_i\) 和 \(i\)。
接下来一行包含 \(n-1\) 个正整数 \(b_2, b_3, ..., b_n (1 \leq b_i \leq n; b_i \leq i)\),用来描述第二棵树。其中,\(a_i\) 表示第二棵树上存在一条边连接了 \(b_i\) 和 \(i\)。
接下来一行包含一个整数 \(idx\),表示第一阶段所删除的蓝色边的编号。假设每棵树的边都按照输入顺序从 \(1\) 到 \(n-1\) 编号。
输出格式
对于每一个删边的阶段,输出两行。如果在一个删除蓝色边的阶段,那么第一行输出一个单词 Blue,否则输出 Red。第二行按升序输出,此阶段被删除的所有边的编号。
样例解释
- 首先删除蓝色 \(3\) 号边:\((1, 4)\);
- 在第一棵树上,只有 \(4\) 号点满足同时在 \(1\) 和 \(4\) 的子树上,所以第二棵树上所有与 \(4\) 相连的边全部被删除,也就是红色 \(1\) 号边:\((2, 4)\) 和红色 \(3\) 号边 \((1, 4)\);
- 在第二棵树上,\(2, 3\) 号点满足同时在 \(2\) 和 \(4\) 的子树上,所以第一棵树上所有与 \(2\) 或 \(3\) 相连的边全部被删除,也就是蓝色 \(1\) 号边:\((1. 2)\) 和蓝色 \(2\) 号边 \((1, 3)\),至于满足同时在 \(1\) 和 \(4\) 子树上的点,由于已经被删除干净,所以不提;
- 在第一棵树上,只有 \(2\) 号点满足同时在 \(1\) 和 \(2\) 的子树上,所以第二棵树上所有与 \(2\) 相连的边全部被删除,也就是红色 \(2\) 号边:\((2. 3)\) ;
- 没有边可以删除,程序结束。
题目描述
You have two rooted undirected trees, each contains $ n $ vertices. Let's number the vertices of each tree with integers from $ 1 $ to $ n $ . The root of each tree is at vertex $ 1 $ . The edges of the first tree are painted blue, the edges of the second one are painted red. For simplicity, let's say that the first tree is blue and the second tree is red.
Edge $ {x,y} $ is called bad for edge $ {p,q} $ if two conditions are fulfilled:
- The color of edge $ {x,y} $ is different from the color of edge $ {p,q} $ .
- Let's consider the tree of the same color that edge $ {p,q} $ is. Exactly one of vertices $ x $ , $ y $ lies both in the subtree of vertex $ p $ and in the subtree of vertex $ q $ .
In this problem, your task is to simulate the process described below. The process consists of several stages:
- On each stage edges of exactly one color are deleted.
- On the first stage, exactly one blue edge is deleted.
- Let's assume that at the stage $ i $ we've deleted edges $ {u_{1},v_{1}} $ , $ {u_{2},v_{2}} $ , $ ... $ , $ {u_{k},v_{k}} $ . At the stage $ i+1 $ we will delete all undeleted bad edges for edge $ {u_{1},v_{1}} $ , then we will delete all undeleted bad edges for edge $ {u_{2},v_{2}} $ and so on until we reach edge $ {u_{k},v_{k}} $ .
For each stage of deleting edges determine what edges will be removed on the stage. Note that the definition of a bad edge always considers the initial tree before it had any edges removed.
输入格式
The first line contains integer $ n $ ( $ 2<=n<=2·10^{5} $ ) — the number of vertices in each tree.
The next line contains $ n-1 $ positive integers $ a_{2},a_{3},...,a_{n} $ ( $ 1<=a_{i}<=n; a_{i}≠i $ ) — the description of edges of the first tree. Number $ a_{i} $ means that the first tree has an edge connecting vertex $ a_{i} $ and vertex $ i $ .
The next line contains $ n-1 $ positive integers $ b_{2},b_{3},...,b_{n} $ ( $ 1<=b_{i}<=n; b_{i}≠i $ ) — the description of the edges of the second tree. Number $ b_{i} $ means that the second tree has an edge connecting vertex $ b_{i} $ and vertex $ i $ .
The next line contains integer $ idx $ ( $ 1<=idx<n $ ) — the index of the blue edge that was removed on the first stage. Assume that the edges of each tree are numbered with numbers from $ 1 $ to $ n-1 $ in the order in which they are given in the input.
输出格式
For each stage of removing edges print its description. Each description must consist of exactly two lines. If this is the stage when blue edges are deleted, then the first line of the description must contain word $ Blue $ , otherwise — word $ Red $ . In the second line print the indexes of the edges that will be deleted on this stage in the increasing order.
样例 #1
样例输入 #1
5
1 1 1 1
4 2 1 1
3
样例输出 #1
Blue
3
Red
1 3
Blue
1 2
Red
2
提示
For simplicity let's assume that all edges of the root tree received some direction, so that all vertices are reachable from vertex $ 1 $ . Then a subtree of vertex $ v $ is a set of vertices reachable from vertex $ v $ in the resulting directed graph (vertex $ v $ is also included in the set).
子树问题,先把他拍成 dfs 序
如果增加一个子树 \(x\),设这个子树的 dfs 序区间是 \([l,r]\),那么可以用线段树维护另一个树的信息,具体来说在每个点维护 set 表示另一棵树和这个点连的边,然后线段树区间里面村这个 dfs 序区间中在另一棵树连的点在这棵树 dfs 序的最大值和最小值2,就能方便的找到所有跨子树的边并从线段树里面删掉了。
#include<bits/stdc++.h>
using namespace std;
const int N=2e5+5,INF=2e9;
int n,tp=1,st[N],ss[N];
struct node{
int mx,mn,wx,wn;
node operator+(const node&n)const{
node k=n;
if(mx>k.mx)
k.wx=wx,k.mx=mx;
if(mn<k.mn)
k.wn=wn,k.mn=mn;
return k;
}
};
struct tree{
int fa[N],in[N],dfn[N],tme,sz[N];
node s[N<<2];
set<int>tr[N<<2];
vector<int>g[N];
void sou(int x)
{
sz[x]=1;
dfn[++tme]=x,in[x]=tme;
for(int v:g[x])
sou(v),sz[x]+=sz[v];
}
void build()
{
for(int i=2;i<=n;i++)
g[fa[i]].push_back(i);
sou(1);
for(int i=0;i<(N<<2);i++)
s[i].mx=0,s[i].mn=INF;
}
void insert(int o,int l,int r,int x,int y)
{
if(l==r)
{
s[o].wn=s[o].wx=l;
s[o].mx=max(s[o].mx,y);
s[o].mn=min(s[o].mn,y);
return tr[x].insert(y),void();
}
int md=l+r>>1;
if(md>=x)
insert(o<<1,l,md,x,y);
else
insert(o<<1|1,md+1,r,x,y);
s[o]=s[o<<1]+s[o<<1|1];
}
void kill(int o,int l,int r,int x,int y)
{
if(l==r)
{
s[o].wn=s[o].wx=l;
tr[x].erase(y);
if(tr[x].empty())
s[o].mx=0,s[o].mn=INF;
else
{
s[o].mx=*(--tr[x].end());
s[o].mn=*tr[x].begin();
}
return;
}
int md=l+r>>1;
if(md>=x)
kill(o<<1,l,md,x,y);
else
kill(o<<1|1,md+1,r,x,y);
s[o]=s[o<<1]+s[o<<1|1];
}
void add(int x,int y)
{
insert(1,1,n,in[x],in[y]);
insert(1,1,n,in[y],in[x]);
}
pair<int,int>askmx(int o,int l,int r,int x,int y)
{
if(s[o].mx<=y)
return make_pair(0,0);
if(x<=l&&r<=y)
{
int l=dfn[s[o].wx],r=dfn[s[o].mx];
kill(1,1,n,s[o].mx,s[o].wx);
kill(1,1,n,s[o].wx,s[o].mx);
return make_pair(l,r);
}
int md=l+r>>1;
pair<int,int>p=make_pair(0,0);
if(md>=x)
p=askmx(o<<1,l,md,x,y);
if(!p.first&&md<y)
p=askmx(o<<1|1,md+1,r,x,y);
return p;
}
pair<int,int>askmn(int o,int l,int r,int x,int y)
{
if(s[o].mn>=x)
return make_pair(0,0);
if(x<=l&&r<=y)
{
int l=dfn[s[o].wn],r=dfn[s[o].mn];
kill(1,1,n,s[o].mn,s[o].wn);
kill(1,1,n,s[o].wn,s[o].mn);
return make_pair(l,r);
}
int md=l+r>>1;
pair<int,int>p=make_pair(0,0);
if(md>=x)
p=askmn(o<<1,l,md,x,y);
if(!p.first&&md<y)
p=askmn(o<<1|1,md+1,r,x,y);
return p;
}
vector<pair<int,int> >ask(int x)
{
vector<pair<int,int> >t;
t.clear();
pair<int,int>p=askmx(1,1,n,in[x],in[x]+sz[x]-1);
while(p.first)
{
t.push_back(p);
p=askmx(1,1,n,in[x],in[x]+sz[x]-1);
}
p=askmn(1,1,n,in[x],in[x]+sz[x]-1);
while(p.first)
{
t.push_back(p);
p=askmn(1,1,n,in[x],in[x]+sz[x]-1);
}
return t;
}
void kill(int x,int y)
{
kill(1,1,n,in[x],in[y]);
kill(1,1,n,in[y],in[x]);
}
}tr[2];
int main()
{
scanf("%d",&n);
for(int i=2;i<=n;i++)
scanf("%d",&tr[0].fa[i]);
for(int i=2;i<=n;i++)
scanf("%d",&tr[1].fa[i]);
tr[0].build();
tr[1].build();
for(int i=2;i<=n;i++)
{
tr[1].add(tr[0].fa[i],i);
tr[0].add(tr[1].fa[i],i);
}
scanf("%d",st+1),++st[1];
tr[1].kill(st[1],tr[0].fa[st[1]]);
for(int op=0;tp;op^=1)
{
puts(op? "Red":"Blue");
for(int i=1;i<=tp;i++)
printf("%d ",st[i]-1);
puts("");
vector<pair<int,int> >p;
int tt=0;
for(int i=1;i<=tp;i++)
{
p=tr[op].ask(st[i]);
for(pair<int,int>v:p)
{
if(tr[op^1].fa[v.second]==v.first)
ss[++tt]=v.second;
else
ss[++tt]=v.first;
}
}
for(int i=1;i<=tt;i++)
st[i]=ss[i];
tp=tt;
sort(st+1,st+tp+1);
}
}
[CF403E]Two Rooted Trees的更多相关文章
- HDU p1294 Rooted Trees Problem 解题报告
http://www.cnblogs.com/keam37/p/3639294.html keam所有 转载请注明出处 Problem Description Give you two definit ...
- Tree - Rooted Trees
Rooted Trees A graph G = (V, E) is a data structure where V is a finite set of vertices and E is a b ...
- 【Aizu - ALDS1_7_A】Rooted Trees(树的表达)
Rooted Trees Descriptions: A graph G = (V, E) is a data structure where V is a finite set of vertice ...
- 10.3 Implementing pointers and objects and 10.4 Representing rooted trees
Algorithms 10.3 Implementing pointers and objects and 10.4 Representing rooted trees Allocating an ...
- 有根树的表达 Aizu - ALDS1_7_A: Rooted Trees
有根树的表达 题目:Rooted Trees Aizu - ALDS1_7_A A graph G = (V, E) is a data structure where V is a finite ...
- HDU1294 Rooted Trees Problem(整数划分 组合数学 DP)
讲解见http://www.cnblogs.com/IMGavin/p/5621370.html, 4 可重组合 dfs枚举子树的节点个数,相乘再累加 1 #include<iostream& ...
- HDU 1294 Rooted Trees Problem
题目大意:求有n个节点的树有几种? 题解:http://www.cnblogs.com/keam37/p/3639294.html #include <iostream> typedef ...
- [LeetCode] Minimum Height Trees 最小高度树
For a undirected graph with tree characteristics, we can choose any node as the root. The result gra ...
- Minimum Height Trees
For a undirected graph with tree characteristics, we can choose any node as the root. The result gra ...
- LeetCode Minimum Height Trees
原题链接在这里:https://leetcode.com/problems/minimum-height-trees/ 题目: For a undirected graph with tree cha ...
随机推荐
- SpringBoot如何整合spring-retry来实现接口请求重试
一.重试机制 由于网络不稳定或网络抖动经常会造成接口请求失败的情况,当我们再去尝试就成功了,这就是重试机制. 其主要目的就是要尽可能地提高请求成功的概率,但一般情况下,我们请求第一次失败,代码运行就抛 ...
- 【技术积累】Docker部署笔记
服务器环境搭建 nacos镜像 使用宝塔Docker管理器直接拉起nacos环境并运行 注意:在同一台服务器中,nacos只对内网才能注册,图中172.17.0.2是内网地址,在多台服务器中需要跨ip ...
- 【路由器】小米 WR30U 解锁并刷机
本文主要记录个人对小米 WR30U 路由器的解锁和刷机过程,整体步骤与 一般安装流程 类似,但是由于 WR30U 的解锁 ssh 和刷机的过程中有一些细节需要注意,因此记录一下 解锁 ssh 环境准备 ...
- 《SQL与数据库基础》22. 分库分表(二)
目录 分库分表(二) MyCat分片规则 范围分片 取模分片 一致性hash分片 枚举分片 应用指定算法 固定分片hash算法 字符串hash解析 按天分片 自然月分片 MyCat原理 MyCat管理 ...
- QA|外部调用类方法总报错missing 1 required positional argument:'self'|UI自动化
外部调用类方法总报错missing 1 required positional argument:'self' 原因:实例化这个类 实例化错了,少了括号() 解决:改成如下就可以了 参考学习:调用类方 ...
- 编译器优化记录(Mem2Reg+SSA Destruction)
编译器优化记录(2) Mem2Reg+SSA Destruction 写的时候忽然想起来,这部分的内容恰好是在我十八岁生日的前一天完成的.算是自己给自己的一份成长的纪念吧. 0. 哪些东西可以Mem2 ...
- C++ 重载运算符在HotSpot VM中的应用
C++支持运算符重载,对于Java开发者来说,这个可能比较陌生一些,因为Java不支持运算符重载.运算符重载本质上来说就是函数重载.下面介绍一下HotSpot VM中的运算符重载. 1.内存分配与释放 ...
- Oracle:Ora-01652无法通过128(在temp表空间中)扩展temp段的过程-解决步骤
现象:查询select * from v$sql时提示"Ora-01652无法通过128(在temp表空间中)扩展temp段的过程" 临时文件是不存储的,可以将数据库重启,重启后重 ...
- LeetCode 周赛上分之旅 #48 一道简单的树上动态规划问题
️ 本文已收录到 AndroidFamily,技术和职场问题,请关注公众号 [彭旭锐] 和 BaguTree Pro 知识星球提问. 学习数据结构与算法的关键在于掌握问题背后的算法思维框架,你的思考越 ...
- MySQL系列之读写分离架构——Atlas介绍、安装配置、Atlas功能测试、生产用户要求、Atlas基本管理、自动分表、关于读写分离建议
文章目录 1. Atlas介绍 2.安装配置 3. Atlas功能测试 4. 生产用户要求 5. Atlas基本管理 6. 自动分表 7. 关于读写分离建议 1. Atlas介绍 Atlas是由 Qi ...