版本比较
就算结果已然注定,我也要放手一搏
正文
增加 142 行删除 203 行
12 行未改动13141516171718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081822021222324878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551562526272829301611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282293132333435207 行未改动全部展开24324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238312 行未改动- 耗时:浏览题解 fv9j9qo5 思路后 30min 调出
水蓝。
本题部分分非常充裕,考虑直接暴力枚举每个乡村是否进行城市化改造,再暴力求 MST 最小代价更新答案,可以得到 48pts 的高分,考场上我是对其又进行了一部分随机化乱搞拿到了 60pts。 本题部分分非常充裕,考虑直接暴力枚举每个乡村是否进行城市化改造,再暴力求 MST 最小代价更新答案,可以得到 48pts 的高分。 考场代码又臭又长,也没有参考价值就不放了。::::error[48pts]```cpp#include<bits/stdc++.h>using namespace std;#define int long longconst int maxn = 1e4 + 20, maxm = 2e6, maxk = 12;int n, m, k, cnt, ans = 1e18;int c[maxk], a[maxk][maxn];int fa[maxn];bool isuse[maxk];struct Node { int u, v, w; bool operator<(const Node &x)const & { return w < x.w; }} E[maxm], e[maxm];int find(int x) { return (x == fa[x] ? x : fa[x] = find(fa[x]));}void solve() { int cnt1 = cnt; for (int i = 1; i <= cnt; i++) e[i] = E[i]; int sum = 0; for (int i = 1; i <= k; i++) { if (!isuse[i]) continue; sum += c[i]; for (int j = 1; j <= n; j++) e[++cnt1] = {i + n, j, a[i][j]}; } sort(e + 1, e + cnt1 + 1); for (int i = 1; i <= n + k; i++) fa[i] = i; for (int i = 1; i <= cnt1; i++) { int fu = find(e[i].u), fv = find(e[i].v), w = e[i].w; if (fu == fv) continue; fa[fu] = fv, sum += w; } ans = min(ans, sum);}void dfs(int idx) { if (idx == k + 1) { solve(); return; } dfs(idx + 1); isuse[idx] = 1; dfs(idx + 1); isuse[idx] = 0;}signed main() { ios::sync_with_stdio(false); cin.tie(0), cout.tie(0); cin >> n >> m >> k; for (int i = 1, u, v, w; i <= m; i++) { cin >> u >> v >> w; E[++cnt] = {u, v, w}; } for (int i = 1; i <= k; i++) { cin >> c[i]; for (int j = 1; j <= n; j++) cin >> a[i][j]; } dfs(1); cout << ans; return 0;}```[Link](https://www.luogu.com.cn/record/244832853)::::注意到所有非原图 MST 的边无论是否加入城市化改造乡村的边,都是没有任何竞争力的,可以直接扔掉。 这步操作将一次 `solve` 的复杂度从 $O(m\log m)$ 优化到了 $(nk\log nk)$,可以得到 80pts,这里已经非常接近正解了。::::warning[80pts]```cpp#include<bits/stdc++.h>using namespace std;#define int long longconst int maxn = 1e4 + 20, maxm = 2e6, maxk = 12;int n, m, k, cnt, ans = 1e18;int c[maxk], a[maxk][maxn];int fa[maxn];bool isuse[maxk];struct Node { int u, v, w; bool operator<(const Node &x)const & { return w < x.w; }} E1[maxm], E2[maxm], e[maxm];int find(int x) { return (x == fa[x] ? x : fa[x] = find(fa[x]));}void solve() { int cnt1 = cnt; for (int i = 1; i <= cnt; i++) e[i] = E2[i]; int sum = 0; for (int i = 1; i <= k; i++) { if (!isuse[i]) continue; sum += c[i]; for (int j = 1; j <= n; j++) e[++cnt1] = {i + n, j, a[i][j]}; } sort(e + 1, e + cnt1 + 1); for (int i = 1; i <= n + k; i++) fa[i] = i; for (int i = 1; i <= cnt1; i++) { int fu = find(e[i].u), fv = find(e[i].v), w = e[i].w; if (fu == fv) continue; fa[fu] = fv, sum += w; } ans = min(ans, sum);}void dfs(int idx) { if (idx == k + 1) { solve(); return; } dfs(idx + 1); isuse[idx] = 1; dfs(idx + 1); isuse[idx] = 0;}signed main() { ios::sync_with_stdio(false); cin.tie(0), cout.tie(0); cin >> n >> m >> k; for (int i = 1, u, v, w; i <= m; i++) { cin >> u >> v >> w; E1[i] = {u, v, w}; } for (int i = 1; i <= k; i++) { cin >> c[i]; for (int j = 1; j <= n; j++) cin >> a[i][j]; } sort(E1 + 1, E1 + m + 1); for (int i = 1; i <= n; i++) fa[i] = i; for (int i = 1; i <= m; i++) { int fu = find(E1[i].u), fv = find(E1[i].v); if (fu == fv) continue; fa[fu] = fv, E2[++cnt] = E1[i]; } dfs(1); cout << ans; return 0;}```[Link](https://www.luogu.com.cn/record/244992303)::::
注意到 `dfs` 函数已经无法优化了(严谨地表述是**很难优化**),算法瓶颈在于 `solve` 函数中的 `sort`。考虑提前排序好,若该边不在所选边的集合中,直接跳过即可。
::::success[100pts]```cpp#include<bits/stdc++.h>using namespace std;#define int long longconst int maxn = 1e4 + 20, maxm = 2e6, maxk = 12;int n, m, k, cnt, ans = 1e18;int c[maxk], a[maxk][maxn];int fa[maxn];bool isuse[maxk];struct Node { int u, v, w, idx; bool operator<(const Node &x)const & { return w < x.w; }} E1[maxm], E2[maxm];int find(int x) { return (x == fa[x] ? x : fa[x] = find(fa[x]));}void solve() { int sum = 0; for (int i = 1; i <= k; i++) { if (!isuse[i]) continue; sum += c[i]; } for (int i = 1; i <= n + k; i++) fa[i] = i; for (int i = 1; i <= cnt; i++) { int fu = find(E2[i].u), fv = find(E2[i].v), w = E2[i].w, idx = E2[i].idx; if (fu == fv || !isuse[idx]) continue; fa[fu] = fv, sum += w; } ans = min(ans, sum);}void dfs(int idx) { if (idx == k + 1) { solve(); return; } dfs(idx + 1); isuse[idx] = 1; dfs(idx + 1); isuse[idx] = 0;}signed main() { ios::sync_with_stdio(false); cin.tie(0), cout.tie(0); cin >> n >> m >> k; for (int i = 1, u, v, w; i <= m; i++) { cin >> u >> v >> w; E1[i] = {u, v, w, 0}; } for (int i = 1; i <= k; i++) { cin >> c[i]; for (int j = 1; j <= n; j++) cin >> a[i][j]; } sort(E1 + 1, E1 + m + 1); for (int i = 1; i <= n; i++) fa[i] = i; for (int i = 1; i <= m; i++) { int fu = find(E1[i].u), fv = find(E1[i].v); if (fu == fv) continue; fa[fu] = fv, E2[++cnt] = E1[i]; } for (int i = 1; i <= k; i++) for (int j = 1; j <= n; j++) E2[++cnt] = {i + n, j, a[i][j], i}; sort(E2 + 1, E2 + cnt + 1); isuse[0] = 1; dfs(1); cout << ans; return 0;}```[Link](https://www.luogu.com.cn/record/271780660)::::### P1967 [NOIP 2013 提高组] 货车运输- 难度:蓝- 耗时:一眼瞪出正解,实现花了 30min+。207 行未改动全部展开:::success[100pts][Link](https://www.luogu.com.cn/record/274088741):::::::## Week 3(4.18-4.24)>Kruskal 重构树 >你说得对但是主播 4.27 期中考,这周想把 Kruskal 重构树学习笔记写了,然后复习一下 whk。___### P13548 [OOI 2022] Air Reform- 难度:黑- 耗时:7days 无果,浏览众多题解后独立实现代码并提交题解 c5w8da8r。
一周啃一道完整黑感觉已经很不错了。
这真是神仙题了。爆肝我 8days,看来还是太菜了。
大概转化一下题意:就是说给你一张无向连通图,你需要建它的一个补图,其中补图两点的边权就是在原图中两点最小瓶颈路的最大边权,你需要求出对于原图的每条边 $(u,v)$,求两点在补图中最小瓶颈路的最大边权。
对于最小瓶颈路的最大边权,这个东西显然可以用 Kruskal 重构树解决,求一个 LCA 即可。 所以算法瓶颈在于怎么快速建出补图的重构树。
考虑暴力,注意到在建原图的重构树时是从小到大枚举边权建新节点的,所以我们考虑在每次建新节点的时候枚举左右两个子树集合,若两点在原图中没有边且在补图中还未连通,则连一条边。 可以得到 21pts 的高分。::::warning[21pts][Link](https://www.luogu.com.cn/record/275086448)::::考虑优化。
为了方便表示,对于当前节点 $u$,不妨设其左右子树分别含 $id_l$ 和 $id_r$ 个联通块,$st_x$ 表示第 $x$ 个连通块中的元素个数。
考虑如何合并 $u$ 左右子树的连通块。
不妨先枚举左右子树的连通块 $id_l$ 和 $id_r$,用 $idx$ 和 $idy$ 表示,再枚举每个连通块中间的元素 $st_{idx}$ 和 $st_{idy}$,用 $x$ 和 $y$ 表示,若两点在原图中没有边,且在补图中尚未连通,则将两点相连(到这里和暴力是一样的)。 $x$ 和 $y$ 连边后,考虑将 $st_{idy}$ 从 $id_r$ 中删去,加到 $st_{idx}$ 里去。
这样会存在一个问题,若左子树 $id_l$ 内有两个连通块 $idx$ 和 $idz$ 内均有节点能和 $idy$ 连通块内的节点相连,则会导致这三个连通块合并成一个大的连通块。 但我们若将 $st_{idy}$ 和 $st_{idx}$ 联通后直接将其加入 $st_{idx}$,就会导致建出的树不是标准的重构树,或是建不出一个完整的树。 所以当我们处理完 $st_{idx}$ 内的所有节点后,还要将 $st_{idx}$ 加到 $id_r$ 里去。
然后在做一个启发式合并,复杂度就会变成优秀的 $O(n\log^2n)$。
正确性显然,考虑证明时间复杂度。
- 若 $st_{idx}$ 和 $st_{idy}$ 存在点对使得两连通块连通。显然最多只会连通 $n$ 次(在补图中判连通还要乘一个 $\alpha(n)$),每一次联通后把两个块启发式合并,这一部分复杂度是 $O(\alpha(n)\log n)$ 的。- 若不能连通,则枚举完 $st_{idy}$ 后,会将 $st_{idx}$ 加到 $id_r$ 里,注意到这里也是用启发式合并的,一个点最多会被加入 $\log n$ 次。
同时每次查一下在原图是否有边也是 $\log n$ 的。 所以建补图重构树的复杂度是 $O(m\log^2n+m\alpha(n)\log n)$。
其余部分复杂度显然没有建补图的复杂度大,直接忽视即可。::::success[100pts][Link](https://www.luogu.com.cn/record/275590006)::::___::::info[Other]{}
::::## Week 4(4.25-4.30)>假期限定,对日期做些调整(浮动一天)。
重构树学习笔记:[Link](https://www.luogu.com.cn/article/od7t4alc),由于 P13548 做太慢了被推迟了。___::::info[Other]之后就不记录这一栏了,感觉没啥用,做题记录又不是给别人看的。::::## Week 5(5.1-5.8)>2-SAT### P4782 【模板】2-SAT板。::::success[100pts][Link](https://www.luogu.com.cn/record/276169824)::::::::info[另外四道板题]{open}[P4171](https://www.luogu.com.cn/record/276172156) [P3007](https://www.luogu.com.cn/record/276242706) [P5782](https://www.luogu.com.cn/record/276245068) [P10969](https://www.luogu.com.cn/record/276246541)::::### P13583 [NWRRC 2023] Colorful Village- 难度:紫- 耗时:独立思考 1day 无果,浏览 ppnjjd7z 后独立实现代码并提交题解 exy4xdci。
妙妙题。
~~我是直接搜 2-SAT 搜到这题的,所以秒猜正解快人一步(~~
2-SAT 不好直接求树上连通块,恰好原树是无根树,不妨直接钦定一点为根。
对于点 $i$,不妨设它父亲为 $fa_i$,用 $\lnot$ 表示不取,则可以连下列边。$$\lnot fa_i \to \lnot i\\i \to fa_i\\$$原因显然,为了保证连通,当一个点取了后则它父亲必须取,否则就与上面的连通块断掉了,逆反可得它父亲不取则它也不取。
对于两个同色的点 $u,v$,能且仅能取一点,可以连下列边:$$u \to \lnot v\\v \to \lnot u\\\lnot u \to v\\\lnot v \to u\\$$
但这样还会有一个问题,就是这么操作根默认必取,所以我们不妨算两次,分别以同色的两个点为根。
当然直接以重心为根可以的,就不需要跑两次了。::::success[100pts][Link](https://www.luogu.com.cn/record/276428007)::::___## Week 6(5.9-5.15)>期中考考成史了,OI 时间被大幅挤压,一周一蓝都很极限了。### P13173 [GCJ 2017 #2] Beaming With Joy- 难度:紫>之后如果非独立切的题我就不记录耗时了,感觉没啥意义。
由于每个激光可以横纵随便调,所以输入的横纵方向没有任何作用,大可以直接将地图分为激光,墙,镜子(其实分两种镜子)三种点位。
注意到 $C$ 非常小,处理出每个激光横纵两种方式分别可以射到哪几个位置是可取的。
2-SAT 直接存每个激光位横纵两种方式即可,下面考虑约束(即建边)。
考虑每个空地。
- 没有激光能射到它,无解。- 有一个激光能射到它,那么该激光必须要放该方向。- 有两个激光能射到它。 - 两个激光射到该空地的方向相同(即都是纵向射到空地或都是横向),无解,显然若要射到该空地必然会连锁射到另一个激光位。 - 两个激光射到该空地的方向不同,任选其一即可。- 有三个激光能射到它。 - 三个激光射到该空地方向均相同,无解,理由同上。 - 只有两个激光射到该空地方向相同,一个不同,只能选那个不同的。
$$\dots$$- 有 $k$ 个激光能射到它。 - 对于横纵两种方向,都有 $\ge 2$ 个激光位,无解。 - 存在一种方向上只有 $1$ 个激光位,则必须取该激光必须要放该方向。
然后跑 2-SAT 求方案即可。::::success[100pts][Link](https://www.luogu.com.cn/record/277674228)::::