답안 #849404

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
849404 2023-09-14T14:59:20 Z luanmenglei Paths (RMI21_paths) C++17
100 / 100
183 ms 23888 KB
#include <bits/stdc++.h>
using namespace std;

void debug(const char *msg, ...) {
#ifdef CLESIP
    va_list arg;
    static char pbString[512];
    va_start(arg,msg);
    vsprintf(pbString,msg,arg);
    cerr << "[DEBUG] " << pbString << "\n";
    va_end(arg);
#endif    
}

using i64 = long long;
using Node = pair<i64, int>;
const int N = 1e5 + 10;
vector<pair<int, int>> G[N];
int n, k, top[N], from[N];
i64 sum[N], val[N], res, ans[N];
bool v[N];
Node son[N], seson[N], oson[N];
set<Node> s, os;

Node reduce(const Node &x, i64 k) { return { x.first - k, x.second }; }

void dfs1(int x, int fa) {
	if (G[x].size() == 1) son[x] = { sum[x], x };
	for (auto [y, z] : G[x]) if (y != fa) {
		sum[y] = sum[x] + z;
		dfs1(y, x);
		if (son[x] < son[y]) from[x] = y, seson[x] = son[x], son[x] = son[y];		
		else if (seson[x] < son[y]) seson[x] = son[y];
	}
}

void dfs2(int x, int fa) {
	for (auto [y, z] : G[x]) if (y != fa) {
		top[son[y].second] = x;
		if (from[x] == y) {
			oson[y] = max(oson[x], reduce(seson[x], sum[x]));
		} else {
			oson[y] = max(oson[x], reduce(son[x], sum[x]));
		}
		oson[y].first += z;
		dfs2(y, x);
	}
	if (son[x].second) top[son[x].second] = fa;
} 

void update(int x, i64 d) {
	if (v[x]) {
		auto it = s.find({ val[x], x });
		s.erase(it), res -= val[x];
		val[x] += d;
		if (!os.empty() && prev(os.end()) -> first > val[x]) {
			it = prev(os.end());
			s.insert(*it), res += it -> first, v[it -> second] = true;
			os.erase(it);
			os.emplace(val[x], x), v[x] = false;
		} else {
			s.emplace(val[x], x), res += val[x];
		}
	} else {
		auto it = os.find({ val[x], x });
		os.erase(it);
		val[x] += d;
		if ((int) s.size() < k || s.begin()->first < val[x]) {
			it = s.begin();
			res -= it -> first, v[it -> second] = false, os.insert(*it);
			s.erase(it);
			s.emplace(val[x], x), res += val[x], v[x] = true;
		} else {
			os.emplace(val[x], x);
		}
	}
}

void dfs3(int x, int fa) {
	ans[x] = res;
	for (auto [y, z] : G[x]) if (y != fa) {
		update(son[y].second, -z);
		update(oson[y].second, z);
		dfs3(y, x);
		update(son[y].second, z);
		update(oson[y].second, -z);
	}
}

