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 ...
随机推荐
- cocos2d-x step by step(3) Double Kill
喏,咱们已经调通hello world 了,然后呢,咱们做一些高大上的东西,那做什么呢,做一个打乒乓球的小东西,啊哈! 这就是最终界面了,没画一个球形 就用一个白色方框代替吧. 啊哈! public ...
- Android动画中Interpolator 详解和演示
遇到一个项目需求,想让动画变得更活泼一点,于是想到了动画属性中的Interpolator,写了基本例子测试一下Android提供给我们现成的加速器的效果: 效果 代码中方法 xml中属性 越来越快 A ...
- linux查看系统CPU,内存,硬盘使用情况
top查看CPU,内存使用情况 free查看硬盘使用情况
- 基于HTML5的PACS--HTML5图像处理
在此之前,此系统是结合DICOM的WADO标准,在浏览器里通过javascript操作返回的JPG图片.这种服务器端解析,客户端展现的方式,对实现图像的移动.缩放.旋转.测量等图像操作能够实现实时的交 ...
- java 图片加水印,设置透明度。说明非常具体
package com.yidao.common; import java.awt.AlphaComposite; import java.awt.Graphics2D; import java.aw ...
- overflow滚动条样式设置,ie和webkit内核
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...
- 随便写一点最近开发遇到的问题和解决方法 大部分关于laravel和php
laravel里要想对对象进行自己设计的排序(usort()), 得用匿名方法, 原声php就不用 php里面可以随便写html代码, 比如可以把html直接后缀名改成.php, 然后在任何地方& ...
- iphone手机连接USB时出现须要Mobile device setup disk上的usbaapl.sys文件
问题: iphone5 手机连接USB出现例如以下弹框 watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvYW5nZWwyMnh1/font/5a6L5L2T/ ...
- linux上查看系统内核版本命令(转载)
uname -a uname -r 查看发行版本信息: 在RedHat系统里,存在一个/etc/redhat-release文件,里面保存了发行版的版本信息 $cat /etc/redhat-rele ...
- 目标检测之hough forest---霍夫森林(Hough Forest)目标检测算法
Hough Forest目标检测一种比较时兴的目标检测算法,Juergen Gall在2009的CVPR上提出. Hough Forest听上去像hough变换+Random Forest的结合体, ...