首页 > 其他分享 >【题解】ABC259F Select Edges

【题解】ABC259F Select Edges

时间:2022-10-25 07:44:16浏览次数:54  
标签:10 ch int 题解 p1 Edges Select buf dp

Sol

考虑 dp。
记 \(dp_{u,0/1}\) 表示 \(u\) 点是否向上连边的最大值。
转移的话相当于是找若干个 \(dp_{v,1}+w(u,v)\) 进行转移。
其中 \(w(u,v)\) 表示 \((u,v)\) 这条边的权值。
那么每次开个 vector 把 \(dp_{v,1}+w(u,v)\) 存下来然后排个序,找最大的 \(d_u\) 个节点即可。
时间复杂度 \(O(n \log n)\)。

Code

//LYC_music yyds!
#include<bits/stdc++.h>
#define IOS ios::sync_with_stdio(0)
#define lowbit(x) (x&(-x))
#define int long long
using namespace std;
inline char gc()
{
	static char buf[1000000],*p1=buf,*p2=buf;
	return p1==p2&&(p2=(p1=buf)+fread(buf,1,1000000,stdin),p1==p2)?EOF:*p1++;
}
int read()
{
	int pos=1,num=0;
	char ch=getchar();
	while (!isdigit(ch))
	{
		if (ch=='-') pos=-1;
		ch=getchar();
	}
	while (isdigit(ch))
	{
		num=num*10+(int)(ch-'0');
		ch=getchar();
	}
	return pos*num;
}
void write(int x)
{
	if (x<0)
	{
		putchar('-');
		write(-x);
		return;
	}
	if (x>=10) write(x/10);
	putchar(x%10+'0');
}
void writesp(int x)
{
	write(x);
	putchar(' ');
}
void writeln(int x)
{
	write(x);
	putchar('\n');
}
const int N=3e5+10;
vector<pair<int,int> > G[N];
int n,a[N],dp[N][2];
void dfs(int u,int fa)
{
	int s=0; vector<int> g;
	for (auto [v,w]:G[u])
	{
		if (v==fa) continue;
		dfs(v,u); s+=dp[v][0];
		g.emplace_back(dp[v][1]-dp[v][0]+w);
	}
	sort(g.begin(),g.end(),greater<int>());
	for (int i=0;i<min((int)g.size(),a[u]-1);i++)
		s+=max(0ll,g[i]);
	dp[u][0]=s+(g.size()>a[u]-1?max(0ll,g[a[u]-1]):0);
	if (!a[u]) dp[u][1]=-0x3f3f3f3f3f3f3f3f;
	else dp[u][1]=s;
}
signed main()
{
	n=read();
	for (int i=1;i<=n;i++)
		a[i]=read();
	for (int i=1;i<n;i++)
	{
		int u=read(),v=read(),w=read();
		G[u].emplace_back(v,w); G[v].emplace_back(u,w);
	}
	dfs(1,0);
	writeln(max(dp[1][0],dp[1][1]));
}





标签:10,ch,int,题解,p1,Edges,Select,buf,dp
From: https://www.cnblogs.com/dd-d/p/16823696.html

相关文章

  • 2022级HAUT新生周赛题解汇总
    2022级HAUT新生周赛(零)题解@:2022级HAUT新生周赛(一)题解@卞子骏:2022级HAUT新生周赛(二)题解@武其轩:2022级HAUT新生周赛(三)题解@焦小航:2022级HAUT新生周赛(四)题解@张子豪:202......
  • leetcode刷题MySQL题解十八
    leetcode刷题MySQL题解十八题目叙述Views表:±--------------±--------+|ColumnName|Type|±--------------±--------+|article_id|int||author_id|int|......
  • leetcode刷题MySQL题解十五
    leetcode刷题MySQL题解十五题目叙述Employee表:±------------±-----+|ColumnName|Type|±------------±-----+|id|int||salary|int|±------------±......
  • leetcode刷题MySQL题解十三
    leetcode刷题MySQL题解十三题目叙述表:Products±------------±--------+|ColumnName|Type|±------------±--------+|product_id|int||store1|int||st......
  • leetcode刷题MySQL题解十四
    leetcode刷题MySQL题解十四题目叙述给定一个表tree,id是树节点的编号,p_id是它父节点的id。±—±-----+|id|p_id|±—±-----+|1|null||2|1||3|1......
  • leetcode刷题MySQL题解十二
    leetcode刷题MySQL题解十二题目叙述表:Employees±------------±--------+|ColumnName|Type|±------------±--------+|employee_id|int||name|varchar......
  • leetcode刷题MySQL题解十一
    leetcode刷题MySQL题解十一题目叙述表:Weather±--------------±--------+|ColumnName|Type|±--------------±--------+|id|int||recordDate|date||t......
  • leetcode刷题MySQL题解九
    leetcode刷题MySQL题解九题目叙述表Activities:±------------±--------+|列名|类型|±------------±--------+|sell_date|date||product|varchar|±---......
  • CF1753B 题解
    前言题目传送门!更好的阅读体验?其实挺简单的,赛时多打了个等号,被人叉了。思路关键是\(n!\times(n+1)=(n+1)!\)。原因很显然:\((1\times2\times\cdots\tim......
  • [abc262E] red and blue gragh 题解
    挺喜欢这道题的,但是因为vp时过了不能写成做题笔记,所以只能写成题解了。绿太配这道题了,实现难度极低,但思维难度较大。AT评了#1719,倒也算恰当。题意:给定一张\(n\)点......