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],每 ...
随机推荐
- substring用法
字符串截取,substring(int beginIndex) 返回一个新的字符串,它是此字符串的一个子字符串. substring(int beginIndex, int endIndex) 返回一 ...
- go初识
for循环 ; i < ; i++ { fmt.Println(i*i) } ls := "agd" for _, arg := range ls{ fmt.Println( ...
- (7)C#连DB2---oledb方式
1安装客户端 安装DbVisualizer Free 客户端软件 2编目 用 win+r 输入 db2cmd 启动命令行 要远程操作数据库,首先要进行编目,分三个步骤: 1. 在客户端建立服务器端数 ...
- 国内90%以上的 iOS 开发者,对 APNs 的认识都是错的
转:http://toutiao.com/a6276578687162040578/?tt_from=weixin&utm_campaign=client_share&app=news ...
- [CSS3] Target HTML Elements not Explicitly set in the DOM with CSS Pseudo Elements (Blockquotes)
Pseudo elements allow us to target elements that are not explicitly set in the DOM. Using ::before : ...
- Solidworks如何创建投影曲线
画好草图之后(草图是在上视基准面上画的)然后点击曲线,投影曲线 面选择要投影的曲面,然后就得到了平面曲线在曲面上的投影得到的空间曲线 注意这种方法对于开环轮廓也是可以用的,比如下面,我定义一个 ...
- WheelView实现省市区三级联动(数据库实现版本号附带完整SQL及数据)
近期在实现收货地址功能,用到了省市区三级联动效果,网上找到一般都是xml或json.数据源陈旧改动麻烦.改动了一下使用数据库方式实现了一下 数据源解决.因为数据量比較大通过初始化批量运行SQL的方式不 ...
- Indri和Terrier搜索引擎的使用
介绍 Indri和Terrier都是开源的搜索引擎,当中Indri作为Lemur项目的一个重要部分,具有强大的查询接口,易建索引,可扩展,高效率等长处.能够在SourceForge Lemur Pro ...
- 终端中的乐趣:6个有趣的Linux命令行工具
文章链接: http://hpw123.net/a/Linux/ruanjiananzhuang/2014/1103/117.html 很多其它文章尽在 http://www.hpw123.net ...
- 【转载】细聊分布式ID生成方法
一.需求缘起 几乎所有的业务系统,都有生成一个记录标识的需求,例如: (1)消息标识:message-id (2)订单标识:order-id (3)帖子标识:tiezi-id 这个记录标识往往就是数据 ...