codeforces 459E

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

.

题目类型:DP

思路:1. dfs铁定超时,因为一开始没想到办法如何两条边去更新一个点时存储哪一个

2.看到排序之后立马有思路,但是还是超时?!为什么?因为我不是更新的点,是去更新的边,这样一条边可能会被访问很多很多次,而如果访问点的话,一条边只需要被访问一次(当然后来修改为AC的之后一条边可能被访问2次),当然就算不超时,后面也会wa的为什么呢?下面解释、、

3.修改为更新点,wa一次。。因为当更新第一次后,后面相同的wei可能会被放弃,最后面的一组样例就是这样错的,使用vector存储要更新的点

4.改了之后还是wa,因为要更新的点覆盖了之前这个点的更新信息,所以用pair存储就可以了

 #include<iostream>

 #include<cstdio>

 #include<cstring>

 #include<vector>

 #include<cmath>

 #include<map>

 #include<algorithm>

 #define M(a,b) memset(a,b,sizeof(a))

 using namespace std;

 int n,m;

 int res;

 int p[];

 int psave[];

 struct ed

 {

     int from;

     int to;

     int wei;

     bool operator < (const ed& rhs) const

     {

         return wei<rhs.wei;

     }

 }edge[];

 vector<pair<int,int> > g;

 void dp()

 {

     int pre = edge[].wei;

     //cout<<u<<'!'<<endl;

     for(int i = ;i<m;i++)

     {

         if(pre!=edge[i].wei)

         {

             pre = edge[i].wei;

             for(int j = ;j<g.size();j++)

                 if(g[j].second>p[g[j].first]) p[g[j].first] = g[j].second;

             g.clear();

         }

         //cout<<edge[i].from<<' '<<edge[i].to<<' '<<p[edge[i].from]<<' '<<pw[edge[i].from]<<endl;

         if(p[edge[i].from]+>p[edge[i].to])

         {

               g.push_back(make_pair(edge[i].to,p[edge[i].from]+));

               if(p[edge[i].from]+>res) res = p[edge[i].from]+;

         }

     }

     for(int j = ;j<g.size();j++)

             if(psave[g[j].second]>p[g[j].first]) p[g[j].first] = psave[g[j].second];

     return;

 }

 int main()

 {

    while(scanf("%d%d",&n,&m)==)

    {

        int u,v,w;

        res = ;

        M(p,);

        for(int i = ;i<m;i++)

        {

            scanf("%d%d%d",&u,&v,&w);

            edge[i].from = u;

            edge[i].to = v;

            edge[i].wei = w;

        }

        sort(edge,edge+m);

        //  cout<<temu<<endl;

        p[edge[].from] = ;

        dp();

        for(int i = ;i<n;i++)

        {

            if(p[i]>res)

             res = p[i];

        }

        printf("%d\n",res);

    }

    return ;

 }

 /*

 9 8

 1 2 1

 2 3 2

 4 5 1

 5 6 2

 6 3 3

 3 7 4

 7 8 5

 8 9 6

 */

codeforces 459E的更多相关文章

  1. Codeforces 459E Pashmak and Graph(dp+贪婪)

    题目链接:Codeforces 459E Pashmak and Graph 题目大意:给定一张有向图,每条边有它的权值,要求选定一条路线,保证所经过的边权值严格递增,输出最长路径. 解题思路:将边依 ...

  2. Codeforces 459E Pashmak and Graph

    http://www.codeforces.com/problemset/problem/459/E 题意: 给出n个点,m条边的有向图,每个边有边权,求一条最长的边权上升的路径的长度. 思路:用f存 ...

  3. Codeforces 459E Pashmak and Graph:dp + 贪心

    题目链接:http://codeforces.com/problemset/problem/459/E 题意: 给你一个有向图,每条边有边权. 让你找出一条路径,使得这条路径上的边权严格递增. 问你这 ...

  4. CodeForces - 459E Pashmak and Graph[贪心优化dp]

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

  5. codeforces 459E E. Pashmak and Graph(dp+sort)

    题目链接: E. Pashmak and Graph time limit per test 1 second memory limit per test 256 megabytes input st ...

  6. Codeforces 459E Roland and Rose

    本以为是个树形DP,按照树形DP的方法在那里dfs,结果WA到死,因为它存在有向环,不是树,凡是存在环的情况切记不要用树形的方法去做 题目的突破点在于将边排完序之后,用点表示以该点为边结尾的最大长度, ...

  7. LUXURY 7

    A.Little Pony and Expected Maximum Time Limit:1000MS     Memory Limit:262144KB     64bit IO Format:% ...

  8. Codeforces Round #261 (Div. 2) - E (459E)

    题目连接:http://codeforces.com/contest/459/problem/E 题目大意:给定一张有向图,无自环无重边,每条边有一个边权,求最长严格上升路径长度.(1≤n,m≤3 * ...

  9. codeforces的dp专题

    1.(467C)http://codeforces.com/problemset/problem/467/C 题意:有一个长为n的序列,选取k个长度为m的子序列(子序列中不能有位置重复),求所取的k个 ...

随机推荐

  1. plain framework 1 1.0.4 更新 稳定版发布

    PF由于各种因素迟迟不能更新,此次更新主要是更新了以往和上个版本出现的内存问题,该版本较为稳定,如果有用到的朋友请更新至此版本. PF 1.0.4 修复1.0.0.3更新后产生的内存问题,可能导致网络 ...

  2. [LeetCode] Sentence Screen Fitting 调整屏幕上的句子

    Given a rows x cols screen and a sentence represented by a list of words, find how many times the gi ...

  3. 从2G到5G, 基站天线过去与未来

    在蜂窝移动通信系统中,天线是电路信号与空间辐射电磁波的转换器,是移动通信系统的末梢关键组成部分. 从2G到4G,移动基站天线经历了全向天线.定向单极化天线.定向双极化天线.电调单极化天线.电调双极化天 ...

  4. python2.7初学(〇)

    为什么学习Python Python为我们提供了非常完善的基础代码库,覆盖了网络.文件.GUI.数据库.文本等大量内容,被形象地称作“内置电池(batteries included)”.而且pytho ...

  5. 修改hosts文件在本地使域名解析到指定IP

    # Additionally, comments (such as these) may be inserted on individual  # lines or following the mac ...

  6. Django form

    简单的from验证 文件目录结构 urls.py from app1.views import account urlpatterns = [ url(r'^admin/', admin.site.u ...

  7. 用libcurl 登录网站

    libcurl 可以发送和接收HTTP消息,因此可以发送用户名.密码和验证码来登录网站,网上有不少这方面的内容,但不甚完整,我摸索了两天,将其中要点记录下来. 基本步骤 正常访问登录页面,访问时,设置 ...

  8. ABP 索引

    官方网站 Github ABP集合贴 @ kebinet https://www.codeproject.com/articles/1115763/using-asp-net-core-entity- ...

  9. com.panie 项目开发随笔_功能任务设计(2016.12.28)

    (一) 第一个菜单 做什么好呢? 1)上次 在研究的功能 是 爬虫,需要将定时爬虫的任务加进来 2)博客的页面,也需要重新布局出来 3)需要做一个,添加博客的页面 (二) 那就先做博客管理吧! 先添加 ...

  10. 【USACO 3.2】Spinning Wheels(同心圆旋转)

    题意: 5个同心圆,告诉你角速度,每个圆有1至5个楔,告诉你起点和宽度.求最早时间如果有的话使得存在某个角度经过5个圆的楔. 题解: 最重要的是要意识到,360秒钟后,每个圆都回到了原来的位置. 我的 ...