惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

C
Check Point Blog
aimingoo的专栏
aimingoo的专栏
Jina AI
Jina AI
Microsoft Security Blog
Microsoft Security Blog
IT之家
IT之家
V
Visual Studio Blog
量子位
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
博客园 - 聂微东
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
大猫的无限游戏
大猫的无限游戏
Y
Y Combinator Blog
Stack Overflow Blog
Stack Overflow Blog
D
Docker
MyScale Blog
MyScale Blog
小众软件
小众软件
云风的 BLOG
云风的 BLOG
美团技术团队
Microsoft Azure Blog
Microsoft Azure Blog
酷 壳 – CoolShell
酷 壳 – CoolShell
Last Week in AI
Last Week in AI
Apple Machine Learning Research
Apple Machine Learning Research
博客园 - 【当耐特】

某岛

AtCoder Beginner Contest 409 Luogu P5325. 【模板】Min_25 筛 UOJ #188. 【UR #13】Sanrd AtCoder Beginner Contest 371 AtCoder Beginner Contest 369 RPGMaker 2k3 百科 OneShot 的考古 2024“开创拓芯”游戏创享节的相关记录 CJ 回来后的戒断反应 Luogu P10221. [省选联考 2024] 重塑时光 Luogu P5308 [COCI2018-2019#4] Akvizna wqs 二分 歌唱王国 Lean 相关 BZOJ 3153. Sone1 The 2023 ICPC World Finals Luxor 新巴别塔 Sora 的想象与思考 Facebook Hacker Cup 2023 Round 1 AtCoder Beginner Contest 322 LLaMA 2 相关 HuggingFace AI Game Jam ACL 2023 Trans 相关… Luogu P2053. [SCOI2007] 修车 Luogu P1973. [NOI2011] NOI 嘉年华 Luogu P1933. [NOI2010] 旅行路线 Luogu P1954. [NOI2010] 航空管制 Luogu P2048. [NOI2010] 超级钢琴 Luogu P2046. [NOI2010] 海拔
Luogu P3647. [APIO2014] 连珠线
2023-06-05 · via 某岛

June 5, 2023

无根树不太好设计状态,想办法转成有根树,然后用子树替换大法。
再跑换根 dp。
我永远爱宏。

#include <lastweapon/io>
using namespace lastweapon;

const int N = int(2e5) + 9;

int d[N][2], e[N][2], z;
VII adj[N];
int n;

#define dv (max(d[v][0], d[v][1] + w))
#define tv ((d[v][0] + w) - dv)
#define dp (max(e[u][0], e[u][1] + pw))
#define tp ((e[u][0] + pw) - dp)

void dfs1(int u = 1, int p = 0) {
    int t = -INF;
    for (auto _: adj[u]) {
        int v = _.fi; if (v == p) continue; int w = _.se;
        dfs1(v, u); d[u][0] += dv;
        checkMax(t, tv);
    }
    d[u][1] = d[u][0] + t;
}

void dfs2(int u = 1, int p = 0, int pw = -INF) {

    checkMax(z, d[u][0] + dp);

    int t0 = tp, t1 = -INF, v0 = -1;
    for (auto _: adj[u]) {
        int v = _.fi; if (v == p) continue; int w = _.se;   if (tv > t0) t1 = t0, t0 = tv, v0 = v;
        else if (tv > t1) t1 = tv;
    }

    for (auto _: adj[u]) {
        int v = _.fi; if (v == p) continue; int w = _.se;
        e[v][0] = d[u][0] - dv + dp;
        e[v][1] = e[v][0] + (v0 == v ? t1 : t0);
        dfs2(v, u, w);
    }
}

int main() {

#ifndef ONLINE_JUDGE
    freopen("in.txt", "r", stdin);
    //freopen("/Users/minakokojima/Documents/GitHub/ACM-Training/Workspace/out.txt", "w", stdout);
#endif

    DO(RD(n)-1) {
        int x, y, w; RD(x, y, w);
        adj[x].PB({y,w});
        adj[y].PB({x,w});
    }

    dfs1(); dfs2();
    cout << z << endl;
}

Posted by xiaodao
Category: 日常