CodeForces - 960F Pathwalks —— 主席树(n棵线段树)
题目链接: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 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个结点和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棵线段树)的更多相关文章
- 主席树[可持久化线段树](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 ...
- Codeforces J. A Simple Task(多棵线段树)
题目描述: Description This task is very simple. Given a string S of length n and q queries each query is ...
- 线段树(单标记+离散化+扫描线+双标记)+zkw线段树+权值线段树+主席树及一些例题
“队列进出图上的方向 线段树区间修改求出总量 可持久留下的迹象 我们 俯身欣赏” ----<膜你抄> 线段树很早就会写了,但一直没有总结,所以偶尔重写又会懵逼,所以还是要总结一下. ...
- Codeforces 765F Souvenirs - 莫队算法 - 链表 - 线段树
题目传送门 神速的列车 光速的列车 声速的列车 题目大意 给定一个长度为$n$的序列,$m$次询问区间$[l, r]$内相差最小的两个数的差的绝对值. Solution 1 Mo's Algorith ...
- Codeforces 1063F - String Journey(后缀数组+线段树+dp)
Codeforces 题面传送门 & 洛谷题面传送门 神仙题,做了我整整 2.5h,写篇题解纪念下逝去的中午 后排膜拜 1 年前就独立切掉此题的 ymx,我在 2021 年的第 5270 个小 ...
- BZOJ_1901_&_ZJU_2112_Dynamic_Rankings_(主席树+树状数组/线段树+(Treap/Splay))
描述 http://www.lydsy.com/JudgeOnline/problem.php?id=1901 给出一个长度为n的数列A,有m次询问,询问分两种:1.修改某一位置的值;2.求区间[l, ...
- CodeForces 587 E.Duff as a Queen 线段树动态维护区间线性基
https://codeforces.com/contest/587/problem/E 一个序列, 1区间异或操作 2查询区间子集异或种类数 题解 解题思路大同小异,都是利用异或的性质进行转化,st ...
- codeforces 811E Vladik and Entertaining Flags(线段树+并查集)
codeforces 811E Vladik and Entertaining Flags 题面 \(n*m(1<=n<=10, 1<=m<=1e5)\)的棋盘,每个格子有一个 ...
- Codeforces 666E Forensic Examination SAM or SA+线段树合并
E. Forensic Examination http://codeforces.com/problemset/problem/666/E 题目大意:给模式串S以及m个特殊串,q个询问,询问S的子串 ...
随机推荐
- 【温故知新】——HTML基础重要知识点复习
前言:本文是自己在学习课程中的课程笔记,这里用来温故知新的,并非本人原创. 一.HTML快速入门(重点) 1.HTML概述 1.什么是HTML HTML : Hyper Text Markup Lan ...
- tomcat启动项目,起不起来
右键tomcat 选择publish
- 17、Spring Boot普通类调用bean【从零开始学Spring Boot】
转载:http://blog.csdn.net/linxingliang/article/details/52013017 我们知道如果我们要在一个类使用spring提供的bean对象,我们需要把这个 ...
- 2017.04.20 Adams仿真介绍
Adams 仿真 | 验证"隐性机器人模型"概念,提高视觉伺服精度 产品:Adams行业:科研优势: 1.Adams 仿真可精确预测机器人的位置和方位 2.仿真在理论工作验证中起着 ...
- NSIS隐藏窗体标题栏自带的button(最大化,最小化,关闭X)
这个问题实在八月份逛csdn论坛的时候偶然遇到的,当时比較好奇楼主为啥要隐藏关闭button.就顺口问了下,结果楼主已经弃楼.未给出原因,猜着可能是为了做自己定义页面美化,无法改变按纽外观之类的,后来 ...
- Android Canvas之Path操作
接上篇,Android自己定义View工具:Paint&Canvas(二) 上一篇中介绍的Canvas绘制图形仅仅能画一些常规图形(圆.椭圆.矩形等),假设想绘制更复杂的图形.Path神器来了 ...
- 应用程序之Xib自定义Cell
效果展示 结构分析 代码实现 一.效果展示 二.结构分析 1⃣️首先我们让我们的控制器不再继承UIViewController,而是继承UITableViewController.这样就直接遵守了de ...
- Apache上部署Django
0. 部署环境 Ubuntu 14.04 on AliYun Apache 2.4.7 Python 3 [2.7升级3请看http://www.cnblogs.com/manhua/p/423504 ...
- Selenium详解
自动化测试工具,支持多种浏览器.爬虫中主要用来解决JavaScript渲染的问题. 主要是操控流量器,让浏览器做一些点击啊.加载渲染js啊,之类的.
- JS 的引用赋值与传值赋值
这个问题说大不大说小不小,如果你有幸踩了这个坑,一定会找这篇文章,哈哈~ 现说一下JS数字的类型:基本类型和引用类型 先看下下面两个栗子: var a = 30; var b = a; a = 20; ...