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 ...
随机推荐
- TCP通过滑动窗口和拥塞窗口实现限流,能抵御ddos攻击吗
tcp可以通过滑动窗口和拥塞算法实现流量控制,限制上行和下行的流量,但是却不能抵御ddos攻击. 限流只是限制访问流量的大小,是无法区分正常流量和异常攻击流量的. 限流可以控制本软件或者应用的流量大小 ...
- 转: svn服务器路径名修改(不需要全部重新拉取文件)
svn路径名修改之后, 一大波的研发代码都可能面临变更.还有有一个svn relote神器 大家可以借助各自的SVN工具中哦relote命令完成路径的切换,而不需要全部重新download所有的新路径 ...
- POJ 1018 Communication System 题解
本题一看似乎是递归回溯剪枝的方法.我一提交,结果超时. 然后又好像是使用DP,还可能我剪枝不够. 想了非常久,无奈忍不住偷看了下提示.发现方法真多.有贪心,DP,有高级剪枝的.还有三分法的.八仙过海各 ...
- vue2.0 + vux (一)Header 组件
1.main.js import Vue from 'vue' import FastClick from 'fastclick' import VueRouter from 'vue-router' ...
- nginx list directory
使用 http autoindex 模块列出 目录, 例如 需要将 /var/www 下的 resourcepacks 目录以 http 的方式 暴露 这样设置 nginx ...
- android-BroadcastReceive广播接收器
应用可以使用它对外部事件进行过滤,只对感兴趣的外部事件(如当电话呼入时,或者数据网络可用时)进行接收并做出响应.广播接收器没有用户界面.然而,它们可以启动一个activity或service来响应它们 ...
- Python中的列表、元祖、字典
一.列表 一组有序项目的集合.可变的数据类型[可进行增删改查] 列表是以方括号"[]"包围的数据集合,不同成员以","分隔. 列表中能够包括不论什么数据类型,也 ...
- Android系统开发(6)——Linux底层输入输出
一.操作系统的体系结构 计算机是由一堆硬件组成的,操作系统是为了有效的控制这些硬件资源的软件.操作系统除了有效地控制这些硬件资源的分配.并提供计算机执行所须要的功能之外,为了提供程序猿更easy开发软 ...
- nanonets
https://app.nanonets.com/ List of Models IMAGES Images: Image Categorization Beta Input: Image Out ...
- linux上查看系统内核版本命令(转载)
uname -a uname -r 查看发行版本信息: 在RedHat系统里,存在一个/etc/redhat-release文件,里面保存了发行版的版本信息 $cat /etc/redhat-rele ...