Codeforces Round #603 (Div. 2)F. Economic Difficulties
F. Economic Difficulties
题目链接:
https://codeforces.com/contest/1263/problem/F
题目大意:
两棵树,都有n个叶子节点,一棵树正着放,一棵树倒着放,叶子节点从左到右对应装置1,2,3,4...n,问最多能删掉多少条边,使得装置能与两棵树任意一个根节点1相连。
解题思路:
mp[ i ][ j ]是装置 i 到 j 这段区间删除这段连续区间所能删除的最大边数,两个图分开看,算出每一个图中如果不连通这段区间对应的叶子节点所能删除的最大边数,取这两个图的最大值就是mp[ i ][ j ]。dp[ i ]代表前 i 个装置中的最大删除数,因此可以推出递推式dp[ i ]=max(dp[ i ],dp[j] + mp[ j ][ i ] ) 其中j是从0 - i。
代码:
#include <bits/stdc++.h>
using namespace std;
const int N=3e3;
const int maxn=1e9+;
const int minn=;
vector<int>arr[N];//存图
int sz[N];//记录当删除这个点后最多能删除多少条边
int L[N],R[N];//记录第i个节点包含的设备区间中的最左端与最右端
int mp[N][N];//记录选取任意连续区间的设备并删除,可以删除最多几条边(连续是指删除的边在同一个图中)
int dp[N]; int dfs(int a,int pre){
if(a!=){//每个点都代表他上方的那条边,1的上方没有边
sz[a]=;
}
for(int i=;i<arr[a].size();i++){
if(arr[a][i]!=pre){
dfs(arr[a][i],a);
sz[a]+=sz[arr[a][i]];//记录删除这个点会删除多少条边
L[a]=min(L[a],L[arr[a][i]]);//这个点代表区间的左端点
R[a]=max(R[a],R[arr[a][i]]);//右端点
}
}
mp[L[a]][R[a]]=max(mp[L[a]][R[a]],sz[a]);//记录这个区间的最大删除边数
} int main(){
int n,a,b,v;
cin>>n;
for(int i=;i<;i++){
scanf("%d",&a);
for(int i=;i<=N;i++){
arr[i].clear();
L[i]=maxn;//初始化无穷大
R[i]=minn;//0
}
for(int i=;i<a;i++){//存图
scanf("%d",&v);
arr[v].push_back(i+);
arr[i+].push_back(v);
}
for(int i=;i<=n;i++){
scanf("%d",&v);//这个点代表的装置区间为i
R[v]=L[v]=i;
}
sz[]=;
dfs(,);
}
dp[]=;
dp[]=mp[][];
for(int i=;i<=n;i++){
for(int j=;j<=i;j++){
dp[i]=max(dp[i],dp[j]+mp[j+][i]);
}
}
cout<<dp[n]<<endl;
return ;
}
Codeforces Round #603 (Div. 2)F. Economic Difficulties的更多相关文章
- 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 ...
- Codeforces Round #485 (Div. 2) F. AND Graph
Codeforces Round #485 (Div. 2) F. AND Graph 题目连接: http://codeforces.com/contest/987/problem/F Descri ...
- Codeforces Round #486 (Div. 3) F. Rain and Umbrellas
Codeforces Round #486 (Div. 3) F. Rain and Umbrellas 题目连接: http://codeforces.com/group/T0ITBvoeEx/co ...
- Codeforces Round #501 (Div. 3) F. Bracket Substring
题目链接 Codeforces Round #501 (Div. 3) F. Bracket Substring 题解 官方题解 http://codeforces.com/blog/entry/60 ...
- Codeforces Round #499 (Div. 1) F. Tree
Codeforces Round #499 (Div. 1) F. Tree 题目链接 \(\rm CodeForces\):https://codeforces.com/contest/1010/p ...
- 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 ...
- Codeforces Round #603 (Div. 2)
传送门 感觉脑子还是转得太慢了QAQ,一些问题老是想得很慢... A. Sweet Problem 签到. Code /* * Author: heyuhhh * Created Time: 2019 ...
- Codeforces Round #603 (Div. 2) (题解)
A. Sweet Problem (找规律) 题目链接 大致思路: 有一点瞎猜的,首先排一个序, \(a_1>a_2>a_3\) ,发现如果 \(a_1>=a_2+a_3\) ,那么 ...
- Codeforces Round #376 (Div. 2)F. Video Cards(前缀和)
题目链接:http://codeforces.com/contest/731/problem/F 题意:有n个数,从里面选出来一个作为第一个,然后剩下的数要满足是这个数的倍数,如果不是,只能减小为他的 ...
随机推荐
- 精读《What's new in javascript》
1. 引言 本周精读的内容是:Google I/O 19. 2019 年 Google I/O 介绍了一些激动人心的 JS 新特性,这些特性有些已经被主流浏览器实现,并支持 polyfill,有些还在 ...
- Java——ArrayList底层源码分析
1.简介 ArrayList 是最常用的 List 实现类,内部是通过数组实现的,它允许对元素进行快速随机访问.数组的缺点是每个元素之间不能有间隔, 当数组大小不满足时需要增加存储能力,就要将已经有数 ...
- so easy(并查集+unordered_map)
There are nn points in an array with index from 11 to nn, and there are two operations to those poin ...
- JProfiler> ERROR: Invalid license key. Aborting.
用IDEA+Tomcat的方式打开JProfiler,出现错误 1,Event Log 出错 16:10 Application Server was not connected before run ...
- Python 矩阵(线性代数)
Python 矩阵(线性代数) 这里有一份新手友好的线性代数笔记,是和深度学习花书配套,还被Ian Goodfellow老师翻了牌. 笔记来自巴黎高等师范学院的博士生Hadrien Jean,是针对& ...
- [51Nod1850] 抽卡大赛
link $solution:$ 朴素 $dp$,暴力枚举选择 $i$ 号人的第 $j$ 张卡片,朴素 $dp$ 即可,时间复杂度 $O(n^4)$ . 考虑对于朴素 $dp$ 的优化,发现其实是一个 ...
- 4.css3文本属性
1.css3文本属性: ①Color:颜色. ②Text-align:文本水平对齐方式. ⑴Left默认值,right,center,justify两端对齐: ⑵新增start相当于left,end相 ...
- sass和less的对比
); < { ; { { ; } ); } ); } ); // if 条件 @dr: if(@my-option = true, { button { ...
- linux 更改 mysql 管理员口令
#更改口令前,确保 mysql 服务已经正常启动 mysqladmin –u root passwd 新口令 #登陆 mysql ./mysql –u root –p Enter password:新 ...
- man.conf - man的设定资料
描述 man(1) man(1) 会 读 取 本 档 . man.conf 的 内 容 包 含 了 (a) 如 何 建 立 man 搜 寻 路 径 的 资 讯 , (b) man 所 使 用 的 程 ...