int main() {
	ios::sync_with_stdio(false), cin.tie(0), cout.tie(0);
	cin >> n >> k;
	for (int i = 1, x, y, z; i < n; i ++) {
		cin >> x >> y >> z;
		G[x].emplace_back(y, z);
		G[y].emplace_back(x, z);
	}
	dfs1(1, 0), dfs2(1, 0);
	for (int i = 1; i <= n; i ++) if (G[i].size() == 1) {
		val[i] = sum[i] - sum[top[i]];
		s.emplace(val[i], i);
		v[i] = true, res += val[i];
		if ((int) s.size() > k) {
			auto it = s.begin();
			v[it -> second] = false, res -= it -> first;
			os.insert(*it), s.erase(it);
		}
	}
	dfs3(1, 0);
	for (int i = 1; i <= n; i ++) cout << ans[i] << "\n";
	return 0;
}
# 결과 실행 시간 메모리 Grader output
1 Correct 3 ms 10328 KB Output is correct
2 Correct 3 ms 10328 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 3 ms 10328 KB Output is correct
2 Correct 3 ms 10328 KB Output is correct
3 Correct 2 ms 10328 KB Output is correct
4 Correct 2 ms 10584 KB Output is correct
5 Correct 2 ms 10584 KB Output is correct
6 Correct 2 ms 10332 KB Output is correct
7 Correct 3 ms 10328 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 3 ms 10328 KB Output is correct
2 Correct 3 ms 10328 KB Output is correct
3 Correct 2 ms 10328 KB Output is correct
4 Correct 2 ms 10584 KB Output is correct
5 Correct 2 ms 10584 KB Output is correct
6 Correct 2 ms 10332 KB Output is correct
7 Correct 3 ms 10328 KB Output is correct
8 Correct 4 ms 10548 KB Output is correct
9 Correct 3 ms 10584 KB Output is correct
10 Correct 3 ms 10588 KB Output is correct
11 Correct 3 ms 10584 KB Output is correct
12 Correct 3 ms 10588 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 3 ms 10328 KB Output is correct
2 Correct 3 ms 10328 KB Output is correct
3 Correct 2 ms 10328 KB Output is correct
4 Correct 2 ms 10584 KB Output is correct
5 Correct 2 ms 10584 KB Output is correct
6 Correct 2 ms 10332 KB Output is correct
7 Correct 3 ms 10328 KB Output is correct
8 Correct 4 ms 10548 KB Output is correct
9 Correct 3 ms 10584 KB Output is correct
10 Correct 3 ms 10588 KB Output is correct
11 Correct 3 ms 10584 KB Output is correct
12 Correct 3 ms 10588 KB Output is correct
13 Correct 4 ms 10584 KB Output is correct
14 Correct 4 ms 10840 KB Output is correct
15 Correct 3 ms 10588 KB Output is correct
16 Correct 4 ms 10584 KB Output is correct
17 Correct 4 ms 10584 KB Output is correct
18 Correct 3 ms 10584 KB Output is correct
19 Correct 4 ms 10584 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 144 ms 18696 KB Output is correct
2 Correct 151 ms 23492 KB Output is correct
3 Correct 94 ms 17716 KB Output is correct
4 Correct 148 ms 20784 KB Output is correct
5 Correct 168 ms 21840 KB Output is correct
6 Correct 141 ms 20940 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 3 ms 10328 KB Output is correct
2 Correct 3 ms 10328 KB Output is correct
3 Correct 2 ms 10328 KB Output is correct
4 Correct 2 ms 10584 KB Output is correct
5 Correct 2 ms 10584 KB Output is correct
6 Correct 2 ms 10332 KB Output is correct
7 Correct 3 ms 10328 KB Output is correct
8 Correct 4 ms 10548 KB Output is correct
9 Correct 3 ms 10584 KB Output is correct
10 Correct 3 ms 10588 KB Output is correct
11 Correct 3 ms 10584 KB Output is correct
12 Correct 3 ms 10588 KB Output is correct
13 Correct 4 ms 10584 KB Output is correct
14 Correct 4 ms 10840 KB Output is correct
15 Correct 3 ms 10588 KB Output is correct
16 Correct 4 ms 10584 KB Output is correct
17 Correct 4 ms 10584 KB Output is correct
18 Correct 3 ms 10584 KB Output is correct
19 Correct 4 ms 10584 KB Output is correct
20 Correct 144 ms 18696 KB Output is correct
21 Correct 151 ms 23492 KB Output is correct
22 Correct 94 ms 17716 KB Output is correct
23 Correct 148 ms 20784 KB Output is correct
24 Correct 168 ms 21840 KB Output is correct
25 Correct 141 ms 20940 KB Output is correct
26 Correct 165 ms 21040 KB Output is correct
27 Correct 183 ms 23380 KB Output is correct
28 Correct 147 ms 23756 KB Output is correct
29 Correct 97 ms 18000 KB Output is correct
30 Correct 135 ms 21200 KB Output is correct
31 Correct 130 ms 19248 KB Output is correct
32 Correct 146 ms 22096 KB Output is correct
33 Correct 140 ms 21052 KB Output is correct
34 Correct 89 ms 17692 KB Output is correct
35 Correct 174 ms 21132 KB Output is correct
36 Correct 130 ms 23888 KB Output is correct