题目链接:https://vjudge.net/problem/CodeForces-960F

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 aibi 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

Input
3 3
3 1 3
1 2 1
2 3 2
Output
2
Input
5 5
1 3 2
3 2 3
3 4 5
5 4 0
4 5 8
Output
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个结点和m条带权有向边,其中可以有环、自环。一条合法的路径为:路径上边的给出次序(输入次序)和权值都满足严格递增,问最长的合法路径?

题解:

1.由于路径需满足“边的给出次序递增”,即只能从先给出的边走到后给出的边,且又需满足“权值严格单调递增”,所以就是一道类LIS的题目。

2.为每个点开一棵线段树,由于边只有1e5条,故使用动态开点就不会超内存。线段树以边权作为区间,每个结点需记录三个信息:左、右孩子,以及当前值能形成的最长路径。

3.剩下的操作与LIS或二维偏序差不多了。

代码如下:

 #include <iostream>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <string>
#include <vector>
#include <map>
#include <set>
#include <queue>
#include <sstream>
#include <algorithm>
using namespace std;
typedef long long LL;
const double eps = 1e-;
const int INF = 2e9;
const LL LNF = 9e18;
const int MOD = 1e9+;
const int MAXN = 2e6+; struct node
{
int lson, rson, cnt;
void init()
{
lson = rson = -;
cnt = ;
}
}q[MAXN];
int root[MAXN], tot; void add(int &rt, int l, int r, int val, int cnt)
{
if(rt==-) //动态开点
{
rt = tot++;
q[rt].init();
}
q[rt].cnt = max(q[rt].cnt,cnt);
if(l==r) return; int mid = (l+r)>>;
if(val<=mid) add(q[rt].lson, l, mid, val, cnt);
else add(q[rt].rson, mid+, r, val, cnt);
} int query(int rt, int l ,int r, int x,int y)
{
if(rt==-) return ; //如果此处没有开点,即表明此处没有插入值,直接返回0
if(x<=l&&r<=y) return q[rt].cnt; int mid = (l+r)>>;
int ret = ;
if(x<=mid) ret = max(ret, query(q[rt].lson, l, mid, x, y));
if(y>=mid+) ret = max(ret, query(q[rt].rson, mid+, r, x, y));
return ret;
} int main()
{
int n, m;
while(scanf("%d%d",&n,&m)!=EOF)
{
tot = ;
memset(root, -, sizeof(root));
int ans = ;
for(int i = ; i<=m; i++)
{
int u, v, w;
scanf("%d%d%d",&u,&v,&w);
int max_cnt = query(root[u],-, , -,w-); //最小值为-1,防止w-1溢出范围
ans = max(ans, max_cnt+);
add(root[v], -, , w, max_cnt+);
}
printf("%d\n", ans);
}
}

