一开始自己想了一种跑的巨慢。。写了题解的做法又跑的巨快。。一脸懵逼

  显然要求边权递增就不可能经过重复的边了,那么设f[i]为第i条边出发能走多远就好了,这是我一开始的写法,可能dfs冗余状态较多,跑的极慢

#include<iostream>
#include<cstdlib>
#include<cstring>
#include<cstdio>
#include<algorithm>
#include<queue>
#include<cmath>
#include<map>
#define ll long long 
using namespace std;
const int maxn=500010,inf=1e9;
struct poi{int too,dis,pre;}e[maxn];
int n,m,x,y,z,tot,ans;
int last[maxn],dp[maxn];
void read(int &k)
{
    int f=1;k=0;char c=getchar();
    while(c<'0'||c>'9')c=='-'&&(f=-1),c=getchar();
    while(c<='9'&&c>='0')k=k*10+c-'0',c=getchar();
    k*=f;
}
void add(int x,int y,int z){e[++tot].too=y;e[tot].dis=z;e[tot].pre=last[x];last[x]=tot;}
int dfs(int x,int fa)
{
    if(dp[x])return dp[x];dp[x]=1;
    for(int i=last[e[x].too];i;i=e[i].pre)
    if(i!=fa&&e[i].dis>e[x].dis)dfs(i,x),dp[x]=max(dp[x],dp[i]+1);
    return dp[x];
}
int main()
{
    read(n);read(m);
    for(int i=1;i<=m;i++)read(x),read(y),read(z),add(x,y,z),add(y,x,z);
    for(int i=1;i<=tot;i++)if(!dp[i])dfs(i,0);
    for(int i=1;i<=tot;i++)ans=max(ans,dp[i]);
    printf("%d\n",ans);
    return 0;
}
View Code

相关文章:

  • 2022-12-23
  • 2021-11-24
  • 2021-06-27
  • 2021-06-26
  • 2021-10-04
  • 2021-11-24
  • 2022-01-07
  • 2022-12-23
猜你喜欢
  • 2022-12-23
  • 2021-10-30
  • 2021-10-19
  • 2022-02-21
  • 2021-06-04
  • 2021-08-24
  • 2021-09-07
相关资源
相似解决方案