Codeforces 459E Pashmak and Graph

时间:2021-05-04 00:20:12

http://www.codeforces.com/problemset/problem/459/E

题意:

给出n个点,m条边的有向图,每个边有边权,求一条最长的边权上升的路径的长度。

思路:用f存边,g存点,然后排序转移,注意相同的要延迟转移

 #include<cstdio>
#include<cmath>
#include<algorithm>
#include<cstring>
#include<iostream>
struct edge{
int u,v,w;
}e[];
int n,m,f[],g[];
int read(){
int t=,f=;char ch=getchar();
while (ch<''||ch>''){if (ch=='-') f=-;ch=getchar();}
while (''<=ch&&ch<=''){t=t*+ch-'';ch=getchar();}
return t*f;
}
bool cmp(edge a,edge b){
return a.w<b.w;
}
int main(){
n=read();m=read();
for (int i=;i<=m;i++){
e[i].u=read();e[i].v=read();e[i].w=read();
}
std::sort(e+,e++m,cmp);
int t=,ans=;
for (int i=;i<=m;i++){
f[i]=g[e[i].u]+;
if (e[i].w!=e[i+].w){
for (int j=t;j<=i;j++)
g[e[j].v]=std::max(g[e[j].v],f[j]);
t=i+;
}
ans=std::max(ans,f[i]);
}
printf("%d\n",ans);
return ;
}