博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
CF459E Pashmak and Graph (DP?
阅读量:6094 次
发布时间:2019-06-20

本文共 3441 字,大约阅读时间需要 11 分钟。

E. Pashmak and Graph
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output

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 n vertices and m 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.

Input

The first line contains two integers n, m (2 ≤ n ≤ 3·105; 1 ≤ m ≤ min(n·(n - 1), 3·105)). Then, m lines follows. The i-th line contains three space separated integers: ui, vi, wi(1 ≤ ui, vi ≤ n; 1 ≤ wi ≤ 105) which indicates that there's a directed edge with weight wi from vertex ui to vertex vi.

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

Output

Print a single integer — the answer to the problem.

Sample test(s)
Input
3 3 1 2 1 2 3 1 3 1 1
Output
1
Input
3 3 1 2 1 2 3 2 3 1 3
Output
3
Input
6 7 1 2 1 3 2 5 2 4 2 2 5 2 2 6 9 5 4 3 4 3 4
Output
6
Note

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 .

大意:给出一个带权有向图,求经过的边权绝对上升的最长路径(可能是非简单路径,即可能经过一个点多次)所包含的边数。

题解:对边按权值排序后,从小到大搞。

设q[x]为已经搞过的边组成的以x点为终点的最长路径包含的边数。

设当前边e[i]为从u到v的边,由于我们是按权值排序好的,只要没有相同的权值,我们就可以q[v]=max(q[v], q[u]+1)。

但是是有相同的权值的,我们直接这样搞,相同权值的可能会连出一条路,是不符合要求的,像第一个样例会输出3,怒萎。

所以相同权值的要特殊搞。我希望相同权值的不是一个个更新,而是一起更新,所以我把相同权值的先不更新,而是压入vector中,统计完这个权值的所有边,再将其一起从vector中取出更新。发现,有些相同权值的边连到的是同一个点,我希望用更长的路来更新这个点,所以对vector排序,后更新更长的。

核心代码如下:

1     S.push_back( pig(e[0].to , 1) ); 2     for(i=1; i
=q[e[i].from]+1)continue;11 S.push_back( pig(e[i].to , q[e[i].from]+1) );12 }

 

全代码:

1 //#pragma comment(linker, "/STACK:102400000,102400000") 2 #include
3 #include
4 #include
5 #include
6 #include
7 #include
8 #include
9 #include
10 #include
11 #include
12 using namespace std;13 #define ll long long14 #define usll unsigned ll15 #define mz(array) memset(array, 0, sizeof(array))16 #define minf(array) memset(array, 0x3f, sizeof(array))17 #define REP(i,n) for(i=0;i<(n);i++)18 #define FOR(i,x,n) for(i=(x);i<=(n);i++)19 #define RD(x) scanf("%d",&x)20 #define RD2(x,y) scanf("%d%d",&x,&y)21 #define RD3(x,y,z) scanf("%d%d%d",&x,&y,&z)22 #define WN(x) prllf("%d\n",x);23 #define RE freopen("D.in","r",stdin)24 #define WE freopen("1biao.out","w",stdout)25 #define mp make_pair26 27 struct Edge {28 int w,from,to,next;29 };30 31 Edge e[311111];32 int en;33 int q[311111];34 int n,m;35 36 struct pig {37 int v,be;38 pig() {}39 pig(int x,int y) {40 v=x;41 be=y;42 }43 };44 45 bool operator <(pig x,pig y) {46 return x.be
S;50 51 void add(int x,int y,int z) {52 e[en].w=z;53 e[en].to=y;54 e[en].from=x;55 en++;56 }57 58 bool cmp(Edge x,Edge y) {59 return x.w
=q[e[i].from]+1)continue;86 S.push_back( pig(e[i].to , q[e[i].from]+1) );87 }88 sort(S.begin(),S.end());89 int maxj=S.size();90 for(int j=0; j
View Code

 

转载于:https://www.cnblogs.com/yuiffy/p/3915993.html

你可能感兴趣的文章
dom4j使用selectSingleNode方法报错
查看>>
搜狗垂搜笔试
查看>>
Android JSON数据解析
查看>>
DEV实现日期时间效果
查看>>
java注解【转】
查看>>
Oracle表分区
查看>>
centos 下安装g++
查看>>
调试、手机-手游开发知识(三)--NDK联机调试-by小雨
查看>>
嵌入式,代码调试----GDB扫盲
查看>>
类斐波那契数列的奇妙性质
查看>>
配置设置[Django]引入模版之后报错Requested setting TEMPLATE_DEBUG, but settings are not configured....
查看>>
下一步工作分配
查看>>
Response. AppendHeader使用大全及文件下载.net函数使用注意点(转载)
查看>>
Wait Functions
查看>>
代码描述10313 - Pay the Price
查看>>
jQuery最佳实践
查看>>
centos64i386下apache 403没有权限访问。
查看>>
下划线的学习5
查看>>
Spring Data JPA教程, 第六部分: Sorting(未翻译)
查看>>
重建二叉树
查看>>