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. 5-python的封装与结构 - set集合

    目录 1 封装与解构 1.1 封装 1.2 解构 1.3 Python3的解构 2 set类型 2.1 set的定义 2.2 set的基本操作 2.2.1 增加元素 2.2.2 删除元素 2.2.3 ...

  2. [004] last_k_node

    [Description] find the k-th node from the last node of single linked list. e.g. Linked-list: 1-2-3-4 ...

  3. Codeforces Round #502

    Codeforces Round #502 C. The Phone Number 题目描述:求一个\(n\)排列,满足\(LIS+LDS\)最小 solution 枚举\(LIS\),可证明\(LD ...

  4. java获取weblogic应用运行路径

    String url = TemplateBuilder(当前类).class.getClassLoader().getResource("").getPath(); String ...

  5. SipDroid +miniSIPServer搭建SIP局域网语音通话(一)

    最近在做语音通讯功能,参考下优秀开源软件SIPDroid好就这个了,svn check下最新的源代码 http://sipdroid.googlecode.com/svn/trunk/sipdroid ...

  6. nginx allow 多个ip & ipv4的网段表示方法解析

    参考:https://www.baidu.com/link?url=5aVe_syihQzhHnSDAdLsNNQYqDe_W2GYG1GeIQ130e4mEZbusxQfqGVTdg-dJg8fqM ...

  7. Java容器---Arrays & Collections工具类

    1.Array & Arrays 与Collection & Collections区别 (1)Collection": 是一个接口,与其子类共同组成一个Collection ...

  8. 使用CLion

    CLion是JetBrains公司的一款C++的IDE.默认使用Cmake构建. ubuntu和fedora下的安装 在ubuntu下安装了CLion,和QtCreator相比: ibus输入法能输入 ...

  9. 借助Visual Studio Code提高基于ActionScript的LayaAir HTML5游戏的调试效率

    借助Visual Studio Code提高基于ActionScript的LayaAir HTML5游戏的调试效率 使用Visual Studio Code(VS Code)调试的优势 借助VS Co ...

  10. <<Javascript Patterns>>阅读笔记 – 第3章 字面量和构造函数

    对象字面量 首先给出对象字面量的定义语法: 1. 将对象定义在一对括号中(左大括号“{”和右大括号”}”) 2. 对象中以逗号分隔属性和方法. 每个属性或方法以key-value的形式出现, key和 ...