题目大意

给你两棵树,结点分别是1~A与1~B,然后给了N台设备,并且A树和B树的叶子结点(两棵树的叶子节点数量相同)都是链接电机的。问,最多可以删掉几条边使得每个设备都能连到任意一棵(或两棵)树的根节点(1号点)

思路

对于每棵树,维护\(val[cnt][i][j]\),\(cnt\)是那个树表示我删掉这个子树的所有边之后,\([i,j]\)这个范围的设备不保证能够全部连上我的根。

用一个\(f[i]\)表示\([1,i]\)区间内,全都能连上根最多能删除多少条边,那么转移就是\(f[i]=max(f[i],f[j-1]+max(val[cnt][j][i]))\)

代码

#include<bits/stdc++.h>
#include<vector>
using namespace std;
const int N=2019;
vector<int> G[2][N];
int f[N];
int val[2][N][N],l[2][N],r[2][N],size[2][N];
int x,n,a;
void dfs(int num,int x)
{
if(x!=1) size[num][x]=1;
for(int i=0;i<G[num][x].size();++i)
{
int to=G[num][x][i];
dfs(num,to);
size[num][x]+=size[num][to];
l[num][x]=min(l[num][x],l[num][to]);
r[num][x]=max(r[num][x],r[num][to]);
}
val[num][l[num][x]][r[num][x]]=max(val[num][l[num][x]][r[num][x]],size[num][x]);
}
void read() {
cin>>n;
for(int cnt=0; cnt<=1; ++cnt) {
cin>>a;
for(int i=1; i<=a; i++) l[cnt][i]=a+1,r[cnt][i]=0;
for(int i=2; i<=a; ++i) {
cin>>x;
G[cnt][x].push_back(i);
}
for(int i=1; i<=n; ++i) {
cin>>x;
l[cnt][x]=r[cnt][x]=i;
}
dfs(cnt,1);
}
}
int main() {
read();
for(int i=1;i<=n;++i)
for(int j=i;j<=n;++j)
f[j]=max(f[j],f[i-1]+max(val[0][i][j],val[1][i][j]));
cout<<f[n];
return 0;
}

推荐看看这篇博客传送门

Codeforces Round #603 F Economic Difficulties的更多相关文章

  1. Codeforces Round #603 (Div. 2)F. Economic Difficulties

    F. Economic Difficulties 题目链接: https://codeforces.com/contest/1263/problem/F 题目大意: 两棵树,都有n个叶子节点,一棵树正 ...

  2. Codeforces Round #603 (Div. 2) F. Economic Difficulties dp

    F. Economic Difficulties An electrical grid in Berland palaces consists of 2 grids: main and reserve ...

  3. Educational Codeforces Round 40 F. Runner's Problem

    Educational Codeforces Round 40 F. Runner's Problem 题意: 给一个$ 3 * m \(的矩阵,问从\)(2,1)$ 出发 走到 \((2,m)\) ...

  4. Codeforces Round #603 (Div. 2)

    传送门 感觉脑子还是转得太慢了QAQ,一些问题老是想得很慢... A. Sweet Problem 签到. Code /* * Author: heyuhhh * Created Time: 2019 ...

  5. Codeforces Round #603 (Div. 2) (题解)

    A. Sweet Problem (找规律) 题目链接 大致思路: 有一点瞎猜的,首先排一个序, \(a_1>a_2>a_3\) ,发现如果 \(a_1>=a_2+a_3\) ,那么 ...

  6. Codeforces Round #603 (Div. 2) A. Sweet Problem(水.......没做出来)+C题

    Codeforces Round #603 (Div. 2) A. Sweet Problem A. Sweet Problem time limit per test 1 second memory ...

  7. [CF Round603 Div2 F]Economic Difficulties

    题目:Economic Difficulties 传送门:https://codeforces.com/contest/1263/problem/F 题意:给了两棵tree:Ta(拥有a个节点,节点编 ...

  8. Codeforces Round #603 (Div. 2) E. Editor 线段树

    E. Editor The development of a text editor is a hard problem. You need to implement an extra module ...

  9. Codeforces Round #603 (Div. 2) E. Editor(线段树)

    链接: https://codeforces.com/contest/1263/problem/E 题意: The development of a text editor is a hard pro ...

随机推荐

  1. 全链路跟踪skywalking简介

    该文章主要包括以下内容: skywalking的简介 skywalking的使用,支持多种调用中间件(httpclent,springmvc,dubbo,mysql等等) skywalking的tra ...

  2. Linux :vim 模式下的常用命令

    [参考文章]:vim 复制一整行 复制多行 1. 查找命令 ?text    查找text,按n健查找下一个,按N健查找前一个 /text     反向查找text,按n健查找下一个,按N健查找前一个 ...

  3. SRS之信号的管理:SrsSignalManager

    1. 综述 SRS 中使用了 State Threads 协程库,该库对信号的处理是将信号事件转换为 I/O 事件.主要做法是:对关注的信号设置同样地信号处理函数 sig_catcher(),该函数捕 ...

  4. Cannot use unsafe construct in safe context

    https://stackoverflow.com/questions/25953887/how-to-use-unsafe-code-in-safe-contex I am not sure if ...

  5. redux异步

    在一个项目中 redux 是必不可少的,redux 中没有提供异步的操作,但是异步又是项目开发中重要的一部分,所以我们的 redux 对此有进行了拓展: 所以我们需要 redux-thunk 的插件, ...

  6. React 番外篇

    小技巧:如果我们想了解一门技术,不知道如何学习,那就在 BOSS 直聘上,来看看对这门技术的要求 这篇给大家讲的是 React 1.0 的初始版本,仅仅是让大家有个了解,毕竟回顾历史,我们才能找到他最 ...

  7. python hash 每次调用结果不一样

    import time import multiprocessing device = ['3695a1c7-0fa6-4fa8-a563-8fd462c04af5', '0dfdd431-f9bc- ...

  8. [SQL Server常用系统存储过程大全]

    1.  sp_help   报告有关数据库对象(sys.sysobjects 兼容视图中列出的所有对象)  sp_help    表名称,存储过程名称等 2.  sp_helpdb   报告有关数据库 ...

  9. Node JS复制文件

    /** * Created by Administrator on 2019/11/6. *指尖敲打着世界 ----一个阳光而又不失帅气的少年!!!. */ var fs=require(" ...

  10. [shell]上一个命令执行完成,才执行下一个操作 | shell脚本中判断上一个命令是否执行成功

    shell脚本中判断上一个命令是否执行成功  shell中使用符号“$?”来显示上一条命令执行的返回值,如果为0则代表执行成功,其他表示失败.结合if-else语句实现判断上一个命令是否执行成功. 场 ...