题解 [51nod1274] 最长递增路径
解析
这题一眼DP啊.
然而想了半天毫无思路.
后来看题解后发现可以按边权的大小顺序DP.
将边权从小到大排序,对于权值相同的边分为一组.
设\(f[i][0]\)表示经过当前权值的边后到达\(i\)的最长路,
\(f[i][1]\)表示经过之前的权值的边后到达\(i\)的最长路.
那么对于一条边\(x,y\),
\(f[x][0]=max(f[x][0],f[y][1]+1\)),
\(f[y][0]=max(f[y][0],f[x][1]+1\)).
在处理完每组边后用\(f[i][0]\)去更新\(f[i][1]\)就行了.
code:
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#define filein(a) freopen(a".cpp","r",stdin)
#define fileout(a) freopen(a".cpp","w",stdout);
using namespace std;
inline int read(){
int sum=0,f=1;char c=getchar();
while((c<'0'||c>'9')&&c!=EOF){if(c=='-') f=-1;c=getchar();}
while(c>='0'&&c<='9'&&c!=EOF){sum=sum*10+c-'0';c=getchar();}
return sum*f;
}
const int N=50001;
struct edge{int x,y,w;}e[N];
int n,m;
int f[N][2];
inline bool cmp(edge a,edge b){return a.w<b.w;}
int main(){
n=read();m=read();
for(int i=1;i<=m;i++)
e[i].x=read(),e[i].y=read(),e[i].w=read();
sort(e+1,e+m+1,cmp);
for(int l=1,r=1;l<=m;l=r=r+1){
while(e[r+1].w==e[r].w) r++;
for(int i=l;i<=r;i++){
int x=e[i].x,y=e[i].y;
f[x][0]=max(f[x][0],f[y][1]+1);
f[y][0]=max(f[y][0],f[x][1]+1);
}
for(int i=l;i<=r;i++){
f[e[i].x][1]=f[e[i].x][0];
f[e[i].y][1]=f[e[i].y][0];
}
}
int ans=0;
for(int i=0;i<n;i++) ans=max(ans,f[i][1]);
printf("%d\n",ans);
return 0;
}
题解 [51nod1274] 最长递增路径的更多相关文章
- 【题解】最长递增路径 [51nod1274]
[题解]最长递增路径 [51nod1274] 传送门:最长递增路径 \([51nod1274]\) [题目描述] 一个可能有自环有重边的无向图,每条边都有边权.输入两个整数 \(n,m\) 表示一共 ...
- 51nod1274 最长递增路径
将边排序后dp一下就可以了. #include<cstdio> #include<cstring> #include<cctype> #include<alg ...
- [LeetCode] Longest Increasing Path in a Matrix 矩阵中的最长递增路径
Given an integer matrix, find the length of the longest increasing path. From each cell, you can eit ...
- [Swift]LeetCode329. 矩阵中的最长递增路径 | Longest Increasing Path in a Matrix
Given an integer matrix, find the length of the longest increasing path. From each cell, you can eit ...
- Leetcode 329.矩阵中的最长递增路径
矩阵中的最长递增路径 给定一个整数矩阵,找出最长递增路径的长度. 对于每个单元格,你可以往上,下,左,右四个方向移动. 你不能在对角线方向上移动或移动到边界外(即不允许环绕). 示例 1: 输入: n ...
- Leetcode之深度优先搜索(DFS)专题-329. 矩阵中的最长递增路径(Longest Increasing Path in a Matrix)
Leetcode之深度优先搜索(DFS)专题-329. 矩阵中的最长递增路径(Longest Increasing Path in a Matrix) 深度优先搜索的解题详细介绍,点击 给定一个整数矩 ...
- LeetCode. 矩阵中的最长递增路径
题目要求: 给定一个整数矩阵,找出最长递增路径的长度. 对于每个单元格,你可以往上,下,左,右四个方向移动. 你不能在对角线方向上移动或移动到边界外(即不允许环绕). 示例: 输入: nums = [ ...
- Java实现 LeetCode 329 矩阵中的最长递增路径
329. 矩阵中的最长递增路径 给定一个整数矩阵,找出最长递增路径的长度. 对于每个单元格,你可以往上,下,左,右四个方向移动. 你不能在对角线方向上移动或移动到边界外(即不允许环绕). 示例 1: ...
- 51nod最长递增路径:(还不错的图)
一个无向图,可能有自环,有重边,每条边有一个边权.你可以从任何点出发,任何点结束,可以经过同一个点任意次.但是不能经过同一条边2次,并且你走过的路必须满足所有边的权值严格单调递增,求最长能经过多少条边 ...
随机推荐
- java访问指示符
是否 可访问 同一package 不同package 当前类 其他类 继承类 其他类 public √ √ √ √ protected √ √ √ × friendly √ √ × × private ...
- 2019南昌网络赛 I. Yukino With Subinterval 树状数组套线段树
I. Yukino With Subinterval 题目链接: Problem Descripe Yukino has an array \(a_1, a_2 \cdots a_n\). As a ...
- 数据结构:BF算法
贴上源代码: #include<iostream> using namespace std; int BF(char S[],char T[]) { int i,j; i = j = 0; ...
- 适合新手的160个creakme(三)
先跑一下,这个程序应该是有定时器,多久之后自动开启,测试一下输入,序列号以字母方式输入会出现类型不匹配,之后程序自动退出 但是如果以数字方式输入序列号,则会出现,Try Again,所以这里序列号应该 ...
- MongoDB环境搭建
MongoDB系列第一课:MongDB简介 MongoDB系列第二课:MongDB环境搭建 MongoDB系列第三课:MongDB用户管理 MongoDB系列第四课:MongoDB数据库.集合.文档的 ...
- Secret的三种形式
Secret ConfigMap这个资源对象是Kubernetes当中非常重要的一个对象,一般情况下ConfigMap是用来存储一些非安全的配置信息,如果涉及到一些安全相关的数据的话用ConfigMa ...
- 基于白名单的Payload
利用 Msiexec 命令DLL反弹 Msiexec是Windows Installer的一部分.用于安装Windows Installer安装包(MSI),一般在运行Microsoft Update ...
- Wannafly挑战赛2D Delete (最短路好题)
大意: 给定DAG, 给定点$S,T$, 每次询问给出点$x$, 求删除$x$后的$S->T$的最短路, 询问之间独立. 删除点$x$的最短路一定要经过一条边$(u,v)$, 满足$u$拓扑序在 ...
- js 替换html
function getInfo(){ var player_name=$("#name").val(); $.ajax({ url: 'get_wefare', method: ...
- Markdown: Syntax Text
Markdown: Syntax Text https://daringfireball.net/projects/markdown/syntax.text Markdown: Syntax ==== ...