CodeForces960F:Pathwalks (主席树+DP)
You are given a directed graph with n nodes and m edges, with all edges having a certain weight.
There might be multiple edges and self loops, and the graph can also be disconnected.
You need to choose a path (possibly passing through same vertices multiple times) in the graph such that the weights of the edges are in strictly increasing order, and these edges come in the order of input. Among all such paths, you need to find the the path that has the maximum possible number of edges, and report this value.
Please note that the edges picked don't have to be consecutive in the input.
Input
The first line contains two integers n and m (1 ≤ n ≤ 100000,1 ≤ m ≤ 100000) — the number of vertices and edges in the graph, respectively.
m lines follows.
The i-th of these lines contains three space separated integers ai, bi and wi (1 ≤ ai, bi ≤ n, 0 ≤ wi ≤ 100000), denoting an edge from vertex ai to vertex bi having weight wi
Output
Print one integer in a single line — the maximum number of edges in the path.
Examples
3 3
3 1 3
1 2 1
2 3 2
2
5 5
1 3 2
3 2 3
3 4 5
5 4 0
4 5 8
3
Note
The answer for the first sample input is 2: . Note that you cannot traverse
because edge
appears earlier in the input than the other two edges and hence cannot be picked/traversed after either of the other two edges.
In the second sample, it's optimal to pick 1-st, 3-rd and 5-th edges to get the optimal answer: .
题意:问树上最长链大小,这条链满足从起点向终点,是按照给出的顺序,而且长度严格递增。
思路:忽略空间时间问题,先假设线段树可以实现。
N棵线段树,每棵开100000个叶子节点,第i棵树的j叶子表示以i节点为尾,最后一条边长度是j的最大链。
合并为主席树。然后DP乱搞。
怎么合并呢? 假设现在新加了一条有向边,(u->v,len),那么把第v棵线段树,长度[1,len-1]的节点都更新node[v][i]=max(node[v][i],node[u][i]+1);
AC in one go!cheers。
#include<bits/stdc++.h>
using namespace std;
const int maxn=;
int rt[maxn],cnt;
struct node
{
int l,r,val;
node(){ l=r=val=;}
node(int L,int R,int V):l(L),r(R),val(V){}
}s[maxn*];
int query(int Now,int L,int R,int l,int r)
{
if(l<=L&&r>=R) return s[Now].val;
int res=,Mid=(L+R)>>;
if(l<=Mid) res=max(res,query(s[Now].l,L,Mid,l,r));
if(r>Mid) res=max(res,query(s[Now].r,Mid+,R,l,r));
return res;
}
void update(int &Now,int pre,int L,int R,int pos,int val)
{
Now=++cnt; s[Now]=node(s[pre].l,s[pre].r,max(s[pre].val,val));
if(L==R) return ; int Mid=(L+R)>>;
if(pos<=Mid) update(s[Now].l,s[pre].l,L,Mid,pos,val);
else update(s[Now].r,s[pre].r,Mid+,R,pos,val);
}
int main()
{
int N,M,i,u,v,c,ans=;
scanf("%d%d",&N,&M);
for(i=;i<=M;i++){
scanf("%d%d%d",&u,&v,&c); c+=; //防止线段树越界
int tmp=query(rt[u],,,,c-);
update(rt[v],rt[v],,,c,tmp+);
ans=max(tmp+,ans);
}
printf("%d\n",ans);
return ;
}
CodeForces960F:Pathwalks (主席树+DP)的更多相关文章
- 【CodeForces】960 F. Pathwalks 主席树+动态规划
[题目]F. Pathwalks [题意]给定n个点m条边的有向图,可能不连通有重边有自环.每条边有编号 i 和边权 wi ,求最长的路径(可以经过重复节点)满足编号和边权都严格递增.n,m,wi&l ...
- CodeForces - 597C:Subsequences (主席树+DP)
For the given sequence with n different elements find the number of increasing subsequences with k + ...
- CodeForces - 960F Pathwalks —— 主席树(n棵线段树)
题目链接:https://vjudge.net/problem/CodeForces-960F You are given a directed graph with n nodes and m ed ...
- 主席树[可持久化线段树](hdu 2665 Kth number、SP 10628 Count on a tree、ZOJ 2112 Dynamic Rankings、codeforces 813E Army Creation、codeforces960F:Pathwalks )
在今天三黑(恶意评分刷上去的那种)两紫的智推中,突然出现了P3834 [模板]可持久化线段树 1(主席树)就突然有了不详的预感2333 果然...然后我gg了!被大佬虐了! hdu 2665 Kth ...
- SRM12 T2夏令营(分治优化DP+主席树 (已更新NKlogN)/ 线段树优化DP)
先写出朴素的DP方程f[i][j]=f[k][j-1]+h[k+1][i] {k<i}(h表示[k+1,j]有几个不同的数) 显然时间空间复杂度都无法承受 仔细想想可以发现对于一个点 i ...
- Codeforces 833B The Bakery(主席树 + 决策单调性优化DP)
题目链接 The Bakery 题目大意:目标是把$n$个数分成$k$组,每个组的值为这个组内不同的数的个数,求$k$个组的值的和的最大值. 题目分析: 这道题我的解法可能和大众解法不太一样……我用主 ...
- BZOJ 4367 [IOI2014]holiday (决策单调DP+主席树+分治)
题目大意:略 题目传送门 神题,不写长题解简直是浪费了这道题 贪心 考虑从0节点出发的情况,显然一直往前走不回头才是最优策略 如果起点是在中间某个节点$s$,容易想到,如果既要游览$s$左边的某些景点 ...
- Codeforces 960 二进制构造子序列 完全二叉树shift模拟 主席树/MAP DP
A #include <bits/stdc++.h> #define PI acos(-1.0) #define mem(a,b) memset((a),b,sizeof(a)) #def ...
- LOJ 6435 「PKUSC2018」星际穿越——DP+倍增 / 思路+主席树
题目:https://loj.ac/problem/6435 题解:https://www.cnblogs.com/HocRiser/p/9166459.html 自己要怎样才能想到怎么做呢…… dp ...
随机推荐
- DICOM:DICOM Print 服务详细介绍
目录(?)[-] 背景 DICOM Print服务数据流 DICOM Print服务各部分关系 DICOM Print服务具体实现 背景: 昨天专栏中发表了一篇关于DICOM Print的博文 ...
- Service 层实现
一.实验介绍 1.1 实验内容 本节课程主要利用 Spring 框架实现 Service 层. 1.2 实验知识点 Spring 框架 1.3 实验环境 JDK1.8 Eclipse JavaEE 二 ...
- XPath可以快速定位到Xml中的节点或者属性。XPath语法很简单,但是强大够用,它也是使用xslt的基础知识。
示例Xml: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 <?xml versio ...
- Optimizer统计信息管理介绍
1. 前言 在我们的日常维护中受理一些一直以来运行得非常好的系统,突然有一天用户反馈没有做不论什么操作,系统的某个功能模块或者是某个报表曾经仅仅须要几秒.但如今须要几分钟或更长的时间都没有返回结 ...
- odoo税金处理
税金可以设置为'税金包含在价格中',或者'税金不包含在价格中'. 在税金计算处理过程中,odoo会将价格/金额按 total_included/ total_exincluded 分开 ...
- iOS_隐藏顶部状态栏
iOS6和iOS7在隐藏 Status Bar 三种方式比較: Storyboard 界面上选中UIViewController,最右边Simulated Metrics找到 Status Bar 设 ...
- 深入Garbage First垃圾收集器(一)术语
Garbage垃圾收集器的原理,在这篇博客中有讲到,可以拿来参考下, Getting Started with the G1 Garbage Collector(译) 另外在这篇博客中也有讲到很多垃圾 ...
- 基于Kubernetes 构建.NET Core技术中台
今天下午在腾讯云+社区社区分享了<基于Kubernetes 构建.NET Core技术中台>,下面是演讲内容的文字实录. 我们为什么需要中台 我们现在处于企业信息化的新时代.为什么这样说呢 ...
- MongoDB连接数与连接优化
默认每个连接数占用10M内存 ulimit -a 查看stack size MongoDB服务器内存要满足 connection overhead + data size + index size 即 ...
- 设置netbeans文件编码格式
在项目ecmall上右键 选择属性,然后在项目属性里设置