【CF459E】 Pashmak and Graph

题目

题目描述
Pashmak’s homework is a problem about graphs. Although he always tries to do his homework completely, he can’t solve this problem. As you know, he’s really weak at graph theory; so try to help him in solving the problem.

You are given a weighted directed graph with nn vertices and mm edges. You need to find a path (perhaps, non-simple) with maximum number of edges, such that the weights of the edges increase along the path. In other words, each edge of the path must have strictly greater weight than the previous edge in the path.

Help Pashmak, print the number of edges in the required path.

输入格式
The first line contains two integers nn , mm (2<=n<=3·10^{5}; 1<=m<=min(n·(n-1),3·10^{5}))(2<=n<=3⋅10
5
; 1<=m<=min(n⋅(n−1),3⋅10
5
)) . Then, mm lines follows. The ii -th line contains three space separated integers: u_{i}u
i

, v_{i}v
i

, w_{i}w
i

(1<=u_{i},v_{i}<=n; 1<=w_{i}<=10^{5})(1<=u
i

,v
i

<=n; 1<=w
i

<=10
5
) which indicates that there’s a directed edge with weight w_{i}w
i

from vertex u_{i}u
i

to vertex v_{i}v
i

.

It’s guaranteed that the graph doesn’t contain self-loops and multiple edges.

输出格式
Print a single integer — the answer to the problem.

题意翻译
给定n 个点,m 条带权边的有向图。 现在请你找一条路径,起点和终点自取,在保证路径上的边权严格递增(即 下一条边的v 严格小于上一条的v)的情况下包含最多的边。 每条边只用一次。请输出路径最多能包含多少条边。

第一行输入2个数字n,m, 表示n 个点m 条有向边。 第2 到m+1 行每行3 个数s,t 和v,表示边的起点、终点、边权。

输入输出样例
输入 #1复制
3 3
1 2 1
2 3 1
3 1 1
输出 #1复制
1
输入 #2复制
3 3
1 2 1
2 3 2
3 1 3
输出 #2复制
3
输入 #3复制
6 7
1 2 1
3 2 5
2 4 2
2 5 2
2 6 9
5 4 3
4 3 4
输出 #3复制
6
说明/提示
In the first sample the maximum trail can be any of this trails: .

In the second sample the maximum trail is .

In the third sample the maximum trail is .

思路

首先我们想到最后这是一个图上最长上升序列,

故考虑把序列上求最长上升子序列的方法搬运过来,

DP,设 f 数组表示每个点结尾的序列最大长度。

然后按理要枚举上一条边进行转移。

但是这样有后效性,即不论怎么樣的 dp 顺序, 都会出现后面的状态影响前面的(考虑环)

怎么办?缩点,显然不行啊,每个 scc 内部怎么做呢。。。

经过一番思考,我们发现可以按照边权从小到大枚举!

(枚举大边时,根据题意小边不能用来转移)

但是注意到要求严格递增,所以我们必须把相同边权的边一起 dp 掉,又为了防止他们互相影响,开一个 g 数组保存临时的转移结果。记得清空 g 哦

代码

#include<bits/stdc++.h>
using namespace std;
#define N 300005
int n, m;
struct edge {
    int x, y, val;
    inline bool operator < (const edge & a) const {
        return val < a.val;
    }
}ed[N];
int f[N], g[N];
int stack[N], top;
int ans;

int main()
{
    n = read(), m = read();
    for (reg int i = 1 ; i <= m ; i ++)
        ed[i] = (edge){read(), read(), read()};
    sort(ed + 1, ed + 1 + m);
    for (reg int i = 1 ; i <= m ; i ++)
    {
        int j = i + 1;
        while(ed[i].val == ed[j].val) j ++;
        for (reg int k = i ; k < j ; k ++)
        {
            g[ed[k].y] = max(g[ed[k].y], f[ed[k].x] + 1);
            stack[++top] = ed[k].y;
        }
        while(top)
        {
            f[stack[top]] = max(f[stack[top]], g[stack[top]]);
            g[stack[top]] = 0;
            top--;
        }
        i = j - 1;
    }
    for (reg int i = 1 ; i <= n ; i ++) ans = max(ans, f[i]);
    cout << ans << endl;
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值