답안 #702720

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
702720 2023-02-24T22:36:47 Z TimDee Paths (RMI21_paths) C++17
68 / 100
600 ms 101080 KB
#include <bits/stdc++.h>
using namespace std;
 
#pragma GCC optimize("O3")
#pragma GCC optimize("unroll-loops")
#pragma GCC target("avx2,popcnt")

using ll = long long;
#define int long long
#define forn(i,n) for(int i=0; i<(n); ++i)
#define pb push_back
#define pi pair<int,int>
#define f first
#define s second
#define vii(a,n) vector<int> a(n); forn(i,n) cin>>a[i];
#define all(x) x.begin(), x.end()
#define rall(x) x.rbegin(), x.rend()

mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
const int inf = INT_MAX;
const int mod = 998244353;

int n=1e5,k;
vector<vector<pi>> adj(n);
vector<vector<ll>> ans(n);
vector<int> mx(n,-1);

vector<int> d(n,0);
vector<int> paiuans(n,0);
void dfspaiu(int u, int p) {
	for(auto&e:adj[u]) {
		int v=e.f,w=e.s;
		if (v==p) continue;
		dfspaiu(v,u);
		d[u]=max(d[u],d[v]+w);
	}
}
void paia(int u, int p, int out) {
	vector<int> pref(1,0);
	paiuans[u]=max(d[u],out);
	for(auto&e:adj[u]) {
		int v=e.f,w=e.s;
		if (v==p) {pref.pb(pref.back()); continue;}
		pref.pb(max(pref.back(),d[v]+w));
	}
	int suf=0;
	int sz=adj[u].size();
	for(int i=sz-1; i>=0; --i) {
		auto e=adj[u][i];
		int v=e.f,w=e.s;
		if (v==p) continue;
		//cout<<v<<' '<<w<<"  "<<pref[i]<<' '<<suf<<'\n';
		paia(v,u,max({pref[i],suf,out})+w);
		suf=max(suf,d[v]+w);
	}
}
void paiu() {
	dfspaiu(0,-1);
	paia(0,-1,0);
	forn(i,n) cout<<paiuans[i]<<'\n';
	exit(0);
}

void dfs(int u,int p) {
	int m=-1;
	for(auto&e:adj[u]) {
		int v=e.f,w=e.s;
		if (v==p) continue;
		dfs(v,u);
		if (ans[u].size()<ans[v].size()) swap(ans[u],ans[v]);
		if (mx[v]+w>m) {
			if (m!=-1) ans[u].pb(m);
			m=mx[v]+w;
		} else {
			ans[u].pb(mx[v]+w);
		}
		for(auto&x:ans[v]) ans[u].pb(x);
	}
	mx[u]=max(m,0ll);
}

void solve() {

	cin>>n>>k;
	forn(i,n-1) {
		int u,v,w; cin>>u>>v>>w; --u, --v;
		adj[u].pb({v,w});
		adj[v].pb({u,w});
	}
	if (k==1) paiu();
	forn(u,n) {
		forn(i,n) ans[i].clear();
		mx.assign(n,-1);
		dfs(u,-1);
		ll answer=mx[u];
		sort(rall(ans[u]));

		forn(i,min(k-1,(int)ans[u].size())) {
			answer+=ans[u][i];
		}
		cout<<answer<<'\n';
	}

}

int32_t main() {
    ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
    int t=1;
    //cin>>t;
    while (t--) solve();
    return 0;
}
# 결과 실행 시간 메모리 Grader output
1 Correct 4 ms 7380 KB Output is correct
2 Correct 3 ms 7380 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 4 ms 7380 KB Output is correct
2 Correct 3 ms 7380 KB Output is correct
3 Correct 5 ms 7556 KB Output is correct
4 Correct 5 ms 7508 KB Output is correct
5 Correct 5 ms 7508 KB Output is correct
6 Correct 4 ms 7380 KB Output is correct
7 Correct 5 ms 7508 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 4 ms 7380 KB Output is correct
2 Correct 3 ms 7380 KB Output is correct
3 Correct 5 ms 7556 KB Output is correct
4 Correct 5 ms 7508 KB Output is correct
5 Correct 5 ms 7508 KB Output is correct
6 Correct 4 ms 7380 KB Output is correct
7 Correct 5 ms 7508 KB Output is correct
8 Correct 52 ms 10444 KB Output is correct
9 Correct 40 ms 10592 KB Output is correct
10 Correct 30 ms 8048 KB Output is correct
11 Correct 51 ms 13732 KB Output is correct
12 Correct 34 ms 8868 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 4 ms 7380 KB Output is correct
2 Correct 3 ms 7380 KB Output is correct
3 Correct 5 ms 7556 KB Output is correct
4 Correct 5 ms 7508 KB Output is correct
5 Correct 5 ms 7508 KB Output is correct
6 Correct 4 ms 7380 KB Output is correct
7 Correct 5 ms 7508 KB Output is correct
8 Correct 52 ms 10444 KB Output is correct
9 Correct 40 ms 10592 KB Output is correct
10 Correct 30 ms 8048 KB Output is correct
11 Correct 51 ms 13732 KB Output is correct
12 Correct 34 ms 8868 KB Output is correct
13 Correct 241 ms 25932 KB Output is correct
14 Correct 184 ms 19628 KB Output is correct
15 Correct 106 ms 8976 KB Output is correct
16 Correct 221 ms 29016 KB Output is correct
17 Correct 145 ms 13388 KB Output is correct
18 Correct 114 ms 16840 KB Output is correct
19 Correct 219 ms 19692 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 74 ms 15640 KB Output is correct
2 Correct 83 ms 19532 KB Output is correct
3 Correct 70 ms 15632 KB Output is correct
4 Correct 74 ms 15660 KB Output is correct
5 Correct 81 ms 17048 KB Output is correct
6 Correct 77 ms 15588 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 4 ms 7380 KB Output is correct
2 Correct 3 ms 7380 KB Output is correct
3 Correct 5 ms 7556 KB Output is correct
4 Correct 5 ms 7508 KB Output is correct
5 Correct 5 ms 7508 KB Output is correct
6 Correct 4 ms 7380 KB Output is correct
7 Correct 5 ms 7508 KB Output is correct
8 Correct 52 ms 10444 KB Output is correct
9 Correct 40 ms 10592 KB Output is correct
10 Correct 30 ms 8048 KB Output is correct
11 Correct 51 ms 13732 KB Output is correct
12 Correct 34 ms 8868 KB Output is correct
13 Correct 241 ms 25932 KB Output is correct
14 Correct 184 ms 19628 KB Output is correct
15 Correct 106 ms 8976 KB Output is correct
16 Correct 221 ms 29016 KB Output is correct
17 Correct 145 ms 13388 KB Output is correct
18 Correct 114 ms 16840 KB Output is correct
19 Correct 219 ms 19692 KB Output is correct
20 Correct 74 ms 15640 KB Output is correct
21 Correct 83 ms 19532 KB Output is correct
22 Correct 70 ms 15632 KB Output is correct
23 Correct 74 ms 15660 KB Output is correct
24 Correct 81 ms 17048 KB Output is correct
25 Correct 77 ms 15588 KB Output is correct
26 Execution timed out 1075 ms 101080 KB Time limit exceeded
27 Halted 0 ms 0 KB -