CF459E Pashmak and Graph (Dag dp)
解题思路
\(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)的更多相关文章
- CF459E Pashmak and Graph (DP?
Codeforces Round #261 (Div. 2) E - Pashmak and Graph E. Pashmak and Graph time limit per test 1 seco ...
- cf459E Pashmak and Graph
E. Pashmak and Graph time limit per test 1 second memory limit per test 256 megabytes input standard ...
- Codeforces Round 261 Div.2 E Pashmak and Graph --DAG上的DP
题意:n个点,m条边,每条边有一个权值,找一条边数最多的边权严格递增的路径,输出路径长度. 解法:先将边权从小到大排序,然后从大到小遍历,dp[u]表示从u出发能够构成的严格递增路径的最大长度. dp ...
- Codeforces 459E Pashmak and Graph:dp + 贪心
题目链接:http://codeforces.com/problemset/problem/459/E 题意: 给你一个有向图,每条边有边权. 让你找出一条路径,使得这条路径上的边权严格递增. 问你这 ...
- codeforces 459 E. Pashmak and Graph(dp)
题目链接:http://codeforces.com/contest/459/problem/E 题意:给出m条边n个点每条边都有权值问如果两边能够相连的条件是边权值是严格递增的话,最长能接几条边. ...
- Pashmak and Graph(dp + 贪心)
题目链接:http://codeforces.com/contest/459/problem/E 题意:给一个带权有向图, 找出其中最长上升路的长度. 题解:先按权值对所有边排序, 然后依次 u -& ...
- CodeForces - 459E Pashmak and Graph[贪心优化dp]
E. Pashmak and Graph time limit per test 1 second memory limit per test 256 megabytes input standard ...
- Codeforces 459E Pashmak and Graph(dp+贪婪)
题目链接:Codeforces 459E Pashmak and Graph 题目大意:给定一张有向图,每条边有它的权值,要求选定一条路线,保证所经过的边权值严格递增,输出最长路径. 解题思路:将边依 ...
- 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 ...
随机推荐
- Java分页查询工具类
public class PageList<T> { private int totalpage; //总页数 private int totalcount; //总记录数 private ...
- thinkphp 模板文件
因为模板文件中可能会泄露数据表的字段信息,有两种方法可以保护你的模板文件不被访问到: 第一种方式是配置.htaccess文件,针对Apache服务器而言. 大理石平台厂家 把以下代码保存在模块的模板目 ...
- 2019 hdu多校1
A:一类线性dp,时间卡的有点紧 /* 定义 dp[t][i][j][k]代表填完前 t 个位置后,{0, 1, 2, 3} 这 4 个数字最后一次出现的位置, 排序后为 t, i, j, k(t & ...
- kafka和flume进行整合的日志采集的confi文件编写
配置flume.conf 为我们的source channel sink起名 a1.sources = r1 a1.channels = c1 a1.sinks = k1 指定我们的source收集到 ...
- JS常见的报错类型
解决错误前,首先要学会阅读报错信息 eg:Uncaught TypeError: ... is not a function Uncaught 表示没有被catch语句捕获到的错误 TypeError ...
- redis笔记--------Jedis使用
redis安装和启动就不说了 一.准备工作 1.redis -cli -p 6379 2.eclipse中新建项目,并导入jedis相关包 3.测试jedis连通性 二.Jedis常用API (哈希) ...
- springmvc静态资源;mvc:default-servlet-handler后Controller失效
springmvc静态资源;mvc:default-servlet-handler后Controller失效 web.xml配置<url-pattern>/</url-pattern ...
- nginx启停脚本
安装nginx时,源码包中未带官方的启动脚本,也就无法使用service nginxd start这种启动方式,查了下资料自己写了一个: #!/bin/bash #@version: #@author ...
- DXP 快捷键
EC 复制 EY array paste TU: update
- VS新建工程或者新建项时 出现未定义标识符号
VS新建工程或者新建项时 出现未定义标识符号,编译之后不影响运行,但是看着很不舒服,影响效率. 解决办法:属性--->VC++目录-->包含目录-->编辑,将自己所用QT的inclu ...