0714A/A+
已结束
IOI
开始于: 2026-7-14 14:30
3
小时
主持人:
48
#include <bits/stdc++.h>
using namespace std;
#define int long long
const int N = 2e5 + 5;
struct node {
int v, w;
};
vector<node>e[N];
int n, a[N], dis[N], siz[N], tot, id[N], fa[N], lst[N];
int book[N];
struct Node {
int Max, id, tag;
} tree[N << 2];
void dfs(int x, int f, int dist) {
dis[x] = dis[f] + dist;
a[x] = dist;
siz[x] = 1;
id[x] = ++tot;
lst[tot] = x;
fa[x] = f;
for (int i = 0; i < e[x].size(); i++) {
int v = e[x][i].v;
int w = e[x][i].w;
if (v == f)
continue;
dfs(v, x, w);
siz[x] += siz[v];
}
}
void pushup(int rt) {
if (tree[rt << 1].Max > tree[rt << 1 | 1].Max) {
tree[rt].Max = tree[rt << 1].Max;
tree[rt].id = tree[rt << 1].id;
} else {
tree[rt].Max = tree[rt << 1 | 1].Max;
tree[rt].id = tree[rt << 1 | 1].id;
}
}
void pushdown(int rt) {
if (tree[rt].tag) {
tree[rt << 1].tag += tree[rt].tag;
tree[rt << 1 | 1].tag += tree[rt].tag;
tree[rt << 1].Max += tree[rt].tag;
tree[rt << 1 | 1].Max += tree[rt].tag;
tree[rt].tag = 0;
}
}
void build(int l, int r, int rt) {
if (l == r) {
tree[rt] = {dis[lst[l]], lst[l], 0};
return;
}
int mid = (l + r) >> 1;
build(l, mid, rt << 1);
build(mid + 1, r, rt << 1 | 1);
pushup(rt);
}
void update(int l, int r, int rt, int L, int R, int c) {
if (L <= l && r <= R) {
tree[rt].tag += c;
tree[rt].Max += c;
return;
}
pushdown(rt);
int mid = (l + r) >> 1;
if (L <= mid)
update(l, mid, rt << 1, L, R, c);
if (R > mid)
update(mid + 1, r, rt << 1 | 1, L, R, c);
pushup(rt);
}
pair<int, int> query(int l, int r, int rt, int L, int R) {
if (L <= l && r <= R) {
return make_pair(tree[rt].Max, tree[rt].id);
}
pushdown(rt);
int mid = (l + r) >> 1;
pair<int, int>res = {0, 0};
if (L <= mid) {
pair<int, int>Left = query(l, mid, rt << 1, L, R);
if (res.first < Left.first) {
res = Left;
}
}
if (R > mid) {
pair<int, int>Right = query(mid + 1, r, rt << 1 | 1, L, R);
if (res.first < Right.first) {
res = Right;
}
}
return res;
}
signed main() {
cin >> n;
for (int i = 1; i < n; i++) {
int x, y, z;
cin >> x >> y >> z;
e[x].push_back({y, z});
e[y].push_back({x, z});
}
dfs(1, 0, 0);
build(1, n, 1);
int res = 0;
for (int i = 1; i <= n; i++) {
pair<int, int>ret = query(1, n, 1, 1, n);
// cout << ret.first << " " << ret.second << endl;
res += ret.first * 2;
// cout << 111 << endl;
int x = ret.second;
while (book[x] == 0 && x != 1) {
book[x] = 1;
update(1, n, 1, id[x], id[x] + siz[x] - 1, -a[x]);
x = fa[x];
}
cout << res << endl;
}
return 0;
}
#include <bits/stdc++.h>
using namespace std;
const int N = 105;
vector<int>e[N];
int n, m, dis[N];
void dfs(int x, int f) {
if (x != 0)
dis[x] = dis[f] + 1;
else
dis[x] = 1;
for (auto v : e[x]) {
if (v == f)
continue;
dfs(v, x);
}
}
int main() {
cin >> n >> m;
for (int i = 1; i < n; i++) {
int x, y;
cin >> x >> y;
e[x].push_back(y);
e[y].push_back(x);
}
dfs(0, -1);
int maxx = 0;
for (int i = 0; i < n; i++) {
maxx = max(maxx, dis[i]);
}
if (m < maxx)
cout << m + 1 << endl;
else
cout << min(n, maxx + (m - maxx + 1) / 2) << endl;
return 0;
}
- 状态
- 已结束
- 规则
- IOI
- 题目
- 4
- 开始于
- 2026-7-14 14:30
- 结束于
- 2026-7-14 17:30
- 持续时间
- 3 小时
- 主持人
- 参赛人数
- 48