abc 214 D~G 题解
D
逆向思维,考虑一条边被多少条路径作为答案。
很套路吧,想到每次找到图中最大的边,断开,它的贡献为两边联通块的大小相乘再乘它的权值。反着来,每次用最小的边连接两个块,用并查集维护。
#include <cstdio>
#include <algorithm>
#include <cmath>
#include <cstring>
#define uint unsigned int
#define LL long long
using namespace std;
const int MAXN = 1e5 + 5;
struct Node {
int X, Y, Z;
bool operator < (const Node P) const { return Z < P.Z; }
}arr[MAXN];
int n, fa[MAXN], siz[MAXN];
LL ans;
// 可把删边看成加边
int Find_Set(int x) { return fa[x] != x ? fa[x] = Find_Set(fa[x]) : fa[x]; }
void Union_Set(int x, int y, int z) {
int u = Find_Set(x), v = Find_Set(y);
ans += (LL)siz[u] * siz[v] * z;// printf("|%lld|", ans);
fa[u] = v; siz[v] += siz[u]; siz[u] = 0;
}
int main() {
scanf("%d", &n);
for(int i = 1; i <= n; i ++) fa[i] = i, siz[i] = 1;
for(int i = 1; i < n; i ++) scanf("%d%d%d", &arr[i].X, &arr[i].Y, &arr[i].Z);
sort(arr + 1, arr + n);
for(int i = 1; i < n; i ++) Union_Set(arr[i].X, arr[i].Y, arr[i].Z);
printf("%lld", ans);
return 0;
}
E
对于循环什么的如果发现 for 不好写就写 while
贪心,很常见的贪心。
考虑按左端点排序,维护一个
Code way 1
#include <cstdio>
#include <algorithm>
#include <cmath>
#include <cstring>
#include <queue>
#define uint unsigned int
#define LL long long
using namespace std;
const int MAXN = 2e5 + 5;
struct Node {
int X, Y;
bool operator < (const Node P) const { return P.X == X ? Y < P.Y : X < P.X; }
}arr[MAXN];
int T, n, L;
priority_queue <int, vector <int>, greater <int> > que;
int main() {
scanf("%d", &T);
while(T --) {
bool f = 1;
scanf("%d", &n);
while(!que.empty()) que.pop();
for(int i = 1; i <= n; i ++) scanf("%d%d", &arr[i].X, &arr[i].Y);
sort(arr + 1, arr + 1 + n); L = 0;
int g = 1;
while(que.size() || g <= n) {
while(arr[g].X <= L && g <= n) que.push(arr[g].Y), g ++;
if(que.empty()) { L = arr[g].X + 1; g ++; continue; }
int t = que.top(); que.pop();
if(L > t) { f = 0; break; }
L ++;
}
if(f) printf("Yes\n");
else printf("No\n");
}
return 0;
}
F
emm,之前没学过子序列自动机,,,这其实就是板题(
两种方法:
1.建好子序列自动机,考虑没有相邻限制,对于下标
2.令 $$
md 不写了 咕咕咕
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 地球OL攻略 —— 某应届生求职总结
· 周边上新:园子的第一款马克杯温暖上架
· Open-Sora 2.0 重磅开源!
· 提示词工程——AI应用必不可少的技术
· .NET周刊【3月第1期 2025-03-02】