标签:
http://www.codeforces.com/problemset/problem/459/E
题意:
给出n个点,m条边的有向图,每个边有边权,求一条最长的边权上升的路径的长度。
思路:用f存边,g存点,然后排序转移,注意相同的要延迟转移
1 #include<cstdio> 2 #include<cmath> 3 #include<algorithm> 4 #include<cstring> 5 #include<iostream> 6 struct edge{ 7 int u,v,w; 8 }e[300005]; 9 int n,m,f[300005],g[300005]; 10 int read(){ 11 int t=0,f=1;char ch=getchar(); 12 while (ch<‘0‘||ch>‘9‘){if (ch==‘-‘) f=-1;ch=getchar();} 13 while (‘0‘<=ch&&ch<=‘9‘){t=t*10+ch-‘0‘;ch=getchar();} 14 return t*f; 15 } 16 bool cmp(edge a,edge b){ 17 return a.w<b.w; 18 } 19 int main(){ 20 n=read();m=read(); 21 for (int i=1;i<=m;i++){ 22 e[i].u=read();e[i].v=read();e[i].w=read(); 23 } 24 std::sort(e+1,e+1+m,cmp); 25 int t=1,ans=0; 26 for (int i=1;i<=m;i++){ 27 f[i]=g[e[i].u]+1; 28 if (e[i].w!=e[i+1].w){ 29 for (int j=t;j<=i;j++) 30 g[e[j].v]=std::max(g[e[j].v],f[j]); 31 t=i+1; 32 } 33 ans=std::max(ans,f[i]); 34 } 35 printf("%d\n",ans); 36 return 0; 37 }
Codeforces 459E Pashmak and Graph
标签:
原文地址:http://www.cnblogs.com/qzqzgfy/p/5625600.html