SCOJ 4427: Miss Zhao's Graph dp
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的更多相关文章
- F. Clique in the Divisibility Graph DP
http://codeforces.com/contest/566/problem/F F. Clique in the Divisibility Graph time limit per test ...
- Codeforces 459E Pashmak and Graph(dp+贪婪)
题目链接:Codeforces 459E Pashmak and Graph 题目大意:给定一张有向图,每条边有它的权值,要求选定一条路线,保证所经过的边权值严格递增,输出最长路径. 解题思路:将边依 ...
- Codeforces Round #261 (Div. 2) E. Pashmak and Graph DP
http://codeforces.com/contest/459/problem/E 不明确的是我的代码为啥AC不了,我的是记录we[i]以i为结尾的点的最大权值得边,然后wa在第35 36组数据 ...
- 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\)最大 ...
- Dungeon Game (GRAPH - DP)
QUESTION The demons had captured the princess (P) and imprisoned her in the bottom-right corner of a ...
- 63. Unique Paths II (Graph; DP)
Follow up for "Unique Paths": Now consider if some obstacles are added to the grids. How m ...
- 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 ...
- 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 ...
- SCOJ4427 / TOPOI 4404: Miss Zhao's Graph 解题报告
题目链接 SCOJ TOPOI 题目描述 Problem 给定一个包含n个顶点m条边的带权有向图,找一条边数最多的路径,且路径上的边的权值严格递增.图中可能有重边和自环. Input Data 第一行 ...
随机推荐
- SVM问题再理解与分析——我的角度
SVM问题再理解与分析--我的角度 欢迎关注我的博客:http://www.cnblogs.com/xujianqing/ 支持向量机问题 问题先按照几何间隔最大化的原则引出他的问题为 上面的约束条件 ...
- Arm-kernel 内存收集【转】
转自:http://blog.csdn.net/linyt/article/details/6627664 Linux kernel的内存管理子系统非常复杂,为了深入了解内存管理系统,我打算分多篇文章 ...
- 【转载】C#异常Retry通用类
//Retry机制 public static class Retry { /// <summary> /// 重试零个参数无返回值的方法 /// </summary> /// ...
- 013 GC机制
本文转自:https://www.cnblogs.com/shudonghe/p/3457990.html 最近还是在找工作,在面试某移动互联网公司之前认为自己对Java的GC机制已经相当了解,其他面 ...
- Machine Learning系列--判别式模型与生成式模型
监督学习的任务就是学习一个模型,应用这一模型,对给定的输入预测相应的输出.这个模型的一般形式为决策函数:$$ Y=f(X) $$或者条件概率分布:$$ P(Y|X) $$监督学习方法又可以分为生成方法 ...
- Loadrunner下WebTours系统自带的用户名和密码
打开:http://127.0.0.1:1080/WebTours/ 系统默认自带两个用户名和密码,位于~\WebTours\MercuryWebTours\users: 1.用户名:joe,密码:y ...
- gdb安装
1.卸载原有gdb 以root用户登录 1.1 查询原有gdb包名,执行命令: rpm -q gdb 1.2 卸载原有gdb包,假设gdb包名为gdb-7.0-0.4.16,执行命令:rpm - ...
- caffe多个gpu数据合并到一起
当多GPU树形拓扑构建完毕,数据预缓冲到GPU显存,开始进入多GPU并行训练.Caffe的Solver提供了两个用于多GPU训练的回调函数:on_start()和on_gradient_ready() ...
- bind1st bind2nd的使用
STL中的函数 bind1st. bind2nd用于将一个二元算子转换成一元算子,需要两个 参数,要转换的bf和一个值v. 参考:http://blog.csdn.net/simahao/articl ...
- 寻找并输出11~999之间的回文数m
寻找并输出11~999之间的数m,它满足m.m2和m3均为回文数. 回文:各位数字左右对称的整数. 例如:11满足上述条件 112=121,113=1331 判断一个数是否是回文数的方法:求该数的反序 ...