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 ...
随机推荐
- canvas的基础入门
canvas是定义在浏览器上的画布.它不仅仅是一个标签元素更是一个编程工具是一套编程的接口.利用它可以开发出很多东西,比如动画,游戏,动态的图表等富有变现力和感染力的应用.还可以开发出绚丽的3D动态效 ...
- eclipse 在复制/粘贴 时很卡(转)
最近发现eclipse在按Ctrl+C 时卡的要命,在网上找了一下,发现原来如此: 打开选项: Window -> Preferences -> General -> Editors ...
- cpu相关信息查看
查看CPU型号:grep "model name" /proc/cpuinfo | uniqdmidecode -s processor-version | uniq 查看物理CP ...
- json转字符串,json转list,json转pojo的工具类
package com.loveshop.util; import java.util.List; import com.fasterxml.jackson.core.JsonProcessingEx ...
- H5在js中向指定的元素添加样式
今天在做一个按钮的功能控制,点击之后,要根据判断条件,修改按钮的样式,然后就发现了一个巨好用的方法, <button type="button" id="btn_A ...
- 初识webSocket及其使用
阅读目录 1.什么是webSocket? 2.webSocket实现原理 3.webSocket优点 4.webSocket和socket的区别 5.webSocket API 6.webSocket ...
- echarts图表自适应,容器宽度设置为百分比,但是图表显示不全,缩到一起
<div id="chartContainer" style="height:100%;width:100%;"></div> cha ...
- Foxmail公司邮箱配置
1.打开Foxmail点击新建输入账户密码,点击创建: 2.勾选IMAP服务器的ssl,修改SMTP服务器端口为587 点击应用,账号创建完成.可以拉取和发送邮件了:
- leetcood学习笔记-501- 二叉搜索树中的众数
题目描述: 方法一: class Solution: def findMode(self, root: TreeNode) -> List[int]: if not root: return [ ...
- 模拟字典序排序——hdu6034
#include <bits/stdc++.h> #include <iostream> using namespace std; ; ; int n; int maxj; s ...