跳到正文

个人记录

lzm

CuteGirlGC约 12 分钟
原文
文章信息与版本

文章信息

发布
2025年10月17日
收录
2025年12月12日
更新
2026年9月6日
篇幅
约 12 分钟

版本

  1. v2当前
  2. v1

A

简单题,但是 5×1055\times10^5 看成 3×1053\times10^5 了。。。

C++
#include <iostream>
#include <vector>

using namespace std;
using LL = long long;

const int kMaxN = 5e5 + 5;

LL n, ans = -1e18, f[2][kMaxN];
vector<pair<LL, LL>> g[kMaxN];

void D(int x, int fa) {
	for (auto _ : g[x]) {
		LL i = _.first, w = _.second;
		if (i != fa) {
			D(i, x);
			LL nd = f[0][i] + w;
			if (nd > f[0][x]) {
				f[1][x] = f[0][x];
				f[0][x] = nd;
			} else if (f[1][x] < nd) {
				f[1][x] = nd;
			}
		}
	}
	ans = max(ans, f[0][x] + f[1][x]);
}

int main() {
	cin >> n;
	for (int i = 1, u, v, w; i < n; i++) {
		cin >> u >> v >> w;
		g[u].push_back({v, w});
		g[v].push_back({u, w});
	}
	D(1, 0);
	cout << ans << '\n';
	return '^'^'^';
}

B

有点思维的题目,赛时没有想到。

由于无权树上任意一个节点,与它距离最远的节点一定是直径的某个端点,所以把直径的端点求出来,然后对于每个不在直径上的叶子节点,判断哪个端点与它距离较远,加上贡献即可。

最后删到只剩直径时,就一个一个删即可。

C++
#include <iostream>
#include <vector>
#include <cstring>
#include <algorithm>

using namespace std;
using LL = long long;

const int kMaxN = 2e5 + 5;

struct Edge {
  int to, nxt;
};

Edge e[kMaxN << 1];
int head[kMaxN], dis[kMaxN], fa[kMaxN], n, cnt, u, v;
bool on[kMaxN];
vector<pair<int, int>> s;
LL ans;

void dfs(int now) {
  for (int i = head[now]; i; i = e[i].nxt) {
    if (dis[e[i].to] == -1) {
      dis[e[i].to] = dis[now] + 1;
      dfs(e[i].to);
    }
  }
}

void dfs2(int now) {
  for (int i = head[now]; i; i = e[i].nxt) {
    if (dis[e[i].to] > dis[now]) {
      fa[e[i].to] = now;
      dfs2(e[i].to);
      on[now] = on[now] || on[e[i].to];
    }
  }
}

void dfs3(int now, int rt) {
  for (int i = head[now]; i; i = e[i].nxt) {
    if (dis[e[i].to] > dis[now]) {
      dfs3(e[i].to, on[e[i].to] ? e[i].to : rt);
    }
  }
  if (!on[now]) {
    if (dis[now] > dis[now] + dis[v] - (dis[rt] << 1)) {
      ans += dis[now];
      s.emplace_back(u, now);
    } else {
      ans += dis[now] + dis[v] - (dis[rt] << 1);
      s.emplace_back(v, now);
    }
  }
}

int main() {
  cin >> n;
  for (int i = 1; i < n; ++i) {
    cin >> u >> v;
    e[++cnt] = {v, head[u]};
    head[u] = cnt;
    e[++cnt] = {u, head[v]};
    head[v] = cnt;
  }
  memset(dis, -1, sizeof dis);
  dis[1] = 0;
  dfs(1);
  u = 1;
  for (int i = 2; i <= n; ++i) {
    if (dis[i] > dis[u]) {
      u = i;
    }
  }
  memset(dis, -1, sizeof dis);
  dis[u] = 0;
  dfs(u);
  v = u;
  for (int i = 1; i <= n; ++i) {
    if (dis[i] > dis[v]) {
      v = i;
    }
  }
  on[v] = 1;
  dfs2(u);
  dfs3(u, u);
  for (; u != v; v = fa[v]) {
    ans += dis[v];
    s.emplace_back(u, v);
  }
  cout << ans << '\n';
  for (auto i : s) {
  	cout << i.first << ' ' << i.second << ' ' << i.second << '\n';
  }
  return 0;
}

C

赛事想到了,但是换根换错了。

