题目大意

给你两棵树,结点分别是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. Yum:[Errno 5] [Errno 2] No such file or directory

    出现这样的问题,就是因为之前python2升级到python3之后,yum有些配置文件定位不到之前的python2了,所以这里需要改掉两个配置文件就不会出事了. yum安装软件时,可以连接yum 仓库 ...

  2. SpringMVC工作原理的介绍

    1.用户向服务器发送请求,请求被Spring前端控制Servlet DispatcherServlet捕获 2.DispatcherServlet对请求URL进行解析,得到请求资源标识符(URL).然 ...

  3. 踩坑:VScode 集成 eslint 插件

    本文以 Vue 官方脚手架 Vue-cli 为例: 1. 创建 Vue 项目 注意:Vue-cli 默认给出了 eslint 配置,一路回车即可.最后在安装模块的时候,选择直接安装!我用淘宝镜像安装时 ...

  4. 依赖注入框架之dagger2

    主页: https://github.com/google/dagger 历史 * Dagger1是由Square公司受到Guice(https://github.com/google/guice)启 ...

  5. np.where()

    numpy.where (condition[, x, y]) numpy.where()两种用法 1. np.where(condition, x, y) 满足条件(condition),输出x,不 ...

  6. shell中变量计算

    year=44 1.let,不需要$引用变量 let m=year+3 echo $m 2.(()) m=$((year+3)) 3.[  ],注意两边一定要有空格 m=$[ year+3 ] 4. ...

  7. AWS EC2 外网不能访问的坑

    概述 今天我在 AWS EC2 上配置并启动了 nginx,但是通过外网不能访问,查了一下资料终于解决了,记录下来供以后开发时参考,相信对其它人也有用. 外网访问不了的原因 外网访问不了的原因不外乎有 ...

  8. Tomcat 8.5 配置自动从http跳转https

    1.需要修改Tomcat目录下的conf/server.xml文件两处地方,Http端口,从默认的8080改为80:Https端口从8443改为443:(80\443分别都是http/https默认端 ...

  9. mybatis-generator自动生成代码时,只生成insert方法

    今天使用mybatis-generator自动生成代码时,发现只能生成insert方法, 以前所有的方法都是可以生成的,查看网上解决办法和检查数据库表结构后, 发现2种可以解决的办法: 1.修改myb ...

  10. jquery中this与$(this)的用法区别

    jquery中this与$(this)的用法区别.先看以下代码: $("#textbox").hover( function() { this.title = "Test ...