Codeforces Round #261 (Div. 2)

E - Pashmak and Graph

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排序,后更新更长的。

核心代码如下:

     S.push_back( pig(e[].to , ) );
for(i=; i<en; i++) {
if(e[i].w != e[i-].w) {///遇到不同权值的边,则将前一权值的全部边更新
sort(S.begin(),S.end());///使路长的后更新(若有同一个点有多种更新,则会保留路长的)
int maxj=S.size();
for(int j=; j<maxj; j++)
q[S[j].v]=S[j].be;
S.clear();
}
if(q[e[i].to]>=q[e[i].from]+)continue;
S.push_back( pig(e[i].to , q[e[i].from]+) );
}

全代码:

 //#pragma comment(linker, "/STACK:102400000,102400000")
#include<cstdio>
#include<cmath>
#include<iostream>
#include<cstring>
#include<algorithm>
#include<cmath>
#include<map>
#include<set>
#include<stack>
#include<queue>
using namespace std;
#define ll long long
#define usll unsigned ll
#define mz(array) memset(array, 0, sizeof(array))
#define minf(array) memset(array, 0x3f, sizeof(array))
#define REP(i,n) for(i=0;i<(n);i++)
#define FOR(i,x,n) for(i=(x);i<=(n);i++)
#define RD(x) scanf("%d",&x)
#define RD2(x,y) scanf("%d%d",&x,&y)
#define RD3(x,y,z) scanf("%d%d%d",&x,&y,&z)
#define WN(x) prllf("%d\n",x);
#define RE freopen("D.in","r",stdin)
#define WE freopen("1biao.out","w",stdout)
#define mp make_pair struct Edge {
int w,from,to,next;
}; Edge e[];
int en;
int q[];
int n,m; struct pig {
int v,be;
pig() {}
pig(int x,int y) {
v=x;
be=y;
}
}; bool operator <(pig x,pig y) {
return x.be<y.be;
} vector<pig>S; void add(int x,int y,int z) {
e[en].w=z;
e[en].to=y;
e[en].from=x;
en++;
} bool cmp(Edge x,Edge y) {
return x.w<y.w;
} int main() {
int i,x,y,z; scanf("%d%d",&n,&m);
en=;
REP(i,m) {
scanf("%d%d%d",&x,&y,&z);
add(x,y,z);
} sort(e,e+en,cmp);///边从小到大遍历
int ans=;
memset(q,,sizeof(q)); S.push_back( pig(e[].to , ) );
for(i=; i<en; i++) {
if(e[i].w != e[i-].w) {///遇到不同权值的边,则将前一权值的全部边更新
sort(S.begin(),S.end());///使路长的后更新(若有同一个点有多种更新,则会保留路长的)
int maxj=S.size();
for(int j=; j<maxj; j++)
q[S[j].v]=S[j].be;
S.clear();
}
if(q[e[i].to]>=q[e[i].from]+)continue;
S.push_back( pig(e[i].to , q[e[i].from]+) );
}
sort(S.begin(),S.end());
int maxj=S.size();
for(int j=; j<maxj; j++)
q[S[j].v]=S[j].be; for(i=; i<=n; i++)
ans=max(ans,q[i]);
printf("%d\n",ans);
return ;
}

