bzoj4919 大根堆
生活随笔
收集整理的這篇文章主要介紹了
bzoj4919 大根堆
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
考慮二分求序列LIS的過程。
g[i]表示長度為i的LIS最小以多少結尾。
對于每個數,二分尋找插入的位置來更新g數組。
放到樹上也是一樣,額外加上一個合并兒子的過程。
發現兒子與兒子直接是互不影響的,可以直接合并。
用啟發式合并set來維護這個g數組,復雜度O(nlogn^2)。
#include<iostream> #include<cctype> #include<cstdio> #include<cstring> #include<string> #include<set> #include<cmath> #include<ctime> #include<cstdlib> #include<algorithm> #define N 2200000 #define L 2000000 #define eps 1e-7 #define inf 1e9+7 #define ll long long using namespace std; inline int read() {char ch=0;int x=0,flag=1;while(!isdigit(ch)){ch=getchar();if(ch=='-')flag=-1;}while(isdigit(ch)){x=(x<<3)+(x<<1)+ch-'0';ch=getchar();}return x*flag; } struct edge {int to,nxt; }e[N*2]; int num=-1,head[N]; inline void add(int x,int y) {e[++num]=(edge){y,head[x]};head[x]=num;e[++num]=(edge){x,head[y]};head[y]=num; } int w[N]; multiset<int>s[N]; multiset<int>::iterator it; void merge(int x,int y)//add y to x {if(s[x].size()<s[y].size())swap(s[x],s[y]);while(!s[y].empty()){it=s[y].begin();s[x].insert(*it);s[y].erase(it);} } void dfs(int x,int fa) {for(int i=head[x];i!=-1;i=e[i].nxt){int to=e[i].to;if(to==fa)continue;dfs(to,x);merge(x,to);}it=s[x].lower_bound(w[x]);if(it!=s[x].end())s[x].erase(it);s[x].insert(w[x]); } int main() {memset(head,-1,sizeof(head));int n=read(),x;for(int i=1;i<=n;i++){w[i]=read();x=read();if(i!=1)add(i,x);}dfs(1,1);printf("%d",(int)s[1].size());return 0; }轉載于:https://www.cnblogs.com/Creed-qwq/p/10078893.html
總結
以上是生活随笔為你收集整理的bzoj4919 大根堆的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 中国银行赞卡信用卡好申请吗?放水是真的吗
- 下一篇: 牛客33-tokitsukaze and