题目连接: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. Quartz中Cron详解

    Quartz中的cron跟Linux系统的cron定义不太一样(Linux从分开始) 特殊字符: * 用来表示包含一个范围内的任意值. 例如, 分钟位置的“*” 表示 “每分钟”. ?  当不特定指代 ...

  2. apache 与php的安装

    1 系统环境与软件   1 php5.5.6 下载链接:http://windows.php.net/download/#php-5.5 推荐 V11 x64,也就是64bit的. 2 apache2 ...

  3. 移动测试之appium+python 简单例子(五)

    # coding=utf-8 from appium import webdriver import time import unittest import os import HTMLTestRun ...

  4. 2019.03.21 读书笔记 枚举ENUM

    其实没必要为枚举显式赋值,如果赋值了,就一定要全部赋值,否则默认在上一个元素的基础上+1,如果不给枚举变量赋值,就算枚举中没有0元素,也会显示为0,而超出枚举范围的整型数据,也会显示值本身,而不是异常 ...

  5. Json JsonUtility对字典/列表的序列化,反序列化

    Unity5.3从开始追加的JsonUtility,但是对于List 和Dictionary不能被直接序列化存储. 例如: 数据模型: using UnityEngine; using System; ...

  6. 最新版本dede与discuz通过ucenter完美整合

    人合租虚拟主机.然后到相关的官方网站上面下载相关的程序,我下载的是DedeCmsV5.7-GBK+Discuz_X2_RC_SC_GBK+UCenter_1.6.0_SC_GBK这个程序组合.涉及到怎 ...

  7. tck/tl 以及expect脚本

    最近有用到,利用expcet脚本自动登录到远程服务器并提权执行脚本. 搜集的知识如下: tcl/tk参考——列表操作lindex expect脚本解释 代码如下 #!/usr/bin/expect - ...

  8. 介绍几个关于C/C++程序调试的函数

    最近调试程序学到的几个挺有用的函数,分享一下,希望对用C/C++的朋友有所帮助! 1. 调用栈系列下面是函数原型: 1 2 3 4 #include "execinfo .h" i ...

  9. msfconsole 控制台使用和操作

    msfconsole 参数 Msfconsole提供了一个一体化的集中控制台.通过msfconsole,你可以访问和使用所有的metasploit的插件,payload,利用模块,post模块等等.M ...

  10. JavaScript 原型链 OOP(二)

    原型对象 `prototype` -  原型对象的所有属性和方法,都能被实例对象共享;   JavaScript 通过构造函数生成新对象,因此构造函数可以视为对象的模板.实例对象的属性和方法,可以定义 ...