CF459E Pashmak and Graph (DP?的更多相关文章

  1. codeforces 459 E. Pashmak and Graph(dp)

    题目链接:http://codeforces.com/contest/459/problem/E 题意:给出m条边n个点每条边都有权值问如果两边能够相连的条件是边权值是严格递增的话,最长能接几条边. ...

  2. Pashmak and Graph(dp + 贪心)

    题目链接:http://codeforces.com/contest/459/problem/E 题意:给一个带权有向图, 找出其中最长上升路的长度. 题解:先按权值对所有边排序, 然后依次 u -& ...

  3. CF459E Pashmak and Graph (Dag dp)

    传送门 解题思路 \(dag\)上\(dp\),首先要按照边权排序,然后图都不用建直接\(dp\)就行了.注意边权相等的要一起处理,具体来讲就是要开一个辅助数组\(g[i]\),来避免同层转移. 代码 ...

  4. cf459E Pashmak and Graph

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

  5. codeforces 340D Bubble Sort Graph(dp,LIS)

    转载请注明出处: http://www.cnblogs.com/fraud/          ——by fraud  Bubble Sort Graph Iahub recently has lea ...

  6. 【BZOJ-4692】Beautiful Spacing 二分答案 + 乱搞(DP?)

    4692: Beautiful Spacing Time Limit: 15 Sec  Memory Limit: 128 MBSubmit: 46  Solved: 21[Submit][Statu ...

  7. [SHOI2001]化工厂装箱员(dp?暴力:暴力)

    118号工厂是世界唯一秘密提炼锎的化工厂,由于提炼锎的难度非常高,技术不是十分完善,所以工厂生产的锎成品可能会有3种不同的纯度,A:100%,B:1%,C:0.01%,为了出售方便,必须把不同纯度 ...

  8. hdu 4747 Mex( 线段树? 不,区间处理就行(dp?))

    Mex Time Limit: 15000/5000 MS (Java/Others)    Memory Limit: 65535/65535 K (Java/Others)Total Submis ...

  9. 【BZOJ 1019】 1019: [SHOI2008]汉诺塔 (DP?)

    1019: [SHOI2008]汉诺塔 Description 汉诺塔由三根柱子(分别用A B C表示)和n个大小互不相同的空心盘子组成.一开始n个盘子都摞在柱子A上,大的在下面,小的在上面,形成了一 ...

随机推荐

  1. Matlab以特殊分隔符写入txt(dlmwrite)

    clc clear all load('E:\RSWeb\mahoyt数据集\movielens\u.data'); dlmwrite('E:\RSWeb\mahoyt数据集\movielens\ne ...

  2. linux永久更改eth0的ip地址

    linux中永久修改ip和子网掩码,可以用命令也可以直接到文件里修改.这里推荐直接到文件中修改.1.进入网卡配置文件 vi /etc/sysconfig/network-scripts/ifcfg-e ...

  3. response与文件下载

    参考博客: http://www.cnblogs.com/lcpholdon/p/4380980.html http://www.cnblogs.com/mingforyou/p/3281945.ht ...

  4. ubuntu使用备忘

    今天在一个古董机子上(03年的)安装了lubuntu14.04,内存512M,奔腾处理器,好像是什么迅驰不支持linux需要的某项技术pae,安装时提示cpu不支持pae,但可以在内核参数中添加for ...

  5. POJ3735 矩阵

    题意:有n只猫咪,开始时每只猫咪有花生0颗,现有一组操作,由下面三个中的k个操作组成:        1. g i 给i只猫咪一颗花生米        2. e i 让第i只猫咪吃掉它拥有的所有花生米 ...

  6. bash 操作 sqlite3

    首先,这是个奇怪的需求...但是遇到了.我参考后文链接里的方法,做了自己的. 表是自己手动建的,数据库名字叫 new.db: create table test (sn varchar(), name ...

  7. POJ 1061青蛙的约会(拓展欧几里德算法)

    题目链接: 传送门 青蛙的约会 Time Limit: 1000MS     Memory Limit: 65536K Description 两只青蛙在网上相识了,它们聊得很开心,于是觉得很有必要见 ...

  8. 分布式服务框架Zookeeper

    协议介绍 zookeeper协议分为两种模式 崩溃恢复模式和消息广播模式 崩溃恢复协议是在集群中所选举的leader 宕机或者关闭 等现象出现 follower重新进行选举出新的leader 同时集群 ...

  9. PHP的单引号与双引号的区别

    字符串应用 单引号 较好! 在某些特定情况下,单引号的效率比双引号高. PHP把单引号中的数据视为普通字符串,不再处理. 而双引号还要对其中的字符串进行处理,比如遇到$了会把其后的内容视为变量等.

  10. 捉襟见肘之TableView的手势(删除、编辑等)与转场动画手势冲突

    在使用PresentModel的方式进行转场动画时,出现UIPercentDrivenInteractiveTransition和 UITableView的自带手势冲突,问题需要总结,今天系统复习和总 ...