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

推荐订阅源

N
Netflix TechBlog - Medium
Blog — PlanetScale
Blog — PlanetScale
月光博客
月光博客
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
CTFtime.org: upcoming CTF events
CTFtime.org: upcoming CTF events
爱范儿
爱范儿
量子位
博客园 - 聂微东
Engineering at Meta
Engineering at Meta
WordPress大学
WordPress大学
GbyAI
GbyAI
MyScale Blog
MyScale Blog
IT之家
IT之家
P
Proofpoint News Feed
M
MIT News - Artificial intelligence
The Cloudflare Blog
H
Hackread – Cybersecurity News, Data Breaches, AI and More
Hugging Face - Blog
Hugging Face - Blog
The Register - Security
The Register - Security
Microsoft Security Blog
Microsoft Security Blog
博客园_首页
MongoDB | Blog
MongoDB | Blog
F
Fortinet All Blogs
博客园 - 三生石上(FineUI控件)
Y
Y Combinator Blog
雷峰网
雷峰网
V
Visual Studio Blog
酷 壳 – CoolShell
酷 壳 – CoolShell
Last Week in AI
Last Week in AI
博客园 - 叶小钗
D
DataBreaches.Net
B
Blog
B
Blog RSS Feed
大猫的无限游戏
大猫的无限游戏
aimingoo的专栏
aimingoo的专栏
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
The GitHub Blog
The GitHub Blog
云风的 BLOG
云风的 BLOG
Recent Announcements
Recent Announcements
阮一峰的网络日志
阮一峰的网络日志
小众软件
小众软件
腾讯CDC
T
Threat Research - Cisco Blogs
SecWiki News
SecWiki News
Martin Fowler
Martin Fowler
D
Docker
Cisco Talos Blog
Cisco Talos Blog
T
Tenable Blog
Webroot Blog
Webroot Blog
宝玉的分享
宝玉的分享

某岛

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 P3227. [HNOI2013] 切糕 Luogu P8500. [NOI2022] 冒泡排序 Luogu P3629. [APIO2010] 巡逻 USACO 2018 February Contest, Gold Problem 2. Directory Traversal Luogu P3647. [APIO2014] 连珠线 IZhO 2017. Problem F. Hard route SPOJ TWOPATHS. Two Paths 换根 dp 洪恩电脑 —— 开天辟地 Facebook Hacker Cup 2022 Round 2 Codeforces Round #875 Luogu P5828 边双连通图计数 EC Final 拉格朗日反演定理 Luogu P5827. 点双连通图计数 无标号连通图 AtCoder Beginner Contest 284 Luogu P4708. 画画 Luogu P6295. 有标号 DAG 计数 BZOJ #2863. 愤怒的元首 HDU 3303. Harmony Forever 聊聊《明日方舟 Side Story 孤星》与《崩坏:星穹铁道》 SGU 208. Toral Tickets 后日谈,SHLUG 月度分享(上) 钢琴练习 EasyRPG x ChatGPT ControlNet 相关 The 1st Universal Cup, Stage 4, Ukraine EasyRPG —— Sliding Puzzle The 1st Universal Cup, Stage 3, Poland DP 优化练习 NOI 2009 Nas 买来做什么… Global Game Jam 2023 参赛纪录 The 1st Universal Cup, Stage 2, Hongkong The 1st Universal Cup, Stage 0, Nanjing Codeforces Round #850 舟游同人游戏 RM2k3 机能增强 —— EasyRPG Player 魔改版 《海之歌》设定与剧本 dfs 序求 lca Codeforces Round #844 P3768 简单的数学题 AtCoder Beginner Contest 281 ChatGPT 相关 AtCoder Grand Contest 059 AtCoder Beginner Contest 280 Codeforces Global Round 24 事实核查,以乌鲁木齐火灾为例 SPOJ MUSKET. Musketeers Pinely Round 1 Note about FTX Permutation ICPC World Final 2021 CodeTON Round 3 Codeforces Round #831 Educational Codeforces Round 138 NovelAI 法术指南 卡农 Educational Codeforces Round 135 Codeforces Round #819 瓦喵之夏 NOI 2022 Luogu P3765 总统选举 Luogu P3369 【模板】普通平衡树 网络国家 旋转卡壳 OFAC Sanctions && Tornado Cash BZOJ 1185. [HNOI2007]最小矩形覆盖 Codeforces Round #814
TypeDB Forces 2023
2023-02-10 · via 某岛

Problem G. Colorful Tree Again

