正文
增加 25 行删除 18 行
10 行未改动111213141516171819202122232425 行未改动505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310 行未改动vector<int> g[N];void dfs(int u, int cnt) {if (cnt >= d[u]) return ; // 最优性剪枝vis[u] = 1;// 从n号点到达u号点的距离是?cntd[u] = min(d[u], cnt);for (auto e : g[u]) {if (vis[e]) continue;dfs(e, cnt+1);}25 行未改动int off[]; // off[i] 表示i是否被关闭void dfs() {for () {if (vis[e] or off[e]) continue;}}signed main() {qwq;for (int i=1; i<=n; i++) {for (int j=1; j<=n; j++) // 找到一个没有被删除的点dfs(j);看这个点能否到达目前所有的点 n-i+1cin >> x;off[x] = 1;}return 0;}```## T5每一条边,价值可以看作每一个点的子节点对一条边的贡献:一端是子节点的子树和另一端是n-子树和```cppint dfs(int u) {// 求子树和的代码int sum = 1;vis[u] = 1;for (auto e : g[u]) {if (vis[e]) continue;int t = dfs(e);// 增加一个计算每条边的费用的式子// u -- e相连,e这边的节点数量就是子树和,t// 另一边就是 n - tsum += t;}return sum;}```10 行未改动111213141516171819202122232425 行未改动505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911010 行未改动vector<int> g[N];void dfs(int u, int cnt) {if (cnt >= d[u]) return ; // 最优性剪枝vis[u] = 1;// 从n号点到达u号点的距离是?cntd[u] = min(d[u], cnt);for (auto e : g[u]) {if (vis[e]) continue;dfs(e, cnt+1);}25 行未改动int off[]; // off[i] 表示i是否被关闭void dfs() {for () {if (vis[e] or off[e]) continue;}}signed main() {qwq;for (int i=1; i<=n; i++) {for (int j=1; j<=n; j++) // 找到一个没有被删除的点dfs(j);看这个点能否到达目前所有的点 n-i+1cin >> x;off[x] = 1;}return 0;}```## T5```cpp#include <bits/stdc++.h>#define int long long#define qwq ios::sync_with_stdio(0), cin.tie(0), cout.tie(0)using namespace std;int res;int dfs(int u) {// 求子树和的代码int sum = 1;vis[u] = 1;for (auto e : g[u]) {if (vis[e]) continue;int t = dfs(e); // 以e为根的子树的节点数// 增加一个计算每条边的费用的式子// u -- e相连,e这边的节点数量就是子树和,t// 另一边就是 n - t// (n-t-t) * e.wres += abs(n-t-t)*e.w;sum += t; // 子树和}return sum;}signed main() {qwq;dfs(1);return 0;}```