题目链接: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. Linux下Reids的安装和使用

    简单记录一下 redis的官网:https://redis.io/ 官网介绍: Installation Download, extract and compile Redis with: $ wge ...

  2. 好工具MyEclise2016 CI下载

    地址:http://pan.baidu.com/s/1gfBw9Ab 安装后,点开crack目录,按步骤走. 下面是我安装成功的画面.

  3. Codeforces445A_DZY Loves Chessboard(预处理)

    DZY Loves Chessboard time limit per test 1 second memory limit per test 256 megabytes input standard ...

  4. Win7如何自定义鼠标右键菜单 添加用记事本打开

    鼠标右键用记事本打开.reg Windows Registry Editor Version 5.00 [HKEY_CLASSES_ROOT\*\shell\Notepad] @="用记事本 ...

  5. make -j 4 echo !$

    make -j 4 #以cpu四核编译 !$上一次命令空格后的部分

  6. Bootstrap 模态框、轮播 结合使用

    Bootstrap 模态框和轮播分开使用的教程网上非常多.可是两者结合使用的样例和资料非常少. 两者结合使用时,開始我遇到了不少bug,如今分享给大家. 我的这个样例是把图片轮播嵌入到模态框里. 最后 ...

  7. 国内云引擎平台概览——新浪SAE,阿里ACE,百度BCE

    新浪SAE 平时大家的測试server都是执行在自己的PC上面,用Tomcat或者IIS搭建的本机server. 事实上新浪云平台SinaAppEngine也是挺好用的. 今天总结一下我使用过程中的一 ...

  8. 【转载】通过sqlserver日志恢复误删除的数据

    如果你已经急的焦头烂额,看到这篇文章的时候,请你换个坐姿,深呼吸几次,静下心来将这篇文章读完,也许你的问题迎刃而解. 我遇到的情况是这样的,网站被植入木马,盗取了我的web.config文件,web. ...

  9. JAVA实现KNN分类

    转载请注明出处:http://blog.csdn.net/xiaojimanman/article/details/51064307 http://www.llwjy.com/blogdetail/f ...

  10. 一些Python黑客脚本

    [Github项目地址] https://github.com/threeworld/Python