Submission #891273

# Submission time Handle Problem Language Result Execution time Memory
891273 2023-12-22T15:58:19 Z fanwen Paths (RMI21_paths) C++17
100 / 100
211 ms 23304 KB
#include <bits/stdc++.h>

using namespace std;

#define fi first
#define se second
#define ll long long
#define file(name)                  \
    if(fopen(name".inp", "r"))      \
        freopen(name".inp", "r", stdin), freopen(name".out", "w", stdout); 

const int MAX = 3e5 + 5;

int n, k;
long long dp[MAX];
vector <pair <int, int>> adj[MAX];

namespace sub4 {
	bool check() {
		return n <= 2000;
	}

	void dfs(int u, int p, vector <long long> &res) {
		for (pair <int, int> tmp : adj[u]) if(tmp.fi != p) {
			int v = tmp.fi, w = tmp.se;
			dfs(v, u, res);
			dp[u] = max(dp[u], dp[v] + w);
		}

		bool oke = false;
		for (pair <int, int> tmp : adj[u]) if(tmp.fi != p) {
			int v = tmp.fi, w = tmp.se;
			if(dp[u] != dp[v] + w || oke) res.emplace_back(dp[v] + w);
			else oke = true;
		}
	}

	void solve() {
		for (int i = 1; i <= n; ++i) {
	    	fill(dp + 1, dp + n + 1, 0);
	    	vector <long long> res;
	    	dfs(i, 0, res);
	    	res.push_back(dp[i]);
	    	sort(res.begin(), res.end(), greater <long long>());
	    	long long sum = 0;
	    	for (int j = 0; j < min((int) res.size(), k); ++j) sum += res[j];
	    	cout << sum << '\n';
	    }
	    exit(0);
	}
}

namespace sub_full {
	bool check() {
		return true;
	}

	multiset <long long> values, max_k;
	long long total, ans[MAX];
	pair <long long, long long> dp[MAX];

	void insert(long long val) {
		if(max_k.empty() || val > *max_k.begin()) {
			total += val;
			max_k.insert(val);
		} else {
			values.insert(val);
		}
	}

	bool remove(long long val) {
		if(max_k.count(val)) {
			max_k.erase(max_k.find(val));
			total -= val;
		} else {
			if(!values.count(val)) return false;
			values.erase(values.find(val));
		}
		return true;
	}

	void get_ans() {
		while((int) max_k.size() < k && !values.empty()) {
			total += *values.rbegin(); 
			max_k.insert(*values.rbegin());
			values.erase(values.find(*values.rbegin()));
		}

		while((int) max_k.size() > k) {
			total -= *max_k.begin();
			values.insert(*max_k.begin());
			max_k.erase(max_k.begin());
		}

	}

	void dfs(int u, int p) {
		for (pair <int, int> tmp : adj[u]) if(tmp.fi != p) {
			int v = tmp.fi, w = tmp.se;
			dfs(v, u);
			if(dp[u].fi < dp[v].fi + w) {
				dp[u].se = dp[u].fi;
				dp[u].fi = dp[v].fi + w;
			} else dp[u].se = max(dp[u].se, dp[v].fi + w);
		}

		bool used = false;

		for (pair <int, int> tmp : adj[u]) if(tmp.fi != p) {
			int v, w; tie(v, w) = tmp;
			if(dp[u].fi != dp[v].fi + w || used) insert(dp[v].fi + w);
			else used = true;
		}
	}

	void calc(int u, int p) {
		get_ans();
		ans[u] = (k == 1 ? dp[u].fi : total);

		for (pair <int, int> tmp : adj[u]) if(tmp.fi != p) {
			int v, w; tie(v, w) = tmp;

			long long val1 = dp[v].fi;
			bool tmp1 = remove(val1 + w); insert(val1);
			long long val2 = (dp[u].fi == dp[v].fi + w ? dp[u].se : dp[u].fi);
			bool tmp2 = remove(val2); insert(val2 + w);

			if(dp[v].fi < val2 + w) {
				dp[v].se = dp[v].fi;
				dp[v].fi = val2 + w;
			} else dp[v].se = max(dp[v].se, val2 + w);

			calc(v, u);

			if(tmp1) insert(val1 + w), assert(remove(val1));
			if(tmp2) insert(val2), assert(remove(val2 + w));
		}
	}
	void solve() {
		dfs(1, 0); 
		insert(dp[1].fi);
		calc(1, 0);
		for (int i = 1; i <= n; ++i) cout << ans[i] << "\n";
			exit(0);
	}
}

void you_make_it(void) {
    cin >> n >> k;
    for (int i = 1; i < n; ++i) {
    	int u, v, w; cin >> u >> v >> w;
    	adj[u].emplace_back(v, w);
    	adj[v].emplace_back(u, w);
    }
    if(sub_full::check()) sub_full::solve();
    if(sub4::check()) sub4::solve();
}

signed main() {

#ifdef LOCAL
    freopen("TASK.inp", "r", stdin);
    freopen("TASK.out", "w", stdout);
#endif
    file("paths");
    auto start_time = chrono::steady_clock::now();

    cin.tie(0), cout.tie(0) -> sync_with_stdio(0);

    you_make_it();

    auto end_time = chrono::steady_clock::now();

    cerr << "\nExecution time : " << chrono::duration_cast <chrono::milliseconds> (end_time - start_time).count() << "[ms]" << endl;

    return (0 ^ 0);
}

