4427: Miss Zhao's Graph

题目连接:

http://acm.scu.edu.cn/soj/problem.action?id=4427

Description

Mr Jiang gives Miss Zhao a problem about graphs. Unfortunately, she is not very good at graph theory.

However, she doesn't want to be looked down upon by Mr Jiang, who is always trying to laugh at her and call her "little fool".

Therefore, she turns to you for help.

There is a weighted directed graph which has n vertices and m edges. You should find a path with maximum number of edges, and the weight of each edge must be strictly greater than the weight of the provious one.

Print the number of edges in the path.

PS: There may be multiple edges with two nodes and self-loops in this graph.

Input

The first line of input is the number of test case.

Then for each case:

The first line contains two integers n,m(2<=n<=3*105;1<=m<=min(n*(n-1),3*105)).

Then, m lines follow. The i-th line contains three integers:

u,v,w(1<=u,v<=n;1<=w<=10^5) which indicates that there's a directed edge with weight w from vertex u to vertex v.

Constraints:

Print a single integer. The length of the path.

Output

For each case output the answer modulo 1000000007 in a single line.

Sample Input

2

3 3

1 2 1

2 3 2

3 1 3

6 7

1 2 1

3 2 5

2 4 2

2 5 2

2 6 9

5 4 3

4 3 4

Sample Output

3

6

Hint

题意

给你一个有向图,然后有边权

问你这个图内最长递增路径的长度是多少

题解:

对于每一条边,我们按照从小到大排序之后,然后直接跑dp就好了

dp[i]表示i点的最长路,由于我们排了序,所以不需要第二维的定义。

对了,要处理一下边权相等的情况,这个可以拿一个tmp去记录一下

代码

#include<bits/stdc++.h>
using namespace std;
const int maxn = 3e5+7;
pair<int,pair<int,int> >E[maxn];
int dp[maxn];
int tmp[maxn];
void init()
{
memset(dp,0,sizeof(dp));
memset(tmp,0,sizeof(tmp));
memset(E,0,sizeof(E));
}
void solve()
{
init();
int n,m;
scanf("%d%d",&n,&m);
for(int i=1;i<=m;i++)
{
int x,y,z;
scanf("%d%d%d",&x,&y,&z);
E[i]=make_pair(z,make_pair(x,y));
}
sort(E+1,E+1+m);
int j = 1;
for(int i=1;i<=m;i++)
{
if(i<=m-1&&E[i+1].first==E[i].first)
continue;
for(int k=j;k<=i;k++)
tmp[E[k].second.second]=max(tmp[E[k].second.second],dp[E[k].second.first]+1);
for(int k=j;k<=i;k++)
dp[E[k].second.second]=tmp[E[k].second.second];
j=i+1;
}
int ans = 0;
for(int i=1;i<=n;i++)
ans = max(ans,dp[i]);
cout<<ans<<endl;
}
int main()
{
int t;
scanf("%d",&t);
while(t--)solve();
return 0;
}

SCOJ 4427: Miss Zhao's Graph dp的更多相关文章

  1. F. Clique in the Divisibility Graph DP

    http://codeforces.com/contest/566/problem/F F. Clique in the Divisibility Graph time limit per test ...

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

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

  3. Codeforces Round #261 (Div. 2) E. Pashmak and Graph DP

    http://codeforces.com/contest/459/problem/E 不明确的是我的代码为啥AC不了,我的是记录we[i]以i为结尾的点的最大权值得边,然后wa在第35  36组数据 ...

  4. Codeforces.566F.Clique in the Divisibility Graph(DP)

    题目链接 \(Description\) 给定集合\(S=\{a_1,a_2,\ldots,a_n\}\),集合中两点之间有边当且仅当\(a_i|a_j\)或\(a_j|a_i\). 求\(S\)最大 ...

  5. Dungeon Game (GRAPH - DP)

    QUESTION The demons had captured the princess (P) and imprisoned her in the bottom-right corner of a ...

  6. 63. Unique Paths II (Graph; DP)

    Follow up for "Unique Paths": Now consider if some obstacles are added to the grids. How m ...

  7. 62. Unique Paths (Graph; DP)

    A robot is located at the top-left corner of a m x n grid (marked 'Start' in the diagram below). The ...

  8. 64. Minimum Path Sum (Graph; DP)

    Given a m x n grid filled with non-negative numbers, find a path from top left to bottom right which ...

  9. SCOJ4427 / TOPOI 4404: Miss Zhao's Graph 解题报告

    题目链接 SCOJ TOPOI 题目描述 Problem 给定一个包含n个顶点m条边的带权有向图,找一条边数最多的路径,且路径上的边的权值严格递增.图中可能有重边和自环. Input Data 第一行 ...

随机推荐

  1. DataFrame衍生新特征操作

    1.DataFrame中某一列的值衍生为新的特征 #将LBL1特征的值衍生为one-hot形式的新特征 piao=df_train_log.LBL1.value_counts().index #先构造 ...

  2. 关于linux系统如何实现fork的研究(二)【转】

    转自:http://www.aichengxu.com/linux/7166015.htm 本文为原创,转载请注明:http://www.cnblogs.com/tolimit/ 引言 前一篇关于li ...

  3. Linux CGI编程基础【整理】

    Linux CGI编程基础 1.为什么使用CGI? 如前面所见,任何的HTML均是静态网页,它无法实现一些复杂的功能,而CGI可以为我们实现.如:a.列出服务器上某个目录中的文件,对目录中的文件进行操 ...

  4. windos8设置cpu数量和内存大小

    转自:http://smilejay.com/2012/03/windows_cpu_memory_setting/ Windows 8(测试版)在作为Xen Guest中的benchmark测试.我 ...

  5. caffe Python API 之上卷积层(Deconvolution)

    对于convolution: output = (input + 2 * p  - k)  / s + 1; 对于deconvolution: output = (input - 1) * s + k ...

  6. TreeSet之定制排序和自然排序

    TreeSet的几大特点: 1.TreeSet中存储的类型必须是一致的,不能一下存int,一下又存string 2.TreeSet在遍历集合元素时,是有顺序的[从小到大](我的理解,如果存的字母,按字 ...

  7. linux 实现自动创建ftp用户并创建文件夹

    创建一个 createuser.sh的脚本文件 #!/bin/sh #传入的文件名 name=$1 #创建该用户所对应的ftp文件夹   /srv/ftp是我的ftp服务器的根目录 mkdir /sr ...

  8. Builder设计模式--改善构造器多个参数时可显著改善可读性

    作为一名程序开发者,设计模式其实一直有在接触,只是没有专门的去学过,所以可能对设计模式没有一个系统的理解.在一次项目中,需要使用到第三方服务商提供的功能,为了尽快的熟悉其功能代码,在官网下了demo来 ...

  9. oracle 一个网站

    http://www.oracle.com/technetwork/cn/articles/11g-pivot-101924-zhs.html

  10. POJ 1392 Ouroboros Snake(数位欧拉)

    题目链接:http://poj.org/problem?id=1392 题目大意:题意看的我头痛,其实跟HDU2894差不多,但是这题要求输出这条路径上第k个数,而不是输出路径. 解题思路:也跟HDU ...