题目大意

给你两棵树,结点分别是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. Servlet——理解会话Session

    1.什么是会话(Session) 超文本传输协议(HTTP)被设计成一种无状态的协议. 所谓无状态协议就是指在服务器端的请求彼此相互之间是不认识彼此的,哪怕是来自同一个客户端的请求,相互之间也是不认识 ...

  2. 【编程漫谈】PHP

    PHP是个很古老的脚本技术了,当年CGI比较让人诟病,于是PHP横空出世.PHP即写即用特性,吸引了一大批粉丝,而且类似C语言的编程风格,让那些C程序员非常容易地转到这个平台上来.当然PHP刚出来的时 ...

  3. tomcat8踩坑:url包含|等特殊字符报错400的问题

    这个问题纠缠了我很久了,终于在今天早上解决了,感谢自己的不放弃和不断尝试的决心,我坚信,我可以找到解决方式!! 项目用的spring boot+spring security框架,关于统一错误页面在开 ...

  4. 【React自制全家桶】二、分析React的虚拟DOM和Diff算法

    一.React如何更新DOM内容: 1.  获取state 数据 2.  获取JSX模版 3.  通过数据 +模版结合,生成真实的DOM, 来显示,以下行代码为例(简称代码1) <div id= ...

  5. Struts2中国际化

    1.  写资源文件 Msg.properties   默认的语言环境: 找不到配置就找它 Msg_en_US.properties  美国 2.  加载 <constant name=" ...

  6. 网络通信框架之okHttp

    主页: https://github.com/square/okhttp 特点: * 支持HTTP/2 和 SPDY * 默认支持 GZIP 降低传输内容的大小 * 支持网络请求的缓存 * 当网络出现 ...

  7. How to run a VBA macro when new mail is received in Outlook

    It can be very useful to run a VBA macro when new mail is received in Outlook. A customer asked me t ...

  8. JMeter使用plugins插件进行服务器性能监控

    JMeter使用plugins插件进行服务器性能监控 性能测试时,我们的关注点有两部分 1 服务本身:并发响应时间 QPS 2 服务器的资源使用情况:cpu memory I/O disk等 JMet ...

  9. Redis ==> 高级

    一.发布订阅 Redis 发布订阅(pub/sub)是一种消息通信模式:发送者(pub)发送消息,订阅者(sub)接收消息. Redis 客户端可以订阅任意数量的频道. 下图展示了频道 channel ...

  10. ubuntu安装成功之后需要做些什么?

    1.安装VMtool 1.1打开虚拟机之后-> 安装VMtool 1.2 点击之后,桌面就会出现一个VMtool光驱文件,如果提示光驱被占用就先用root登录 1.3在命令行挂载 sudo mo ...