传送门

解题思路

  \(dag\)上\(dp\),首先要按照边权排序,然后图都不用建直接\(dp\)就行了。注意边权相等的要一起处理,具体来讲就是要开一个辅助数组\(g[i]\),来避免同层转移。

代码

#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cstdlib> using namespace std;
const int MAXN = 300005; inline int rd(){
int x=0,f=1;char ch=getchar();
while(!isdigit(ch)) {f=ch=='-'?0:1;ch=getchar();}
while(isdigit(ch)) {x=(x<<1)+(x<<3)+ch-'0';ch=getchar();}
return f?x:-x;
} int n,m,f[MAXN],g[MAXN],stk[MAXN],top,ans;
bool vis[MAXN]; struct Edge{
int u,v,w;
friend bool operator<(const Edge A,const Edge B){
return A.w<B.w;
}
}edge[MAXN]; int main(){
n=rd(),m=rd();
for(int i=1;i<=m;i++)
edge[i].u=rd(),edge[i].v=rd(),edge[i].w=rd();
sort(edge+1,edge+1+m);
for(int i=1;i<=m;i++){
int x=edge[i].u,y=edge[i].v;
if(edge[i].w==edge[i-1].w) {
g[y]=max(g[y],f[x]+1);
if(!vis[y]) {vis[y]=1;stk[++top]=y;}
}
else {
while(top) {
vis[stk[top]]=0;
f[stk[top]]=g[stk[top]];
top--;
}
g[y]=max(g[y],f[x]+1);
vis[y]=1;stk[++top]=y;
}
}
while(top) {
vis[stk[top]]=0;
f[stk[top]]=g[stk[top]];
top--;
}
for(int i=1;i<=n;i++) ans=max(ans,f[i]);
printf("%d\n",ans);
return 0;
}

CF459E Pashmak and Graph (Dag dp)的更多相关文章

  1. CF459E Pashmak and Graph (DP?

    Codeforces Round #261 (Div. 2) E - Pashmak and Graph E. Pashmak and Graph time limit per test 1 seco ...

  2. cf459E Pashmak and Graph

    E. Pashmak and Graph time limit per test 1 second memory limit per test 256 megabytes input standard ...

  3. Codeforces Round 261 Div.2 E Pashmak and Graph --DAG上的DP

    题意:n个点,m条边,每条边有一个权值,找一条边数最多的边权严格递增的路径,输出路径长度. 解法:先将边权从小到大排序,然后从大到小遍历,dp[u]表示从u出发能够构成的严格递增路径的最大长度. dp ...

  4. Codeforces 459E Pashmak and Graph:dp + 贪心

    题目链接:http://codeforces.com/problemset/problem/459/E 题意: 给你一个有向图,每条边有边权. 让你找出一条路径,使得这条路径上的边权严格递增. 问你这 ...

  5. codeforces 459 E. Pashmak and Graph(dp)

    题目链接:http://codeforces.com/contest/459/problem/E 题意:给出m条边n个点每条边都有权值问如果两边能够相连的条件是边权值是严格递增的话,最长能接几条边. ...

  6. Pashmak and Graph(dp + 贪心)

    题目链接:http://codeforces.com/contest/459/problem/E 题意:给一个带权有向图, 找出其中最长上升路的长度. 题解:先按权值对所有边排序, 然后依次 u -& ...

  7. CodeForces - 459E Pashmak and Graph[贪心优化dp]

    E. Pashmak and Graph time limit per test 1 second memory limit per test 256 megabytes input standard ...

  8. Codeforces 459E Pashmak and Graph(dp+贪婪)

    题目链接:Codeforces 459E Pashmak and Graph 题目大意:给定一张有向图,每条边有它的权值,要求选定一条路线,保证所经过的边权值严格递增,输出最长路径. 解题思路:将边依 ...

  9. codeforces 459E E. Pashmak and Graph(dp+sort)

    题目链接: E. Pashmak and Graph time limit per test 1 second memory limit per test 256 megabytes input st ...

随机推荐

  1. 什么是URI、URL、URN、URC和Data URI?

    前言 不知道大家有没有电话拨号通过'猫'上网的经历,那时测试网络是否连接,最好的方式就是打开浏览器输入: www.baidu.com 那会管这一连串字母叫' 网址 '.之后上大学(计算机专业),知道了 ...

  2. NetCore2.2使用Nlog自定义日志写入路径配置方式

    在一些特定场景的业务需求下,日志需要写入到不同的路径下提供日志分析.第一种:默认Nlog可以通过日志级别来区分路径,——优点是不需要额外配置,开箱即用——缺点是不够灵活,如果超过级别数量,则不满足需求 ...

  3. 在阿里云 既php和mysql装好之后,如何安装zabbix

    首先找到php.ini这个文件 命令如下 find / -name php.ini 然后参数修改为如下,不改装不了 max_execution_time = 300  memory_limit = 1 ...

  4. loop find column

    declare l_sql varchar2(500); -- variable that contains a query l_c sys_refcursor; -- cursor variable ...

  5. linux 下格式化命令小记

    mkfs.ext4 /dev/sda1    # 格式化为ext4分区mkfs.ext3 /dev/sda1    # 格式化为ext3分区mkfs.ext2 /dev/sda1    # 格式化为e ...

  6. zero udp

    Description UDP transport can only be used with the ZMQ_RADIO and ZMQ_DISH socket types.

  7. bootstrap 基础模板相关信息

    <!DOCTYPE html> <html> <head> <title></title> <link rel="style ...

  8. javascript--判断语句

    1.if...else.. if(m===1){ console.log('1') }else{ console.log('X') } 一般if 里面采用类型全等的运算符. 2.switch var ...

  9. HTML5布局篇

    <!DOCTYPE html> <html> <head> <meta charset="utf-8"/> <title> ...

  10. bzoj1024题解

    [解题思路] 爆搜,状态f(r,x,y)表示剩下r刀,边长为x和y,对于每个状态枚举切成两块后的长度比或宽度比.复杂度o((n/2)n). [参考代码] #include <algorithm& ...