Codeforces Round #261 (Div. 2)
E. Pashmak and Graph
time limit per test
1 second memory limit per test
256 megabytes input
standard input output
standard output Pashmak's homework is a problem about graphs. Although he always tries to do his homework completely, he can't solve this problem. As you know, he's really weak at graph theory; so try to help him in solving the problem. You are given a weighted directed graph with n vertices and m edges. You need to find a path (perhaps, non-simple) with maximum number of edges, such that the weights of the edges increase along the path. In other words, each edge of the path must have strictly greater weight than the previous edge in the path. Help Pashmak, print the number of edges in the required path. Input
The first line contains two integers n, m (2 ≤ n ≤ 3·105; 1 ≤ m ≤ min(n·(n - 1), 3·105)). Then, m lines follows. The i-th line contains three space separated integers: ui, vi, wi (1 ≤ ui, vi ≤ n; 1 ≤ wi ≤ 105) which indicates that there's a directed edge with weight wi from vertex ui to vertex vi. It's guaranteed that the graph doesn't contain self-loops and multiple edges. Output
Print a single integer — the answer to the problem. Sample test(s)
Input
3 3 Output
1 Input
3 3 Output
3 Input
6 7 Output
6 Note
In the first sample the maximum trail can be any of this trails: . In the second sample the maximum trail is . In the third sample the maximum trail is . |
大意:给出一个带权有向图,求经过的边权绝对上升的最长路径(可能是非简单路径,即可能经过一个点多次)所包含的边数。
题解:对边按权值排序后,从小到大搞。
设q[x]为已经搞过的边组成的以x点为终点的最长路径包含的边数。
设当前边e[i]为从u到v的边,由于我们是按权值排序好的,只要没有相同的权值,我们就可以q[v]=max(q[v], q[u]+1)。
但是是有相同的权值的,我们直接这样搞,相同权值的可能会连出一条路,是不符合要求的,像第一个样例会输出3,怒萎。
所以相同权值的要特殊搞。我希望相同权值的不是一个个更新,而是一起更新,所以我把相同权值的先不更新,而是压入vector中,统计完这个权值的所有边,再将其一起从vector中取出更新。发现,有些相同权值的边连到的是同一个点,我希望用更长的路来更新这个点,所以对vector排序,后更新更长的。
核心代码如下:
S.push_back( pig(e[].to , ) );
for(i=; i<en; i++) {
if(e[i].w != e[i-].w) {///遇到不同权值的边,则将前一权值的全部边更新
sort(S.begin(),S.end());///使路长的后更新(若有同一个点有多种更新,则会保留路长的)
int maxj=S.size();
for(int j=; j<maxj; j++)
q[S[j].v]=S[j].be;
S.clear();
}
if(q[e[i].to]>=q[e[i].from]+)continue;
S.push_back( pig(e[i].to , q[e[i].from]+) );
}
全代码:
//#pragma comment(linker, "/STACK:102400000,102400000")
#include<cstdio>
#include<cmath>
#include<iostream>
#include<cstring>
#include<algorithm>
#include<cmath>
#include<map>
#include<set>
#include<stack>
#include<queue>
using namespace std;
#define ll long long
#define usll unsigned ll
#define mz(array) memset(array, 0, sizeof(array))
#define minf(array) memset(array, 0x3f, sizeof(array))
#define REP(i,n) for(i=0;i<(n);i++)
#define FOR(i,x,n) for(i=(x);i<=(n);i++)
#define RD(x) scanf("%d",&x)
#define RD2(x,y) scanf("%d%d",&x,&y)
#define RD3(x,y,z) scanf("%d%d%d",&x,&y,&z)
#define WN(x) prllf("%d\n",x);
#define RE freopen("D.in","r",stdin)
#define WE freopen("1biao.out","w",stdout)
#define mp make_pair struct Edge {
int w,from,to,next;
}; Edge e[];
int en;
int q[];
int n,m; struct pig {
int v,be;
pig() {}
pig(int x,int y) {
v=x;
be=y;
}
}; bool operator <(pig x,pig y) {
return x.be<y.be;
} vector<pig>S; void add(int x,int y,int z) {
e[en].w=z;
e[en].to=y;
e[en].from=x;
en++;
} bool cmp(Edge x,Edge y) {
return x.w<y.w;
} int main() {
int i,x,y,z; scanf("%d%d",&n,&m);
en=;
REP(i,m) {
scanf("%d%d%d",&x,&y,&z);
add(x,y,z);
} sort(e,e+en,cmp);///边从小到大遍历
int ans=;
memset(q,,sizeof(q)); S.push_back( pig(e[].to , ) );
for(i=; i<en; i++) {
if(e[i].w != e[i-].w) {///遇到不同权值的边,则将前一权值的全部边更新
sort(S.begin(),S.end());///使路长的后更新(若有同一个点有多种更新,则会保留路长的)
int maxj=S.size();
for(int j=; j<maxj; j++)
q[S[j].v]=S[j].be;
S.clear();
}
if(q[e[i].to]>=q[e[i].from]+)continue;
S.push_back( pig(e[i].to , q[e[i].from]+) );
}
sort(S.begin(),S.end());
int maxj=S.size();
for(int j=; j<maxj; j++)
q[S[j].v]=S[j].be; for(i=; i<=n; i++)
ans=max(ans,q[i]);
printf("%d\n",ans);
return ;
}