考虑枚举断边,然后对于两个连通分量分别求最长链的最小值(半径)和直径,再取 max⁡\max 就是这种情况的答案,再取个 min⁡\min 即可。

C++
#include <cstring>
#include <iostream>
#include <tuple>
#include <vector>

using namespace std;
using LL = long long;

const int kMaxN = 5005;

LL n, f[kMaxN][2], as1, as2;
vector<pair<LL, LL>> g[kMaxN];
vector<tuple<LL, LL, LL>> e;

void D(LL x, LL fa, LL dep) {
    for (auto _ : g[x]) {
        LL i = _.first, w = _.second;
        if (i != fa) {
            D(i, x, dep + w);
            LL nd = f[i][1] + w;
            if (nd > f[x][1]) {
                f[x][0] = f[x][1];
                f[x][1] = nd;
            } else if (nd > f[x][0]) {
                f[x][0] = nd;
            }
        }
    }
    as1 = max(as1, f[x][0] + f[x][1]);
}

void S(LL x, LL fa, LL d) {
    LL zd = 0, cd = 0;
    for (auto _ : g[x]) {
        LL i = _.first, w = _.second;
        if (i != fa) {
            if (f[i][1] + w > zd) {
                cd = zd, zd = f[i][1] + w;
            } else if (f[i][1] + w > cd) {
                cd = f[i][1] + w;
            }
        }
    }
    if (d > zd) {
        cd = zd, zd = d;
    } else if (d > cd) {
        cd = d;
    }
    as2 = min(as2, max(zd, d));
    for (auto _ : g[x]) {
        LL i = _.first, w = _.second;
        if (i != fa) {
            LL nd = (f[i][1] + w == zd ? cd : zd);
            S(i, x, max(d, nd) + w);
        }
    }
}

int main() {
    cin >> n;
    for (int i = 1, u, v, w; i < n; i++) {
        cin >> u >> v >> w;
        g[u].push_back({v, w});
        g[v].push_back({u, w});
        e.emplace_back(u, v, w);
    }

    LL ans = 1e18;
    for (auto i : e) {
        memset(f, 0, sizeof f);
        LL u = get<0>(i), v = get<1>(i), w = get<2>(i);
        as2 = 1e18;
        as1 = 0;
        D(u, v, 0);
        S(u, v, 0);
        LL maxn1 = as1, c1 = as2;
        as1 = 0;
        as2 = 1e18;
        D(v, u, 0);
        S(v, u, 0);
        LL maxn2 = as1, c2 = as2;
        LL d = max(max(maxn1, maxn2), c1 + c2 + w);
        ans = min(ans, d);
    }
    cout << ans << '\n';
    return 0;
}

D

先求出图上任意两点的距离,然后枚举路径 ii 到 jj,对于每对 (i,j)(i,j),再枚举结点 kk 找偏心距。考虑求偏心距。

设 fi,jf_{i, j} 表示 ii 到 jj 的距离,路径 (i,j)(i,j) 对应的偏心距就是 fi,k+fj,k−fi,j2\frac{f_{i, k}+f_{j, k}-f_{i, j}}{2} 的最大值,答案就是每条路径对应偏心距的最小值。

C++
#include <iostream>
#include <algorithm>
#include <cstring>

using namespace std;

const int kMaxN = 305, kInf = 1e9;

int f[kMaxN][kMaxN], n, s, ans = kInf;

int main() {
  cin >> n >> s;
  memset(f, 0x3f, sizeof f);
  for (int i = 1, u, v, w; i < n; ++i) {
    cin >> u >> v >> w;
    f[u][v] = f[v][u] = w;
  }
  for (int i = 1; i <= n; ++i) {
    f[i][i] = 0;
  }
  for (int k = 1; k <= n; ++k) {
    for (int i = 1; i <= n; ++i) {
      for (int j = 1; j <= n; ++j) {
        f[i][j] = min(f[i][j], f[i][k] + f[k][j]);
      }
    }
  }
  for (int i = 1; i <= n; ++i) {
    for (int j = 1; j <= n; ++j) {
      if (f[i][j] <= s) {
        int mx = 0;
        for (int k = 1; k <= n; ++k) {
          mx = max(mx, (f[i][k] + f[j][k] - f[i][j]) / 2);
        }
        ans = min(ans, mx);
      }
    }
  }
  cout << ans;
  return 0;
}
原文