Codeforces Round #261 (Div. 2) - E (459E)
题目连接: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)的更多相关文章
- Codeforces Round #261 (Div. 2)[ABCDE]
Codeforces Round #261 (Div. 2)[ABCDE] ACM 题目地址:Codeforces Round #261 (Div. 2) A - Pashmak and Garden ...
- Codeforces Round #261 (Div. 2) B
链接:http://codeforces.com/contest/459/problem/B B. Pashmak and Flowers time limit per test 1 second m ...
- 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 Round #261 (Div. 2)459D. Pashmak and Parmida's problem(求逆序数对)
题目链接:http://codeforces.com/contest/459/problem/D D. Pashmak and Parmida's problem time limit per tes ...
- Codeforces Round #261 (Div. 2) B. Pashmak and Flowers 水题
题目链接:http://codeforces.com/problemset/problem/459/B 题意: 给出n支花,每支花都有一个漂亮值.挑选最大和最小漂亮值得两支花,问他们的差值为多少,并且 ...
- 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 ...
- Codeforces Round 261 Div.2 E Pashmak and Graph --DAG上的DP
题意:n个点,m条边,每条边有一个权值,找一条边数最多的边权严格递增的路径,输出路径长度. 解法:先将边权从小到大排序,然后从大到小遍历,dp[u]表示从u出发能够构成的严格递增路径的最大长度. dp ...
- 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). 解法:分别从左到右,由右到 ...
- Codeforces Round #261 (Div. 2)
第一场难得DIV2简单+AK人数多: E:给出一张图,求最多的边数,满足:在这个边的集合中后面的边的权值大于前面的边; 思路:我们将图按权值排列,以为只可能边权值小的跟新权值大的所以对于一条边我们只跟 ...
随机推荐
- 读经典——《CLR via C#》(Jeffrey Richter著) 笔记_new新建对象
CLR使用 new 操作符来创建新对象,例如:Employee e=new Employee("Param1"); 以下是 new 操作符所做的事情. 它计算类型及其所有基类型 ...
- CF628D Magic Numbers (数据大+数位dp)求[a,b]中,偶数位的数字都是d,其余为数字都不是d,且能被m整除的数的个数
题意:求[a,b]中,偶数位的数字都是d,其余为数字都不是d,且能被m整除的数的个数(这里的偶数位是的是从高位往低位数的偶数位).a,b<10^2000,m≤2000,0≤d≤9 a,b< ...
- div css 伪类 不固定图片大小 居中, css div 实现三角形
div css 伪类 不固定图片大小 居中 <style> .pic_box{width:300px; height:300px; background-color:#beceeb; fo ...
- 17-----BBS论坛
BBS论坛(十七) 17.首页导航条实现和代码抽离 (1)temlates/common/_head.html <meta name="csrf-token" content ...
- 2 Sum
Problem Given an array of integers, find two numbers such that they add up to aspecific target numbe ...
- java——时间复杂度、动态数组
O(n)不一定小于O(n^2),要具体来看,而我们说的这种时间复杂度其实是渐进时间复杂度,描述的是n趋近于无穷的情况. 动态数组的时间复杂度: 添加操作:O(n) addLast()的均摊复杂度为O( ...
- 美团Linux运维工程师面试真题
1.LINUX系统软件安装和卸载的常见方法 答: A.rpm包卸载:rpm -e XXX.rpm (如果想忽略依赖,可加上–nodeps) B.yum remove xxx.rpm 这种方法非常 ...
- SpringBoot初始教程之Servlet、Filter、Listener配置详解
1.介绍 通过之前的文章来看,SpringBoot涵盖了很多配置,但是往往一些配置是采用原生的Servlet进行的,但是在SpringBoot中不需要配置web.xml的 因为有可能打包之后是一个ja ...
- Trace Logging Level
Trace In functions which support this level, details every parameter and operation to help diagnose ...
- 二叉查找树的C语言实现(二)
接着上次的话题.这次我们要讨论,二叉查找树的中序遍历和后序遍历(递归和非递归),另外还有先序遍历(非递归) 1.中序遍历(递归) static void __in_order(struct bnode ...