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的更多相关文章

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

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

  2. 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 ...

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

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

  4. 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 ...

  5. 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). 解法:分别从左到右,由右到 ...

  6. 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预处理出  ...

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

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

  8. 构造图 Codeforces Round #236 (Div. 2) C. Searching for Graph

    题目地址 /* 题意:要你构造一个有2n+p条边的图,使得,每一个含k个结点子图中,最多有2*k+p条边 水得可以啊,每个点向另外的点连通,只要不和自己连,不重边就可以,正好2*n+p就结束:) */ ...

  9. Codeforces Round #367 (Div. 2) C. Hard problem(DP)

    Hard problem 题目链接: http://codeforces.com/contest/706/problem/C Description Vasiliy is fond of solvin ...

随机推荐

  1. Oracle基本流程语句

    ====1.IF语句==========

  2. mysql 触发器和存储过程组合使用,实现定时触发操作

    mysql可以实现定时触发功能,比如说定于某某时间mysql数据库做什么工作,或每隔多长时间做什么工作. 第二种情况应用还是比较广的,比如说我希望每天检查一下我的数据信息,超过一个月的无用信息清除以腾 ...

  3. 23设计模式(3):Abstract Factory模式

    定义:要创建一组相关或依赖对象提供一个接口,而你并不需要指定其具体类别. 类型:创建一个类模型 类图: 抽象工厂模式与工厂方法模式的差别 抽象工厂模式是工厂方法模式的升级版本号,他用来创建一组相关或者 ...

  4. Unity UGUI——开源

    开源许可证:MIT/X11 来源托管网站:BitBucket

  5. 使用CSS如何悬停背景颜色变色 onmouseover、onmouseout

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...

  6. 解决 下载 CM-12.0 源代码出现 Fatal: duplicate project .....问题

    在使用 repo init -u git://github.com/CyanogenMod/android.git -b cm-12.0 初始化代码库的时候出现如下错误: fatal: manifes ...

  7. Jenkins(转)

    1  修改jenkins的根目录,默认地在C:\Documents and Settings\AAA\.jenkins . .jenkins ├─jobs│  └─JavaHelloWorld│    ...

  8. HDU 4288 Coder (线段树)

    Coder 题目:http://acm.hdu.edu.cn/showproblem.php?pid=4288 题意:有三种类型的操作,(1)."add x",表示往集合里加入�数 ...

  9. CSAPP 六个重要的实验 lab5

    CSAPP  && lab5 实验指导书: http://download.csdn.net/detail/u011368821/7951657 实验材料: http://downlo ...

  10. effective c++ 条款26 postpone variable definition as long as possible

    因为构造和析构函数有开销,所以也许前面定义了,还没用函数就退出了. 所以比较好的方法是用到了才定义.