CF1119F Niyaz and Small Degrees【treedp+堆】
如果枚举d来dp,那么就是设f[u][0/1]为u点不断/断掉和父亲的边,然后优先选取f[v][1]+w(u,v)<=f[v][0]的,如果断掉这些度数还是多就用一个堆维护剩下的按f[v][1]+w(u,v)-f[v][0]从大到小取(负的)
然后注意到设当前要求度数是d,那么度数<=d的点全都不用dp,所以可以直接把贡献累加到和他相邻的点上然后断掉边,这样一共算Σ度数ci,就是O(nlogn)的了
#include<iostream>
#include<cstdio>
#include<queue>
#include<vector>
#include<algorithm>
using namespace std;
const int N=300005;
int n,du,v[N],d[N],s[N],ne[N];
long long ans,sm[N],f[N][2];
vector<pair<int,int> >e[N];
vector<int>a[N];
int read()
{
int r=0,f=1;
char p=getchar();
while(p>'9'||p<'0')
{
if(p=='-')
f=-1;
p=getchar();
}
while(p>='0'&&p<='9')
{
r=r*10+p-48;
p=getchar();
}
return r*f;
}
struct dui
{
priority_queue<long long>p,q;
void push(long long x)
{
q.push(x);
}
void erase(long long x)
{
p.push(x);
}
int top()
{
while(!q.empty()&&!p.empty()&&q.top()==p.top())
q.pop(),p.pop();
return q.top();
}
void pop()
{
top();
q.pop();
}
int size()
{
return q.size()-p.size();
}
bool empty()
{
return q.size()==p.size();
}
}q[N];
bool cmp(const pair<int,int>&a,const pair<int,int>&b)
{
return d[a.first]<d[b.first];
}
void add(int u,int v,int w)
{
e[u].push_back(make_pair(v,w));
e[v].push_back(make_pair(u,w));
}
void dfs(int u,int fa)
{
v[u]=1;
int r=d[u]-du;
long long sum=0;
vector<long long>ad,de;
while(q[u].size()>r)
sm[u]-=q[u].top(),q[u].pop();
while(s[u]<e[u].size()&&d[e[u][s[u]].first]<=du)
s[u]++;
for(int i=s[u],len=e[u].size();i<len;i++)
if(!v[e[u][i].first]&&e[u][i].first!=fa)
{
dfs(e[u][i].first,u);
if(f[e[u][i].first][1]+e[u][i].second<=f[e[u][i].first][0])
r--,sum+=f[e[u][i].first][1]+e[u][i].second;
else
{
sum+=f[e[u][i].first][0];
long long nw=f[e[u][i].first][1]+e[u][i].second-f[e[u][i].first][0];
q[u].push(nw);
sm[u]+=nw;
de.push_back(nw);
}
}
while(q[u].size()>max(0,r))
sm[u]-=q[u].top(),ad.push_back(q[u].top()),q[u].pop();
f[u][0]=sum+sm[u];
r--;
while(q[u].size()>max(0,r))
sm[u]-=q[u].top(),ad.push_back(q[u].top()),q[u].pop();
f[u][1]=sum+sm[u];
for(int i=0,len=ad.size();i<len;i++)
q[u].push(ad[i]),sm[u]+=ad[i];
for(int i=0,len=de.size();i<len;i++)
q[u].erase(de[i]),sm[u]-=de[i];
}
int main()
{
n=read();
for(int i=1;i<n;i++)
{
int x=read(),y=read(),z=read();
add(x,y,z);
d[x]++,d[y]++;
ans+=z;
}
for(int i=1;i<=n;i++)
{
a[d[i]].push_back(i);
sort(e[i].begin(),e[i].end(),cmp);
}
ne[n]=n+1;
for(int i=n-1;i>=1;i--)
{
if(a[i+1].size())
ne[i]=i+1;
else
ne[i]=ne[i+1];
}
printf("%lld ",ans);
for(int i=1;i<n;i++)
{
for(int j=0,len=a[i].size(),u;j<len;j++)
{
u=a[i][j];
v[u]=1;
for(int k=0,le=e[u].size();k<le;k++)
if(!v[e[u][k].first])
q[e[u][k].first].push(e[u][k].second),sm[e[u][k].first]+=e[u][k].second;
}
ans=0,du=i;
for(int j=i+1;j<=n;j=ne[j])
for(int k=0,len=a[j].size();k<len;k++)
if(!v[a[j][k]])
{
dfs(a[j][k],0);
ans+=f[a[j][k]][0];
}
for(int j=i+1;j<=n;j=ne[j])
for(int k=0,len=a[j].size();k<len;k++)
v[a[j][k]]=0;
printf("%lld ",ans);
}
return 0;
}
CF1119F Niyaz and Small Degrees【treedp+堆】的更多相关文章
- CF1119F Niyaz and Small Degrees
题意 给你\(n\)个点的树,边有边权 问使得所有的点度数都小于等于\(x\)的最小删边的代价 \([x \in 0...n-1]\) 题解 首先对于每个\(x\) 可以有一个\(O(nlogn)\) ...
- 树形DP ---- Codeforces Global Round 2 F. Niyaz and Small Degrees引发的一场血案
Aspirations:没有结果,没有成绩,acm是否有意义?它最大的意义就是让我培养快速理解和应用一个个未知知识点的能力. ————————————————————————————————————— ...
- [codeforces contest 1119 F] Niyaz and Small Degrees 解题报告 (树形DP+堆)
interlinkage: http://codeforces.com/contest/1119/problem/F description: 有一颗$n$个节点的树,每条边有一个边权 对于一个$x$ ...
- 【codeforces contest 1119 F】Niyaz and Small Degrees
题目 描述 \(n\) 个点的树,每条边有一个边权: 对于一个 \(X\) ,求删去一些边后使得每个点的度数 \(d_i\) 均不超过 \(X\) 的最小代价: 你需要依次输出 \(X=0 \to n ...
- CF 1119F Niyaz and Small Degrees
打VP的时候由于CXR和XRY切题太快了导致我只能去写后面的题了 然而VP的时候大概还有一小时时想出了\(O(n^2\log n)\)的暴力,然后过了二十分钟才想到删点的优化 结果细节很多当然是写不出 ...
- Solution -「CF 1119F」Niyaz and Small Degrees
\(\mathcal{Description}\) Link. 给定一棵 \(n\) 个结点的树,边有边权,对于每个整数 \(x\in[0,n)\),求出最少的删边代价使得任意结点度数不超过 ...
- Codeforces Global Round 2 Solution
这场题目设置有点问题啊,难度:Div.2 A->Div.2 B->Div.2 D->Div.2 C->Div.2 D->Div.1 D-> Div.1 E-> ...
- Codeforces Global Round 2 部分题解
F.Niyaz and Small Degrees 挺sb的一题,为什么比赛时只过了4个呢 考虑当\(x\)固定的时候怎么做.显然可以树形DP:设\(f_{u,i=0/1}\)表示只考虑\(u\)子树 ...
- P7600-[APIO2021]封闭道路【堆,dp】
正题 题目链接:https://www.luogu.com.cn/problem/P7600 题目大意 给出\(n\)个点的一棵树,边有边权,对于每个\(k\)求去掉最小边权和的点使得每个点的度数都不 ...
随机推荐
- Android 破解
一.反编译 默认你的电脑中完好的有java环境 1.下载 Android killer 链接: https://pan.baidu.com/s/1s6lfm8CbdU9ABYEOhdFWxA 提取码 ...
- HDU - 1495 非常可乐 【BFS】
题目链接 http://acm.hdu.edu.cn/showproblem.php?pid=1495 思路 首先 如果可乐的体积 是奇数 那么是无解的 然后 如果能够得到两杯 都是一般容量的可乐 那 ...
- rbx1包里机器人仿真程序的实践
git clone https://github.com/pirobot/rbx1.git 1.打开一个终端 cd ~/catkin_ws/ catkin_make source ./devel/s ...
- error:Flash Download failed-“Cortex-M3”,“Programming Algorithm”【转】
本文转载自:http://www.yfrobot.com/thread-11763-1-1.html 最近安装了KEIL5,在使用KEIL5和JLIN实现在线调试功能时,一定会在Utilities选项 ...
- Contiki Ctimer模块
Ctimer 提供和Etimer类似的功能,只是Ctimer是在一段时间后调用回调函数,没有和特定进程相关联. 而Etimer是在一段时间后发送PROCESS_EVENT_TIMER事件给特定的进程. ...
- 为什么在实际的 kaggle 比赛中 gbdt 和 random forest 效果非常好?
https://www.zhihu.com/question/51818176/answer/127637712
- python的easygui
1.利用msgbox(单词messagebox的缩写)给出一个提示信息: import easygui as g reply=g.msgbox('This is a basic message box ...
- BZOJ 1529 [POI2005]ska Piggy banks:并查集
题目链接:http://www.lydsy.com/JudgeOnline/problem.php?id=1529 题意: Byteazar有N个小猪存钱罐. 每个存钱罐只能用钥匙打开或者砸开. By ...
- 高效上网教程---资源软件搜索技巧(搜索好用软件或者app去哪些网站)
高效上网教程---资源软件搜索技巧(搜索好用软件或者app去哪些网站) 一.总结 一句话总结:查看下面这些网站用户推荐的 知乎:比如 小众软件 site:zhihu.com 简书:查看你需要的用户推荐 ...
- html5--1.5 文本元素
html5--1.5 文本元素 学习要点: 掌握常用的文本元素 文本元素,就是讲一段文本设置成相匹配的结构和含义 1.b元素: 我的作用就是 加粗文字: 2.br元素: 我的作用就是强制换行: 3.i ...