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的子串 ...
随机推荐
- lampp、xampp安装文档
第一步:去官网 看这个介绍http://www.apachefriends.org/zh_cn/xampp-linux.html#1677 第二步:下载安装包 2.1 要区分Linux是32位还是64 ...
- Solr Cloud的搭建使用
Solr的安装下载http://archive.apache.org/dist/lucene/solr/6.4.0/或者直接去官网下载最新版本网页指导 https://cwiki.apache.org ...
- spring boot 读取配置文件(application.yml)中的属性值
在spring boot中,简单几步,读取配置文件(application.yml)中各种不同类型的属性值: 1.引入依赖: <!-- 支持 @ConfigurationProperties 注 ...
- ubuntu更改mysql的编码配置
1.Ctrl+t打开终端 2.输入mysql -u root -p 命令,进入MySQL 输入 SHOW VARIABLES LIKE 'char%'; 查看MySQL编码,有两个不是utf8 3.在 ...
- PHP执行linux系统命令
本文是第一篇,讲述如何在PHP中执行系统命令从而实现一些特殊的目的,比如监控服务器负载,重启MySQL.更新SVN.重启Apache等.第二篇<PHP监控linux服务器负载>:http: ...
- vue.js+koa2项目实战(三)登录注册模态框
登录注册模态框 注: [Vue warn]: Do not use built-in or reserved HTML elements as component id: diaLog 原因:diaL ...
- rtmp 错误 Server error: call to function _checkbw failed
客户端使用rtmp协议与rtmp服务通信如遇到 Server error: call to function _checkbw failed错误 需要在服务端修改代码.如服务端使用的是CrtmpSer ...
- Javascript模式(一) 单例模式
function A(){ // 存储实例对象 var instance; // 重写构造函数,只返回闭包内的局部变量instance A = function(){ return instance; ...
- CentOS6.5下Oracle11G-R2安装、卸载
CentOS6.5下Oracle11G-R2安装.卸载 资源下载地址(包含本人全部安装过程中,系统备份文件):http://download.csdn.net/detail/attagain/7700 ...
- 线性表的链式实现(C++)
相关内容: 线性表的顺序实现 链式实现(C语言) (推荐在GitHub上查看,下面代码只是最初实现,后续如果有修改,由于太麻烦,不会再更改下文,仅仅更新Github上的代码文件) 结点以及链表类的定义 ...