Codeforces Round #261 (Div. 2) E. Pashmak and Graph DP
http://codeforces.com/contest/459/problem/E
不明确的是我的代码为啥AC不了,我的是记录we[i]以i为结尾的点的最大权值得边,然后wa在第35 36组数据
然后參考答案了,然后----网上一份题解
大意: 给出一个带权有向图,求经过的边权绝对上升的最长路径(可能是非简单路径,就可以能经过一个点多次)所包括的边数。
题解: 对边按权值排序后,从小到大搞。
设q[x]为已经搞过的边组成的以x点为终点的最长路径包括的边数。
设当前边e[i]为从u到v的边,因为我们是按权值排序好的,仅仅要没有同样的权值,我们就能够q[v]=max(q[v], q[u]+1)。
可是是有同样的权值的,我们直接这样搞,同样权值的可能会连出一条路,是不符合要求的,像第一个例子会输出3,怒萎。
所以同样权值的要特殊搞。我希望同样权值的不是一个个更新,而是一起更新,所以我把同样权值的先不更新,而是压入vector中,统计完这个权值的全部边,再将其一起从vector中取出更新。发现,有些同样权值的边连到的是同一个点,我希望用更长的路来更新这个点,所以对vector排序,后更新更长的。
AC代码:
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <string>
#include <iostream>
#include <iomanip>
#include <cmath>
#include <map>
#include <set>
#include <queue>
using namespace std; #define ls(rt) rt*2
#define rs(rt) rt*2+1
#define ll long long
#define ull unsigned long long
#define rep(i,s,e) for(int i=s;i<e;i++)
#define repe(i,s,e) for(int i=s;i<=e;i++)
#define CL(a,b) memset(a,b,sizeof(a))
#define IN(s) freopen(s,"r",stdin)
#define OUT(s) freopen(s,"w",stdout)
const ll ll_INF = ((ull)(-1))>>1;
const double EPS = 1e-8;
const int INF = 100000000;
const int MAXN = 4*(1e6)+100;
struct Edge
{
int from,to,w;
}e[MAXN];
int n,m;
int dp[MAXN],we[MAXN]; inline void add(int u,int v,int w,int k)
{
e[k].from=u;
e[k].to=v;
e[k].w=w;
} void init()
{
CL(dp,0);
CL(we,0);
//same.clear();
} bool cmp(const Edge a, const Edge b)
{
return a.w<b.w;
} int main()
{
//IN("E.txt");
int u,v,w;
while(~scanf("%d%d",&n,&m))
{
init();
for(int i=0;i<m;i++)
{
scanf("%d%d%d",&u,&v,&w);
add(u,v,w,i);
}
sort(e,e+m,cmp);
int mmax=0;
for(int i=0,pos=0;i<m;i++)
{
dp[i]=we[e[i].from]+1;
if(e[i].w!=e[i+1].w)
{
for(int j=pos;j<=i;j++)
{
we[e[j].to]=max(we[e[j].to],dp[j]);
}
pos=i+1;
}
mmax=max(mmax,dp[i]);
} printf("%d\n",mmax);
}
return 0;
}
WA而不知道原因的代码,就是记录最大权值的结尾
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <string>
#include <iostream>
#include <iomanip>
#include <cmath>
#include <map>
#include <set>
#include <queue>
using namespace std; #define ls(rt) rt*2
#define rs(rt) rt*2+1
#define ll long long
#define ull unsigned long long
#define rep(i,s,e) for(int i=s;i<e;i++)
#define repe(i,s,e) for(int i=s;i<=e;i++)
#define CL(a,b) memset(a,b,sizeof(a))
#define IN(s) freopen(s,"r",stdin)
#define OUT(s) freopen(s,"w",stdout)
const ll ll_INF = ((ull)(-1))>>1;
const double EPS = 1e-8;
const int INF = 100000000;
const int MAXN = 4*(1e6)+100;
struct Edge
{
int from,to,w;
}e[MAXN];
int n,m;
int dp[MAXN],we[MAXN]; inline void add(int u,int v,int w,int k)
{
e[k].from=u;
e[k].to=v;
e[k].w=w;
} void init()
{
CL(dp,0);
CL(we,0);
} bool cmp(const Edge a, const Edge b)
{
if(a.w!=b.w)
return a.w<b.w;
else
{
if(a.from!=b.from)
return a.from<b.from;
else
return a.to<b.to;
}
} int main()
{
//IN("E.txt");
int u,v,w;
while(~scanf("%d%d",&n,&m))
{
init();
for(int i=0;i<m;i++)
{
scanf("%d%d%d",&u,&v,&w);
add(u,v,w,i);
}
sort(e,e+m,cmp);
int mmax=0;
for(int i=0;i<m;i++)
{
u=e[i].from;
v=e[i].to;
if(e[i].w > we[u])
{
if(dp[u]+1>dp[v])
{
we[v]=e[i].w;
dp[v]=dp[u]+1;
}
if(dp[u]+1 == dp[v])
we[v]=min(we[v],e[i].w);
}
//if(e[i].w == we[u])
mmax=max(mmax,dp[v]);
mmax=max(mmax,dp[u]);
}
printf("%d\n",mmax);
}
return 0;
}
Codeforces Round #261 (Div. 2) E. Pashmak and Graph DP的更多相关文章
- Codeforces Round 261 Div.2 E Pashmak and Graph --DAG上的DP
题意:n个点,m条边,每条边有一个权值,找一条边数最多的边权严格递增的路径,输出路径长度. 解法:先将边权从小到大排序,然后从大到小遍历,dp[u]表示从u出发能够构成的严格递增路径的最大长度. dp ...
- 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 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) 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的种类数. 我们可以用map预处理出 ...
- Codeforces Round #261 (Div. 2)[ABCDE]
Codeforces Round #261 (Div. 2)[ABCDE] ACM 题目地址:Codeforces Round #261 (Div. 2) A - Pashmak and Garden ...
- 构造图 Codeforces Round #236 (Div. 2) C. Searching for Graph
题目地址 /* 题意:要你构造一个有2n+p条边的图,使得,每一个含k个结点子图中,最多有2*k+p条边 水得可以啊,每个点向另外的点连通,只要不和自己连,不重边就可以,正好2*n+p就结束:) */ ...
- Codeforces Round #367 (Div. 2) C. Hard problem(DP)
Hard problem 题目链接: http://codeforces.com/contest/706/problem/C Description Vasiliy is fond of solvin ...
随机推荐
- VS调试技巧之附加进程
用过VS一段时间的程序猿们相信都有过这种调试经历:每次按下F5进行断点调试时,都要等待好长时间:先让解决方式编译通过,然后启动VS自带的简版IIS作为server启动,进而开启浏览器,最后进行对应的操 ...
- B桥接模式ridge
1.一个简短的引论 1)模式概述:将抽象部分与实现部分分离.使它们都能够独立的变化.让抽象类和派生类各自实现自己的对象.当一个系统有多维度的变化时,将各个维度分离出来让它们独立于变化(多角度地分类实现 ...
- [置顶] quartznet任务调度和消息调度(JAVA与C#版对比)
quartznet任务调度和消息调度 1. 作用 自动执行任务. 2. 下载地址 NET版本 JAVA版本 1下载 http://quartznet.sourceforge.net/downloa ...
- const使用摘要
const在四种方案如以下: int b = 500; const int *a = &b; ①(底层const) int const *a = &b; ②(底层const) int ...
- Ubuntu14.04 用 CrossOver 安装 TMQQ2013
Crossover 是 wine 的优化+商业版本号 , 免去了wine的繁琐配置,让Ubuntu安装windows软件很easy..... 部分移植的软件还有官方的维护,执行效果也比較好..... ...
- 数学思想方法-分布式计算-linux/unix技术基础(5)
shell命令行参数 -bash-4.2$ cat test1.sh#!/bin/shecho "$0 "echo "$1 "echo "$2 ...
- IOS加强知识(1)理解力Objective-C
一直想写一般Objective-C帖子,总是没时间.所以,我希望有一个巨大的知识更小.温馨提示小的变化.写一点点,每天.东西把他们的学习分享,好了废话不多. 1.一门动态的语言OC Object-C( ...
- 远程方法调用(RMI)原理与示例 (转)
RMI介绍 远程方法调用(RMI)顾名思义是一台机器上的程序调用另一台机器上的方法.这样可以大致知道RMI是用来干什么的,但是这种理解还不太确切.RMI是Java支撑分布式系统的基石,例如著名的EJB ...
- 【原创】poj ----- 1611 The Suspects 解题报告
题目地址: http://poj.org/problem?id=1611 题目内容: The Suspects Time Limit: 1000MS Memory Limit: 20000K To ...
- 2015华为德州扑克入境摘要——软体project
直到6一个月2号下午12时00,华为长达一个月的德州扑克锦标赛落下帷幕也被认为是. 我们的团队一直共同拥有3民,间.一个同学(吴)负责算法设计,一个同学(宋)负责分析消息,而我负责的实现框架设计和详细 ...