codeforces 459E E. Pashmak and Graph(dp+sort)
题目链接:
1 second
256 megabytes
standard input
standard output
Pashmak's homework is a problem about graphs. Although he always tries to do his homework completely, he can't solve this problem. As you know, he's really weak at graph theory; so try to help him in solving the problem.
You are given a weighted directed graph with n vertices and m edges. You need to find a path (perhaps, non-simple) with maximum number of edges, such that the weights of the edges increase along the path. In other words, each edge of the path must have strictly greater weight than the previous edge in the path.
Help Pashmak, print the number of edges in the required path.
The first line contains two integers n, m (2 ≤ n ≤ 3·105; 1 ≤ m ≤ min(n·(n - 1), 3·105)). Then, m lines follows. The i-th line contains three space separated integers: ui, vi, wi (1 ≤ ui, vi ≤ n; 1 ≤ wi ≤ 105) which indicates that there's a directed edge with weight wi from vertex ui to vertex vi.
It's guaranteed that the graph doesn't contain self-loops and multiple edges.
Print a single integer — the answer to the problem.
3 3
1 2 1
2 3 1
3 1 1
1
3 3
1 2 1
2 3 2
3 1 3
3
6 7
1 2 1
3 2 5
2 4 2
2 5 2
2 6 9
5 4 3
4 3 4
6 题意: 一个有向图,找出一条最长的路径,这条路径上的每条边权重都严格递增;问最长的长度是多少; 思路: 按边的权重排序,排完后把一层一层的更新; AC代码:
#include <bits/stdc++.h>
/*
#include <vector>
#include <iostream>
#include <queue>
#include <cmath>
#include <map>
#include <cstring>
#include <algorithm>
#include <cstdio>
*/
using namespace std;
#define For(i,j,n) for(int i=j;i<=n;i++)
#define Riep(n) for(int i=1;i<=n;i++)
#define Riop(n) for(int i=0;i<n;i++)
#define Rjep(n) for(int j=1;j<=n;j++)
#define Rjop(n) for(int j=0;j<n;j++)
#define mst(ss,b) memset(ss,b,sizeof(ss));
typedef long long LL;
template<class T> void read(T&num) {
char CH; bool F=false;
for(CH=getchar();CH<''||CH>'';F= CH=='-',CH=getchar());
for(num=;CH>=''&&CH<='';num=num*+CH-'',CH=getchar());
F && (num=-num);
}
int stk[], tp;
template<class T> inline void print(T p) {
if(!p) { puts(""); return; }
while(p) stk[++ tp] = p%, p/=;
while(tp) putchar(stk[tp--] + '');
putchar('\n');
} const LL mod=1e9+;
const double PI=acos(-1.0);
const LL inf=1e18;
const int N=3e5+;
const int maxn=;
const double eps=1e-; int dis[N],temp[N],le[N];
/*
struct Edge
{
int from,to,va,next;
}edge[N];
void add_edge(int s,int e,int w)
{
edge[cnt].next=head[s];
edge[cnt].from=s;
edge[cnt].to=e;
edge[cnt].va=w;
head[s]=cnt++;
}*/
struct PO
{
int u,v,w;
}po[N];
int cmp(PO x,PO y)
{
return x.w<y.w;
}
/*
void dfs(int cur,int fa,int wi)
{
cout<<cur<<" "<<fa<<" "<<wi<<" "<<dis[cur]<<"###"<<endl;
for(int i=head[cur];i!=-1;i=edge[i].next)
{
int y=edge[i].to,w=edge[i].va;
cout<<y<<" "<<w<<"$$$$$"<<endl;
if(w>wi)
{
if(dis[y]<dis[cur]+1)dis[y]=dis[cur]+1,dfs(y,cur,w);
}
}
}*/ int n,m; int main()
{ read(n);read(m);
For(i,,m)
read(po[i].u),read(po[i].v),read(po[i].w);//add_edge(po[i].u,po[i].v,po[i].w);//add_edge(v,u,w); sort(po+,po+m+,cmp);
int cnt=;
le[]=;
For(i,,m)
{
if(po[i].w!=po[i-].w)
{
le[++cnt]=i;
}
}
le[++cnt]=m+; for(int j=;j<=cnt;j++)
{
for(int i=le[j];i<le[j+];i++)
temp[po[i].v]=max(dis[po[i].u]+,temp[po[i].v]);
for(int i=le[j];i<le[j+];i++)
dis[po[i].v]=temp[po[i].v];
}
int ans=;
For(i,,n)
{
ans=max(ans,dis[i]);
}
cout<<ans<<"\n";
return ;
}
codeforces 459E E. Pashmak and Graph(dp+sort)的更多相关文章
- 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 459 E. Pashmak and Graph(dp)
题目链接:http://codeforces.com/contest/459/problem/E 题意:给出m条边n个点每条边都有权值问如果两边能够相连的条件是边权值是严格递增的话,最长能接几条边. ...
- codeforces 459E
codeforces 459E E. Pashmak and Graph time limit per test 1 second memory limit per test 256 megabyte ...
- CodeForces - 459E Pashmak and Graph[贪心优化dp]
E. Pashmak and Graph time limit per test 1 second memory limit per test 256 megabytes input standard ...
- CF459E Pashmak and Graph (DP?
Codeforces Round #261 (Div. 2) E - Pashmak and Graph E. Pashmak and Graph time limit per test 1 seco ...
- cf459E Pashmak and Graph
E. Pashmak and Graph time limit per test 1 second memory limit per test 256 megabytes input standard ...
- Codeforces Round #261 (Div. 2) E (DP)
E. Pashmak and Graph Pashmak's homework is a problem about graphs. Although he always tries to do hi ...
- [Codeforces 865C]Gotta Go Fast(期望dp+二分答案)
[Codeforces 865C]Gotta Go Fast(期望dp+二分答案) 题面 一个游戏一共有n个关卡,对于第i关,用a[i]时间通过的概率为p[i],用b[i]通过的时间为1-p[i],每 ...
随机推荐
- py3.6 + xadmin的自学网站搭建
xadmin安装 由于安装时需要依赖包并且暂时不支持py3等原因,直接pip install的方法很容易就狗带了. 说一下我在网上找到的安装方式,在GitHub上有这个项目的开源代码,并且还有很多个版 ...
- R语言入门--画图(一)--ggplot2
先写一些需要用到的知识点,比如包.函数 dplyr 很好用的包 经常与ggplot2连用 mutate:用于对数据框的列进行重新处理,或者用处理的结果添加新列 数据清洗: 1.na.omit() ...
- BZOJ 1090 字符串折叠(Hash + DP)
题目链接 字符串折叠 区间DP.$f[l][r]$为字符串在区间l到r的最小值 正常情况下 $f[l][r] = min(f[l][r], f[l][l+k-1]+f[l+k][r]);$ 当$l$到 ...
- 转 vs2010转vs2008 其他的一样
如果你使用VS2010的任何版本写代码,那么在VS2008中就不能打开VS2010的解决方案了,为此,通过以下三步就可以解决了一.对于工程名.sln; 1.用你喜欢的编辑器打开sln文件,比如note ...
- Ubuntu下使用UFW配置防火墙(简化iptables的操作)
UFW全称为Uncomplicated Firewall,是Ubuntu系统上配置iptables防火墙的工具.UFW提供一个非常友好的命令用于创建基于IPV4,IPV6的防火墙规则. 但是,UFW是 ...
- Nuxt.js使用mint-ui
环境 vue nuxt 要使用mint-ui 记录下其中的坑 npm install mint-ui --save plugins目录下 增加 mint-ui.js 代码: import Vue fr ...
- Proximal Gradient Descent for L1 Regularization(近端梯度下降求解L1正则化问题)
假设我们要求解以下的最小化问题: $min_xf(x)$ 如果$f(x)$可导,那么一个简单的方法是使用Gradient Descent (GD)方法,也即使用以下的式子进行迭代求解: $x_{k+1 ...
- Deleting array elements in JavaScript - delete vs splice
javascript 数组中删除元素用 array.splice(start, deleteCount);这个方法. ----------------------------------------- ...
- python 3.4读取输入参数
python 3.4读取输入参数 学习了:https://blog.csdn.net/qq_24815615/article/details/52302615 注意,sys.args[0]是pytho ...
- C#复习总结4
第十三章 委托 什么是委托 委托就是函数的指针. 其和类相似,其实就是用户自定义的引用类型. 委托是包含有序方法列表的对象,这些方法具有相同的签名和返回类型. MyDel delvar = new M ...