CodeForces - 960F Pathwalks —— 主席树(n棵线段树)的更多相关文章

  1. 主席树[可持久化线段树](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 ...

  2. Codeforces J. A Simple Task(多棵线段树)

    题目描述: Description This task is very simple. Given a string S of length n and q queries each query is ...

  3. 线段树(单标记+离散化+扫描线+双标记)+zkw线段树+权值线段树+主席树及一些例题

    “队列进出图上的方向 线段树区间修改求出总量 可持久留下的迹象 我们 俯身欣赏” ----<膜你抄>     线段树很早就会写了,但一直没有总结,所以偶尔重写又会懵逼,所以还是要总结一下. ...

  4. Codeforces 765F Souvenirs - 莫队算法 - 链表 - 线段树

    题目传送门 神速的列车 光速的列车 声速的列车 题目大意 给定一个长度为$n$的序列,$m$次询问区间$[l, r]$内相差最小的两个数的差的绝对值. Solution 1 Mo's Algorith ...

  5. Codeforces 1063F - String Journey(后缀数组+线段树+dp)

    Codeforces 题面传送门 & 洛谷题面传送门 神仙题,做了我整整 2.5h,写篇题解纪念下逝去的中午 后排膜拜 1 年前就独立切掉此题的 ymx,我在 2021 年的第 5270 个小 ...

  6. BZOJ_1901_&_ZJU_2112_Dynamic_Rankings_(主席树+树状数组/线段树+(Treap/Splay))

    描述 http://www.lydsy.com/JudgeOnline/problem.php?id=1901 给出一个长度为n的数列A,有m次询问,询问分两种:1.修改某一位置的值;2.求区间[l, ...

  7. CodeForces 587 E.Duff as a Queen 线段树动态维护区间线性基

    https://codeforces.com/contest/587/problem/E 一个序列, 1区间异或操作 2查询区间子集异或种类数 题解 解题思路大同小异,都是利用异或的性质进行转化,st ...

  8. codeforces 811E Vladik and Entertaining Flags(线段树+并查集)

    codeforces 811E Vladik and Entertaining Flags 题面 \(n*m(1<=n<=10, 1<=m<=1e5)\)的棋盘,每个格子有一个 ...

  9. Codeforces 666E Forensic Examination SAM or SA+线段树合并

    E. Forensic Examination http://codeforces.com/problemset/problem/666/E 题目大意:给模式串S以及m个特殊串,q个询问,询问S的子串 ...

随机推荐

  1. Android开发之布局文件里实现OnClick事件关联处理方法

    一般监听OnClickListener事件,我们都是通过Button button = (Button)findViewById(....); button.setOClickLisener....这 ...

  2. 以其他字段作为某一字段的值. 字段长度char_length(?)

    UPDATE t_dealer a INNER JOIN t_dealer b ON a.id=b.id SET a.zihao=b.shortName where a.zihao is null o ...

  3. Foreach嵌套Foreach速度慢优化方案

    有时候这样的效率还可以,但是只要牵涉到操作数据库,那就GAMEOVER.. 最近在维护项目,一个Foreach循环,4分半才能出来结果. 代码: foreach ($content as $key = ...

  4. 性能指标 - OEE

    work center 是指 执行制造作业的资源, 可以是 一个人, 一组人, 一台自动机器, 一组自动机器, 一个半自动机器, 一组半自动机器, 或者是 一个区域组成的生产资源 基本参数 Time ...

  5. c# 推荐5款超实用的.NET性能分析工具

    虽然.NET框架号称永远不会发生内存泄漏,原因是引入了内存回收机制.但在实际应用中,往往我们分配了对象但没有释放指向该对象的引用,导致对象永远无法释放.最常见的情况就是给对象添加了事件处理函数,但当不 ...

  6. c# using三种用法

    http://www.cnblogs.com/fashui/archive/2011/09/29/2195061.html http://www.cnblogs.com/iamv/archive/20 ...

  7. JSF教程(8)——生命周期之Apply Request Values Phase

    当一个组件树在一个postbacks请求中被恢复之后其中每一个组件从request的參数中取得各自的值,这里使用的是processDecodes方法. 这个值会保存在本地的每一个组件中,在源代码中此过 ...

  8. 工作总结 a标签 <a href="/meetingtheme">Back to List</a> 返回上一级 指向 控制器 默认Index @Html.ActionLink("Edit59", "Edit", new { id = item.ID }) 默认当前控制器

    @Html.ActionLink("Back to List", "Index")  ----  <a href="/doctorinfo&qu ...

  9. webapi设置一个Action同时支持get和post请求

    代码如下: [AcceptVerbs("GET", "POST")] public HttpResponseMessage Http([FromUri]Prox ...

  10. win10系统架构调用

    操作系统模型 操作系统有两种模式: 用户模式 内核模式 当用户模式调用系统服务时,CPU执行一个特殊的指令以切换到内核模式(Ring0),当系统服务调用完成时,操作系统切换回用户模式(Ring3).  ...