题意:给定一个边权图,初始固定每个边有颜色和边权。每个节点也有颜色,初始都是黑色。
每次操作可以改变一个节点的颜色,并询问当前最大的好的路径的权值和是多少。
一个路径是好的,当且仅当路径上的所有点颜色相同,并包含了所有该颜色的边,且路径上经过的所有节点的颜色为白。

解法:这个题首先第一反应是 QTREE 4 和 QTREE 6 的某种结合。。。 但是仔细读题发现判定的条件是 path 而不是 component !!!
因而题目难度大幅简化。。。但是想写得简洁依然会很困难。。。

首先第一步当然是剔除不合法的颜色。。。(最好用度数做简单的判定,不要在 dfs() 的时候判定。。)
然后再 dfs() 的时候,剩下的路径都有且仅有一个最高点。。我们对所有最高点位 u 的路径都放在一个堆里维护,记做 Q[u]…
那么在用所有的 Q[u] 组织一个堆来维护最优解。。(类似动态直径问题的点分树做法里最有的堆。。。)

那么每次 toggle 一个点时,除了会影响 Q[u].. 还会影响 u 向上的一条路径。。
记录那条路径所在的堆为 Q[y],那么如果现在 Q[y] 包含在 ans 里时,更新 Q[y] 前后还要顺手更新一下 ans。
无论那种情况,我们现在都可以 O(1) 的 push()、pop() 操作进行处理。。

那么我们只需要实现一个可删除堆即可。。。stl 里的堆自然是不可定向删除的。。。
所以现在主流的 Dijkstra 模板里要求可删除堆的时候。。其实我们都是做延迟处理。。(尽管 clrs 里其实有可删除堆的相关代码。)

这里我们也可以用类似的方法。。。
对于每个堆,再开一个辅助堆,记录删除过的值即可。。

#include <lastweapon/io>
using namespace lastweapon;

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

struct heap {
	priority_queue<LL>Q, q;
	void push(LL x){if(x)Q.push(x);}
	void pop(LL x){if(x)q.push(x);}
	LL top() {
		while(!q.empty() && Q.top() == q.top()) Q.pop(), q.pop();
		if (Q.empty()) return 0;
		return Q.top();
	}
};

vector<pair<int, PII>> adj[N]; map<int, int> sub[N]; heap Q[N], ans;
LL W[N]; bool path[N], bad[N]; int top[N], upc[N], del[N];
 // 颜色的权值和,该颜色是否组成路径,节点是否已损坏,颜色的最高代表点,点到父亲的边的颜色,除最高点外路径被阻塞的次数
int n;



void dfs(int u = 1, int p = 0) {
    for (auto e: adj[u]) {
        int v = e.fi; if (v == p) continue;
        int w = e.se.fi, c = e.se.se;
        if (!top[c]) top[c] = u;
        upc[v] = c; dfs(v, u);
    }
}

void fix(int x) {
    if (bad[x]) {
        ans.pop(Q[x].top());
        if (x != 1) {
            int c = upc[x]; if (!path[c]) return;
            if (del[c]++) return;
            int y = top[c];
            if (!bad[y]) ans.pop(Q[y].top());
            Q[y].pop(W[c]);
            if (!bad[y]) ans.push(Q[y].top());
        }

    } else {
        ans.push(Q[x].top());
        if (x != 1) {
            int c = upc[x]; if (!path[c]) return;
            if (--del[c]) return;
            int y = top[c];
            if (!bad[y]) ans.pop(Q[y].top());
            Q[y].push(W[c]);
            if (!bad[y]) ans.push(Q[y].top());
        }
    }
}

int main() {
#ifndef ONLINE_JUDGE
     freopen("in.txt", "r", stdin);
#endif
    int q; RD(n, q); DO(n-1) {
		int x, y, w, c; RD(x, y, w, c); ++sub[c][x]; ++sub[c][y]; W[c] += w;
		adj[x].PB({y, {w, c}}); adj[y].PB({x, {w, c}});
	}

	dfs();

    REP_1(c, n) if (W[c]) {
        int c1 = 0; path[c] = 1;
        for (auto e: sub[c]) {
            if (e.se == 1) ++c1;
            else if (e.se != 2) {
                path[c] = 0;
                break;
            }
        }
        path[c] &= c1 == 2;
        if (path[c]) Q[top[c]].push(W[c]);
    }

    REP_1(i, n) ans.push(Q[i].top());

    DO(q) {
	    int p, x; RD(p, x); bad[x] = !p; fix(x);
        printf("%lld\n", ans.top());
    }
}

Posted by xiaodao
Category: 日常