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;
}

转载于:https://www.cnblogs.com/sdfzsyq/p/9866511.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值