F. Gardening Friends
1.P3478 [POI2008] STA-Station2.E1. Weights Division (easy version)3.P5007 DDOSvoid 的疑惑4.P2168 [NOI2015] 荷马史诗5.P8306 【模板】字典树6.P1481 魔族密码7.P3128 [USACO15DEC] Max Flow P8.P3258 [JLOI2014] 松鼠的新家9.P5536 【XR-3】核心城市10.P5836 [USACO19DEC] Milk Visits S11.P3384 【模板】重链剖分/树链剖分12.P3038 [USACO11DEC] Grass Planting G13.P4551 最长异或路径14.F. Alex's whims15.J. 上学16.Game on Tree17.E. We Need More Bosses18.A. Party19.D. Strong Vertices20.B. Omkar and Heavenly Tree21.B. Mahmoud and Ehab and the bipartiteness22.P8655 [蓝桥杯 2017 国 B] 发现环23.P10298 [CCC 2024 S4] Painting Roads
24.F. Gardening Friends
25.D. Playoff Tournament26.E. Lomsat gelral27.F. Minimum Maximum Distance28.D. Book of Evil29.P2195 HXY造公园30.P3304 [SDOI2013] 直径31.D. Sasha and a Walk in the City32.D. Vitaly and Cycle33.P10838 『FLA - I』庭中有奇树34.F - Perfect Matching on a Tree题解
最大深度·k-与节点1的距离·c
其中最大深度只要知道了节点1的最大深度,其子节点的最大深度可分类讨论得出
code
#include<bits/stdc++.h>
#define ll long long
using namespace std;
vector<ll> G[200005];
ll height1[200005] = {0};
ll height2[200005] = {0};
ll rec[200005] = {0};
ll cnt[200005] = {0};
inline void read(ll &x) {
x = 0;
ll flag = 1;
char c = getchar();
while(c < '0' || c > '9'){
if(c == '-')flag = -1;
c = getchar();
}
while(c >= '0' && c <= '9') {
x = (x << 3) + (x << 1) + (c ^ 48);
c = getchar();
}
x *= flag;
}
inline void write(ll x)
{
if(x < 0){
putchar('-');
x = -x;
}
if(x > 9)
write(x / 10);
putchar(x % 10 + '0');
}
void dfs1(ll now, ll fa)
{
height1[now] = height2[now] = 0;
for(auto next : G[now])
{
if(next == fa) continue;
dfs1(next, now);
if(height1[next] + 1 > height1[now])
{
height1[now] = height1[next] + 1;
}
else if(height1[next] + 1 > height2[now])
{
height2[now] = height1[next] + 1;
}
}
}
void dfs2(ll now, ll fa, ll op)
{
rec[now] = height1[now];
cnt[now] = op;
for(auto next : G[now])
{
if(next == fa) continue;
ll tem2 = height1[next];
if(height1[next] + 1 == height1[now])
{
ll tem1 = height1[now];
height1[next] = max(height1[next], height2[now] + 1);
height1[now] = height2[now];
dfs2(next, now, op + 1);
height1[now] = tem1;
height1[next] = tem2;
}
else
{
height1[next] = height1[now] + 1;
dfs2(next, now, op + 1);
height1[next] = tem2;
}
}
}
int main()
{
ll t;
read(t);
while(t--)
{
ll n, k, c;
read(n); read(k); read(c);
for(ll i = 1; i < n; i++)
{
ll x, y;
read(x); read(y);
G[x].push_back(y);
G[y].push_back(x);
}
dfs1(1, 1);
dfs2(1, 1, 0);
ll ans = 0;
for(ll i = 1; i <= n; i++)
{
ans = max(ans, rec[i] * k - cnt[i] * c);
G[i].clear();
}
write(ans);
putchar('\n');
}
return 0;
}
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· Manus的开源复刻OpenManus初探
· AI 智能体引爆开源社区「GitHub 热点速览」
· 从HTTP原因短语缺失研究HTTP/2和HTTP/3的设计差异
· 三行代码完成国际化适配,妙~啊~