459E - Pashmak and Graph

对边权进行排序。令dp[i]表示以i为结束点的最大上升长度。那么,我们每一次加入一条边,dp[to]=dp[from]+1。其中需要注意,边权相等的时候,需要特殊考虑。

#include<iostream>
#include<cstring>
#include<algorithm>
#include<cmath>
#include<cstdlib>
#include<climits>
#include<stack>
#include<vector>
#include<queue>
#include<set>
#include<bitset>
#include<map>
//#include<regex>
#include<cstdio>
#pragma GCC optimize(2)
#define up(i,a,b)  for(int i=a;i<b;i++)
#define dw(i,a,b)  for(int i=a;i>b;i--)
#define upd(i,a,b) for(int i=a;i<=b;i++)
#define dwd(i,a,b) for(int i=a;i>=b;i--)
//#define local
typedef long long ll;
typedef unsigned long long ull;
const double esp = 1e-6;
const double pi = acos(-1.0);
const int INF = 0x3f3f3f3f;
const int inf = 1e9;
using namespace std;
ll read()
{
	char ch = getchar(); ll x = 0, f = 1;
	while (ch<'0' || ch>'9') { if (ch == '-')f = -1; ch = getchar(); }
	while (ch >= '0' && ch <= '9') { x = x * 10 + ch - '0'; ch = getchar(); }
	return x * f;
}
typedef pair<int, int> pir;
#define lson l,mid,root<<1
#define rson mid+1,r,root<<1|1
#define lrt root<<1
#define rrt root<<1|1
const int N = 3e5 + 10;
struct eg {
	int u, v, wi;
	bool operator<(const eg temp)const {
		return wi < temp.wi;
	}
}edge[N];
int dp[N][2];
int n, m;
stack<eg>st;
int main()
{
	n = read(), m = read();
	int u, v, wi;
	upd(i, 1, m)
	{
		u = read(), v = read(), wi = read();
		edge[i].u = u; edge[i].v = v; edge[i].wi = wi;
	}
	sort(edge + 1, edge + 1 + m);
	for (int i = 1; i <= m;)
	{
		st.push(edge[i]);
		u = edge[i].u, v = edge[i].v, wi = edge[i].wi;
		dp[v][1] = dp[u][0] + 1;
		int j;
		for ( j= i+1;j<=m &&edge[j-1].wi==edge[j].wi;j++)
		{
			st.push(edge[j]);
			u = edge[j].u, v = edge[j].v, wi = edge[j].wi;
			dp[v][1] = max(dp[v][1], dp[u][0] + 1);
		}
		i = j;
		while (!st.empty())
		{
			eg top = st.top();
			v = top.v;
			dp[v][0] = max(dp[v][1], dp[v][0]);
			dp[v][1] = 0;
			st.pop();
		}
	}
	int ans = 0;
	upd(i, 1, n)
	{
		ans = max(ans, dp[i][0]);
	}
	cout << ans << endl;
	return 0;
}
发布了208 篇原创文章 · 获赞 1 · 访问量 1万+

猜你喜欢

转载自blog.csdn.net/weixin_44019404/article/details/104964506