传送门

解题思路

  \(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. CAS机制是什么?有什么缺点,会出现什么问题

    CAS是英文单词Compare And Swap的缩写,翻译过来就是比较并替换. CAS机制当中使用了3个基本操作数:内存地址V,旧的预期值A,要修改的新值B. CAS的缺点: 1.CPU开销较大 在 ...

  2. WordPress建站要怎样选择适合自己的主机

    目前很多大中小的网站都在使用WordPress进行建站,因为互联网站长都知道WordPress建站是很方便的,简洁的界面,栅格化管理风格,深受互联网站长的喜爱. 现在支持WordPress建站的主机商 ...

  3. Oracle 修改 新增 触发器 针对字段修改 触发器 误删Oracle表、数据、触发器找回 闪回查询

    emmmm 写这个博客心情很复杂,,,本来这个触发器早就写好了,后来发生点事就写个博客当个备份吧,就当留纪念了:话不多数上问题以及SQL: 问题: 在ABONPB表上增加一个触发器,针对车牌号字段做u ...

  4. URLSearchParams接口用来处理浏览器的url

    URLSearchParams 接口定义了一些实用的方法来处理 URL 的查询字符串. URLSearchParams.append()插入一个指定的键/值对作为新的搜索参数. URLSearchPa ...

  5. leetcood学习笔记-235-二叉搜索树的最近公共祖先

    题目描述: 利用二叉搜索树的特点,如果p.q的值都小于root,说明p q 肯定在root的左子树中:如果p q都大于root,说明肯定在root的右子树中,如果一个在左一个在右 则说明此时的root ...

  6. 使用Thread创建线程

    #_author:来童星#date:2019/12/17#使用Thread创建线程import threadingimport timeclass Sunthread(threading.Thread ...

  7. JAVA C 数据类型对应

    { Java—C和操作系统数据类型的对应表 Java Type C Type Native Representation boolean int 32-bit integer (customizabl ...

  8. c++ exit() 函数

    函数用法 编辑 函数名: exit() 所在头文件:stdlib.h(如果是”VC6.0“的话头文件为:windows.h) 功 能: 关闭所有文件,终止正在执行的进程. exit(0)表示正常退出, ...

  9. 01二维背包+bitset优化——hdu5890

    口胡一种别的解法: 三重退背包,g1[j]k]表示不选x的选了j件物品,体积为k的方案数,g[0][0] = 1 , g1[j][k]=dp[j][k]-g1[j-1][k-a[x]] 然后按这样再退 ...

  10. C语言itoa()函数和atoi()函数详解(整数转字符C实现)【转载】

    文章转载自https://www.cnblogs.com/bluestorm/p/3168719.html   C语言提供了几个标准库函数,可以将任意类型(整型.长整型.浮点型等)的数字转换为字符串. ...