// Dream it. Wish it. Do it.

Compilation message

Main.cpp: In function 'int main()':
Main.cpp:10:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   10 |         freopen(name".inp", "r", stdin), freopen(name".out", "w", stdout);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
Main.cpp:165:5: note: in expansion of macro 'file'
  165 |     file("paths");
      |     ^~~~
Main.cpp:10:49: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   10 |         freopen(name".inp", "r", stdin), freopen(name".out", "w", stdout);
      |                                          ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~
Main.cpp:165:5: note: in expansion of macro 'file'
  165 |     file("paths");
      |     ^~~~
# Verdict Execution time Memory Grader output
1 Correct 2 ms 10584 KB Output is correct
2 Correct 2 ms 10588 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 10584 KB Output is correct
2 Correct 2 ms 10588 KB Output is correct
3 Correct 2 ms 10588 KB Output is correct
4 Correct 2 ms 10720 KB Output is correct
5 Correct 2 ms 10588 KB Output is correct
6 Correct 2 ms 10588 KB Output is correct
7 Correct 2 ms 10588 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 10584 KB Output is correct
2 Correct 2 ms 10588 KB Output is correct
3 Correct 2 ms 10588 KB Output is correct
4 Correct 2 ms 10720 KB Output is correct
5 Correct 2 ms 10588 KB Output is correct
6 Correct 2 ms 10588 KB Output is correct
7 Correct 2 ms 10588 KB Output is correct
8 Correct 3 ms 10584 KB Output is correct
9 Correct 3 ms 10844 KB Output is correct
10 Correct 4 ms 10736 KB Output is correct
11 Correct 3 ms 10588 KB Output is correct
12 Correct 3 ms 10584 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 10584 KB Output is correct
2 Correct 2 ms 10588 KB Output is correct
3 Correct 2 ms 10588 KB Output is correct
4 Correct 2 ms 10720 KB Output is correct
5 Correct 2 ms 10588 KB Output is correct
6 Correct 2 ms 10588 KB Output is correct
7 Correct 2 ms 10588 KB Output is correct
8 Correct 3 ms 10584 KB Output is correct
9 Correct 3 ms 10844 KB Output is correct
10 Correct 4 ms 10736 KB Output is correct
11 Correct 3 ms 10588 KB Output is correct
12 Correct 3 ms 10584 KB Output is correct
13 Correct 4 ms 10844 KB Output is correct
14 Correct 4 ms 10904 KB Output is correct
15 Correct 3 ms 10732 KB Output is correct
16 Correct 4 ms 10844 KB Output is correct
17 Correct 4 ms 10844 KB Output is correct
18 Correct 4 ms 10844 KB Output is correct
19 Correct 4 ms 10800 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 144 ms 20732 KB Output is correct
2 Correct 136 ms 22796 KB Output is correct
3 Correct 91 ms 18260 KB Output is correct
4 Correct 140 ms 20820 KB Output is correct
5 Correct 155 ms 21564 KB Output is correct
6 Correct 130 ms 20940 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 10584 KB Output is correct
2 Correct 2 ms 10588 KB Output is correct
3 Correct 2 ms 10588 KB Output is correct
4 Correct 2 ms 10720 KB Output is correct
5 Correct 2 ms 10588 KB Output is correct
6 Correct 2 ms 10588 KB Output is correct
7 Correct 2 ms 10588 KB Output is correct
8 Correct 3 ms 10584 KB Output is correct
9 Correct 3 ms 10844 KB Output is correct
10 Correct 4 ms 10736 KB Output is correct
11 Correct 3 ms 10588 KB Output is correct
12 Correct 3 ms 10584 KB Output is correct
13 Correct 4 ms 10844 KB Output is correct
14 Correct 4 ms 10904 KB Output is correct
15 Correct 3 ms 10732 KB Output is correct
16 Correct 4 ms 10844 KB Output is correct
17 Correct 4 ms 10844 KB Output is correct
18 Correct 4 ms 10844 KB Output is correct
19 Correct 4 ms 10800 KB Output is correct
20 Correct 144 ms 20732 KB Output is correct
21 Correct 136 ms 22796 KB Output is correct
22 Correct 91 ms 18260 KB Output is correct
23 Correct 140 ms 20820 KB Output is correct
24 Correct 155 ms 21564 KB Output is correct
25 Correct 130 ms 20940 KB Output is correct
26 Correct 160 ms 21076 KB Output is correct
27 Correct 139 ms 22792 KB Output is correct
28 Correct 152 ms 23304 KB Output is correct
29 Correct 104 ms 18512 KB Output is correct
30 Correct 141 ms 21064 KB Output is correct
31 Correct 211 ms 19792 KB Output is correct
32 Correct 147 ms 22024 KB Output is correct
33 Correct 146 ms 20996 KB Output is correct
34 Correct 89 ms 18260 KB Output is correct
35 Correct 146 ms 21200 KB Output is correct
36 Correct 123 ms 23088 KB Output is correct