标签:namespace end bit include c++ ++ ace erase its
输入给出m条边,要求找到一条最长的路径满足边按照输入的顺序出现并且权值严格递增
两种方法:第一种利用单调队列性质
第二种利用数据结构优化
#include<bits/stdc++.h> #define forn(i, n) for (int i = 0 ; i < int(n) ; i++) #define fore(i, s, t) for (int i = s ; i < (int)t ; i++) #define fi first #define se second #define all(x) x.begin(),x.end() #define pf2(x,y) printf("%d %d\n",x,y) #define pf(x) printf("%d\n",x) #define each(x) for(auto it:x) cout<<it<<endl; #define pi pair<int,int> using namespace std; typedef long long ll; const int maxn=1e6+5; const int maxm=2e5+5; const int inf=1e9; vector<map<int,int>> s; int calc(int a,int w){ auto it=s[a].lower_bound(w); if(it==s[a].begin()) return 1; it--; return (it->second)+1; } int main(){ int n,m; cin>>n>>m; s.resize(n+1); int ans=0; for(int i=0;i<m;i++){ int x,y,z; cin>>x>>y>>z; int val=calc(x,z); if(calc(y,z+1)>val) continue; s[y][z]=max(s[y][z],val); auto it=s[y].upper_bound(z); while(!(it==s[y].end() || it->se>val)) it=s[y].erase(it); ans=max(ans,val); } cout<<ans<<endl; }
标签:namespace end bit include c++ ++ ace erase its
原文地址:https://www.cnblogs.com/033000-/p/12345491.html