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 第一行 ...
随机推荐
- 深入理解C指针----学习笔记
深入理解C指针 第1章 认识指针 理解指针的关键在于理解C程序如何管理内存,指针包含的就是内存地址. 1.1 指针和内存 C程序在编译后,以三种方式使用内存: 1. 静态. ...
- 64_p5
php-nette-bootstrap-2.4.3-1.fc26.noarch.rpm 20-Feb-2017 07:19 16290 php-nette-caching-2.5.3-1.fc26.n ...
- u-boot中的Makefile
在windos下,pc机上电之后,BIOS会初始化硬件配置,为内核传递参数,引导操作系统启动,并且识别C盘.D盘.等整个操作系统启动起来之后,才可以运行应用程序比如QQ.QQ音影.同理,在嵌入式Lin ...
- 报错:Cobbler check 时报错
报错:[root@test88 ~]# cobbler checkTraceback (most recent call last): File "/usr/bin/cobbler&quo ...
- Python编程规范精简版
用四个空格缩进,不要用tab键:四个空格是在较小缩进(可以允许更大的嵌套深度)和较大缩进(可读性更好)之间的一个很好的折中.制表符会带来混乱,最好不要使用: 包装行保证每行不超过79个字符:这对那些使 ...
- BootStrap的table表格的基本写法
代码如下: <!DOCTYPE html> <html> <head> <title>BootStrap的基础入门</title> < ...
- python基础(9)--递归、二叉算法、多维数组、正则表达式
1.递归 在函数内部,可以调其他函数,如果一个函数在内部调用它本身,这个函数就是递归函数.递归算法对解决一大类问题是十分有效的,它往往使算法的描述简洁而且易于裂解 递归算法解决问题的特点: 1)递归是 ...
- AndroidStudio升到最新版本(3.1.2)之后
暂时发现的需要大家注意的地方 1.androidstudio3无法导入moudle? 例如:我写了一个简单的项目,需要导入一个第三方的moudle,我导入: 因为AS升级之后,没有突出颜色的变化(变黑 ...
- Typecho-反序列化漏洞学习
目录 Typecho-反序列化漏洞学习 0x00 前言 0x01 分析过程 0x02 调试 0x03 总结 0xFF 参考 Typecho-反序列化漏洞学习 0x00 前言 补丁: https://g ...
- tornado中的cookie
1. cookie与session的区别: Session:通过在服务器端记录用户信息从而来确认用户身份,保存在服务器上,每个用户会话都有一个对应的session Cookie:通过在客户端记录信息确 ...