您的位置:首页 > 其它

SCU 4427 Miss Zhao's Graph (dp)

2015-04-09 21:15 393 查看
题目链接

4427: Miss Zhao's Graph

Submit your solution

Discuss this problem
Best solutions

Time: 1000ms

The Problem

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*10^5;1<=m<=min(n*(n-1),3*10^5)).

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.

Output

Print a single integer. The length of the path.

Example 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

Example Output

3
6

Author

mrxy56


题意:求一个有向图的边权严格上升的最长路径。

题解:将边排序,然后再dp。用dp[i] 表示到当前的边,以i为终点的最长的路径。但是由于前面的边可能等于当前这条边。所以对于每一个dp[i],我们要记录当前最优值对应的边长,还要记录每个dp[i]的次优值(这个次优值对应的边长要严格小于最优值对应的边长)。代码如下:

#include<cstdio>
#include<cstring>
#include<set>
#include<iostream>
#include<map>
#include<cmath>
#include<string>
#include<vector>
#include<queue>
#include<cctype>
#include<algorithm>
#define inf 0x3fffffff
#define nn 310000
#define mod 1000000007
typedef long long LL;
using namespace std;
int n,m;
struct node
{
int st,en,len;
}a[nn];
bool cmp(node x,node y)
{
return x.len<y.len;
}
pair<int,int>dp[nn][2];
int main()
{
int i;
int t;
scanf("%d",&t);
while(t--)
{
scanf("%d%d",&n,&m);
for(i=1;i<=m;i++)
{
scanf("%d%d%d",&a[i].st,&a[i].en,&a[i].len);
}
sort(a+1,a+m+1,cmp);
for(i=1;i<=n;i++)
{
dp[i][0]=make_pair(0,-inf);
dp[i][1]=make_pair(-inf,-inf);
}
int u,v;
int ans=-inf;
for(i=1;i<=m;i++)
{
u=a[i].st,v=a[i].en;
if(dp[u][0].second<a[i].len)
{
if(dp[u][0].first+1>dp[v][0].first)
{
if(dp[v][0].second<a[i].len)
dp[v][1]=dp[v][0];
dp[v][0]=make_pair(dp[u][0].first+1,a[i].len);
}
}
else
{
if(dp[u][1].first+1>dp[v][0].first)
{
if(dp[v][0].second<a[i].len)
dp[v][1]=dp[v][0];
dp[v][0]=make_pair(dp[u][1].first+1,a[i].len);
}
}
}
for(i=1;i<=n;i++)
ans=max(ans,dp[i][0].first);
printf("%d\n",ans);
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: