题目连接:http://codeforces.com/contest/459/problem/E

题目大意:给定一张有向图,无自环无重边,每条边有一个边权,求最长严格上升路径长度。(1≤n,m≤3 *10^5)

初见此题觉得以边为点,以点为边,重建一张图,小边权向(通过点)相邻的大边权连边,然后得到一张DAG,跑最长DAG路即可。然而仔细一想,这样新图边数上限可以达到m^2。被否决。

后来想到边权有序化思想(并不知道怎么想到的…可能受kruskal思想影响),按边从大到小排序,然后一条边一条边加入,对于u->v这样一条边,dp[u] = max(dp[u], dp[v] + 1),之所以从大到小排是为了保证状态转移的正确性(与上升路径有关),实际上也就确定了dp的顺序。特殊些的是,对于边权相同的边,不能逐条加入,因为题目要求严格单增,边权相等的时候先加的可能影响后加的转移正确性。因此把边权相同的边同时更新同时加入(用另外一个数组先记下值,再更新dp)。最后答案就是所有点的dp值中取最大。

#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <vector>
#include <algorithm>
#include <cmath>
using namespace std; const int MaxN = 3 * 1e5;
struct EDGE
{
int u, v, w;
}edge[MaxN + 5];
int n, m, res = 0, ans[MaxN + 5], t[MaxN + 5]; bool cmp(EDGE x, EDGE y)
{
return x.w > y.w;
} void Init()
{
scanf("%d%d", &n, &m);
for (int i = 1; i <= m; i++)
scanf("%d%d%d", &edge[i].u, &edge[i].v, &edge[i].w);
sort(edge + 1, edge + m + 1, cmp);
} void Solve()
{
memset(ans, 0, sizeof(ans));
int head = 1, tail;
while (head <= m)
{
tail = head;
while (tail <= m - 1 && edge[tail + 1].w == edge[tail].w) tail++;
for (int i = head; i <= tail; i++) t[edge[i].u] = ans[edge[i].u];
for (int i = head; i <= tail; i++)
t[edge[i].u] = max(t[edge[i].u], ans[edge[i].v] + 1);
for (int i = head; i <= tail; i++) ans[edge[i].u] = t[edge[i].u];
head = tail + 1;
}
for (int i = 1; i <= n; i++) res = max(res, ans[i]);
printf("%d\n", res);
} int main()
{
Init();
Solve();
}

  

Codeforces Round #261 (Div. 2) - E (459E)的更多相关文章

  1. Codeforces Round #261 (Div. 2)[ABCDE]

    Codeforces Round #261 (Div. 2)[ABCDE] ACM 题目地址:Codeforces Round #261 (Div. 2) A - Pashmak and Garden ...

  2. Codeforces Round #261 (Div. 2) B

    链接:http://codeforces.com/contest/459/problem/B B. Pashmak and Flowers time limit per test 1 second m ...

  3. Codeforces Round #261 (Div. 2) E. Pashmak and Graph DP

    http://codeforces.com/contest/459/problem/E 不明确的是我的代码为啥AC不了,我的是记录we[i]以i为结尾的点的最大权值得边,然后wa在第35  36组数据 ...

  4. Codeforces Round #261 (Div. 2)459D. Pashmak and Parmida&#39;s problem(求逆序数对)

    题目链接:http://codeforces.com/contest/459/problem/D D. Pashmak and Parmida's problem time limit per tes ...

  5. Codeforces Round #261 (Div. 2) B. Pashmak and Flowers 水题

    题目链接:http://codeforces.com/problemset/problem/459/B 题意: 给出n支花,每支花都有一个漂亮值.挑选最大和最小漂亮值得两支花,问他们的差值为多少,并且 ...

  6. Codeforces Round #261 (Div. 2)459A. Pashmak and Garden(数学题)

    题目链接:http://codeforces.com/problemset/problem/459/A A. Pashmak and Garden time limit per test 1 seco ...

  7. Codeforces Round 261 Div.2 E Pashmak and Graph --DAG上的DP

    题意:n个点,m条边,每条边有一个权值,找一条边数最多的边权严格递增的路径,输出路径长度. 解法:先将边权从小到大排序,然后从大到小遍历,dp[u]表示从u出发能够构成的严格递增路径的最大长度. dp ...

  8. Codeforces Round 261 Div.2 D Pashmak and Parmida's problem --树状数组

    题意:给出数组A,定义f(l,r,x)为A[]的下标l到r之间,等于x的元素数.i和j符合f(1,i,a[i])>f(j,n,a[j]),求有多少对这样的(i,j). 解法:分别从左到右,由右到 ...

  9. Codeforces Round #261 (Div. 2)

    第一场难得DIV2简单+AK人数多: E:给出一张图,求最多的边数,满足:在这个边的集合中后面的边的权值大于前面的边; 思路:我们将图按权值排列,以为只可能边权值小的跟新权值大的所以对于一条边我们只跟 ...

随机推荐

  1. git学习中遇到的疑难杂症

    GIT仓库如何恢复到前一次提交 一.通过使用Git版本恢复命令reset,可以回退版本 reset命令有3种方式: 1.git   reset   –mixed:此为默认方式,不带任何参数的git r ...

  2. powershell和cmd区别

    Powershell是cmd的超集,换句话说,cmd能做的事情,Powershell都能做,但是Powershell还能额外做许多cmd不能做的活. 主要是系统管理功能.脚本语言和在线帮助更强大,你确 ...

  3. Java中的String、StringBuffer和StringBuilder的区别

    类型  是否可变  线程安全  能否频繁修改  String  不可变  安全  否  StringBuffer  可变  安全  能  StringBuilder  可变  不安全  能 1.可变与 ...

  4. Install NGINX, PHP-FPM (5.6) on CentOS 6

    Installing NGINX with PHP on CentOS 6 can be a hassle depending on the install and packages you use. ...

  5. HikariPool连接池的使用

    HikariDataSource datasource= new HikariDataSource( xxxx ); Connection cn = datasource.getConnection( ...

  6. NET CORE Learning

    ASP.NET Core 基础教程https://www.cnblogs.com/lonelyxmas/tag/ASP.NET%20Core%20%E5%9F%BA%E7%A1%80%E6%95%99 ...

  7. JS数组去重总结

    方法一: 双层循环,外层循环元素,内层循环时比较值 如果有相同的值则跳过,不相同则push进数组 Array.prototype.distinct = function(){ var arr = th ...

  8. tcp头和ip头 图文简介和简要说明

    https://blog.csdn.net/soullsj/article/details/80304124

  9. python_文件的打开和关闭

    文件对象 = open('文件名','使用方式')rt:读取一个txt文件wt: 只写打开一个txt文件,(如果没有该文件则新建该文件)会覆盖原有内容at:打开一个txt文件,并从文件指针位置追加写内 ...

  10. LISP语言学习资源

    LISP的介绍:Paul Graham 的主页 http://paulgraham.com/index.html Lisp之根源 - 保罗·格雷厄姆 http://daiyuwen.freeshell ...