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 ...
随机推荐
- CodeIgniter Apacheserver htaccess SEO重写写法
1)支持重写 2)兼容全部APACHE server 3)假设不是文件,请求的结尾不包含反斜杠,自己主动跳转到反斜杠 4)文件名称 .htaccess 5)这个放到nginxserver怎么都不支持重 ...
- 重温delphi之控制台程序:Hello World!
原文:重温delphi之控制台程序:Hello World! 这二天用c#开发ActiveX时,发现不管怎么弄,c#就是没办法生成ocx的纯正activeX控件,而且还要强迫用户安装巨大的.net f ...
- GCC的使用(编译,链接,运行)
以下这三篇日志非常的好,真的非常的好.介绍使用gcc编译,链接,运行c程序. 1.http://lveyo.iteye.com/blog/240578 2.http://lveyo.iteye.com ...
- 使用Visual Studio 创建可视Web Part部件
使用Visual Studio 创建可视Web Part部件 可视Web Part部件是很强大的Web 部件.它提供内置设计器创建你的用户界面. 本文主要解说怎样使用Visual Studio 创建可 ...
- 浅谈TCP优化(转)
很多人常常对TCP优化有一种雾里看花的感觉,实际上只要理解了TCP的运行方式就能掀开它的神秘面纱.Ilya Grigorik 在「High Performance Browser Networking ...
- Amazon AWS创建RHEL 7实例
在AWS上登录 如果没有账号的话先注册,参考 http://blog.banban.me/blog/2014/06/09/li-yong-awsmian-fei-zhang-hu-da-jian-vp ...
- graph driver-device mapper-01driver初始化
// thin device数据结构 type DevInfo struct { Hash string `json:"-"` DeviceId int `json:"d ...
- hibernate学习——Set集合配置
Set集合的配置 数据表的创建:表关系一个员工拥有多个身份 create table EMPLOYEE ( id INT NOT NULL auto_increment, first_name VAR ...
- C语言 cgi(3)
1cs3157 – Advanced ProgrammingSummer 2014, Project 1, 150 pointsJune 17, 2014Follow these step-by-st ...
- JavaEEB2C网上商城前端系统
问题的提出: 电子商务已经成为人们生活中不可或缺的组成部分,它提供了一种足不出户就可以挑选.购买.使用商品的方式.在众多点上网站中,综合类的B2C电商以其较高的可信度,丰富的商品类目,得到消费者的青睐 ...