답안 #704148

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
704148 2023-03-01T17:23:11 Z TimDee Paths (RMI21_paths) C++17
56 / 100
253 ms 47220 KB
#include <bits/stdc++.h>
using namespace std;
 
//#pragma GCC optimize("O3")
//#pragma GCC optimize("Ofast")
//#pragma GCC optimize("O2")

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 = 1e15;
const int mod = 998244353;

const int n = 1e5;
vector<int> qans(n,0);

multiset<int> maxs, mins;
int sum = 0;

vector<pi> in(n,{0,0}),out(n,{0,0});

vector<vector<pi>> adj(n);
void dfs(int u, int p) {
	in[u]={-1,u};
	for (auto&e:adj[u]) {
		int v=e.f, w=e.s;
		if (v==p) continue;
		dfs(v,u);
		in[u]=max(in[u],{in[v].f+w,in[v].s});
	}
	if (in[u].f==-1) in[u].f=0;
}

void dfs2(int u, int p, pi o) {
	if (u) out[u]=o;
	vector<pi> pr={{-1,-1}};
	pi sf={-1,-1};
	for (auto&e:adj[u]) {
		int v=e.f, w=e.s;
		if (v==p) {
			pr.pb(pr.back());
			continue;
		}
		pr.pb(max(pr.back(),{in[v].f+w,in[v].s}));
	}
	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;
		dfs2(v,u,max({ make_pair(o.f+w,o.s) , make_pair(sf.f+w,sf.s) , make_pair(pr[i].f+w,pr[i].s) }));
		sf=max(sf,{in[v].f+w,in[v].s});
	}
}

vector<int> cnt(n,0);

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

void reroot(int u, int p, int W) {
	int Z=0,V=0,X,ZZ=0,VV=0,XX=0,S=0,SS=0;
	if (u) {
		if (maxs.count(cnt[out[u].s])) {
			Z=1;
			maxs.erase(maxs.find(cnt[out[u].s]));
			cnt[out[u].s]+=W;
			maxs.insert(cnt[out[u].s]);
			sum+=W;
		} else {
			mins.erase(mins.find(-cnt[out[u].s]));
			cnt[out[u].s]+=W;
			if (cnt[out[u].s]>(*maxs.begin())) {
				V=1;
				int x=*maxs.begin();
				X=x;
				maxs.erase(maxs.begin());
				maxs.insert(cnt[out[u].s]);
				sum+=cnt[out[u].s]-x;
				mins.insert(-x);
			} else {
				mins.insert(-cnt[out[u].s]);
			}
		}
		
		if (maxs.count(cnt[in[u].s])) {
			ZZ=1;
			maxs.erase(maxs.find(cnt[in[u].s]));
			sum-=cnt[in[u].s];
			cnt[in[u].s]-=W;
			
			if (cnt[in[u].s] >= -(*mins.begin())) {
				VV=1;
				maxs.insert(cnt[in[u].s]);
				sum+=cnt[in[u].s];
			} else if (mins.size()) {
				S=1;
				int x=*mins.begin();
				mins.erase(mins.begin());
				XX=-x;
				sum+=-x;
				maxs.insert(-x);
				mins.insert(-cnt[in[u].s]);
			}
		} else {
			mins.erase(mins.find(-cnt[in[u].s]));
			cnt[in[u].s]-=W;
			mins.insert(-cnt[in[u].s]);
		}
	}

	qans[u]=sum;
	for (auto&e:adj[u]) {
		int v=e.f, w=e.s;
		if (v==p) continue;
		reroot(v,u,w);
	}

	if (u) {
		if (Z) {
			maxs.erase(maxs.find(cnt[out[u].s]));
			cnt[out[u].s]-=W;
			maxs.insert(cnt[out[u].s]);
			sum-=W;
		} else {
			if (V) {
				mins.erase(mins.find(-X));
				maxs.erase(maxs.find(cnt[out[u].s]));
				maxs.insert(X);
				sum-=cnt[out[u].s]-X;
			} else {
				mins.erase(mins.find(-cnt[out[u].s]));
			}
			cnt[out[u].s]-=W;
			mins.insert(-cnt[out[u].s]);
		}
		
		if (ZZ) {
			if (VV) {
				sum-=cnt[in[u].s];
				maxs.erase(maxs.find(cnt[in[u].s]));
			} else if (S) {
				mins.erase(mins.find(-cnt[in[u].s]));
				maxs.erase(maxs.find(XX));
				sum-=XX;
				mins.insert(-XX);
			}
			cnt[in[u].s]+=W;
			sum+=cnt[in[u].s];
			maxs.insert(cnt[in[u].s]);
		} else {
			mins.erase(mins.find(-cnt[in[u].s]));
			cnt[in[u].s]+=W;
			mins.insert(-cnt[in[u].s]);
		}
	}
}

void solve() {

	int n,k; 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});
	}
	dfs(0,0);
	dfs2(0,0,{0,0});
	cntdfs(0,0);
	ans[0].pb(mx[0]);
	if (adj[0].size()==1) ans[0].pb({0,0});
	sort(rall(ans[0]));
	for (int i=0; i<ans[0].size(); ++i) {
		cnt[ans[0][i].s]=ans[0][i].f;
		if (i<k) {
			maxs.insert(ans[0][i].f);
			sum+=ans[0][i].f;
		} else mins.insert(-ans[0][i].f);
	}

	if (k>=ans[0].size()) {
		forn(i,n) cout<<sum<<'\n';
	}

	reroot(0,0,0);
	forn(i,n) cout<<qans[i]<<'\n';

}
     
int32_t main() {
    int t=1;
    //cin>>t;
    while (t--) solve();
    return 0;
}

Compilation message

Main.cpp: In function 'void reroot(long long int, long long int, long long int)':
Main.cpp:89:35: warning: unused variable 'SS' [-Wunused-variable]
   89 |  int Z=0,V=0,X,ZZ=0,VV=0,XX=0,S=0,SS=0;
      |                                   ^~
Main.cpp: In function 'void solve()':
Main.cpp:200:17: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<std::pair<long long int, long long int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  200 |  for (int i=0; i<ans[0].size(); ++i) {
      |                ~^~~~~~~~~~~~~~
Main.cpp:208:7: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<std::pair<long long int, long long int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  208 |  if (k>=ans[0].size()) {
      |      ~^~~~~~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 6 ms 11220 KB Output is correct
2 Correct 7 ms 11200 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 6 ms 11220 KB Output is correct
2 Correct 7 ms 11200 KB Output is correct
3 Correct 7 ms 11284 KB Output is correct
4 Correct 8 ms 11220 KB Output is correct
5 Correct 7 ms 11220 KB Output is correct
6 Correct 8 ms 11220 KB Output is correct
7 Correct 6 ms 11220 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 6 ms 11220 KB Output is correct
2 Correct 7 ms 11200 KB Output is correct
3 Correct 7 ms 11284 KB Output is correct
4 Correct 8 ms 11220 KB Output is correct
5 Correct 7 ms 11220 KB Output is correct
6 Correct 8 ms 11220 KB Output is correct
7 Correct 6 ms 11220 KB Output is correct
8 Correct 10 ms 11280 KB Output is correct
9 Correct 10 ms 11476 KB Output is correct
10 Correct 10 ms 11348 KB Output is correct
11 Correct 9 ms 11348 KB Output is correct
12 Correct 8 ms 11276 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 6 ms 11220 KB Output is correct
2 Correct 7 ms 11200 KB Output is correct
3 Correct 7 ms 11284 KB Output is correct
4 Correct 8 ms 11220 KB Output is correct
5 Correct 7 ms 11220 KB Output is correct
6 Correct 8 ms 11220 KB Output is correct
7 Correct 6 ms 11220 KB Output is correct
8 Correct 10 ms 11280 KB Output is correct
9 Correct 10 ms 11476 KB Output is correct
10 Correct 10 ms 11348 KB Output is correct
11 Correct 9 ms 11348 KB Output is correct
12 Correct 8 ms 11276 KB Output is correct
13 Correct 10 ms 11476 KB Output is correct
14 Correct 9 ms 11624 KB Output is correct
15 Correct 8 ms 11516 KB Output is correct
16 Correct 10 ms 11476 KB Output is correct
17 Correct 9 ms 11428 KB Output is correct
18 Correct 10 ms 11424 KB Output is correct
19 Correct 10 ms 11412 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Runtime error 253 ms 47220 KB Execution killed with signal 11
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 6 ms 11220 KB Output is correct
2 Correct 7 ms 11200 KB Output is correct
3 Correct 7 ms 11284 KB Output is correct
4 Correct 8 ms 11220 KB Output is correct
5 Correct 7 ms 11220 KB Output is correct
6 Correct 8 ms 11220 KB Output is correct
7 Correct 6 ms 11220 KB Output is correct
8 Correct 10 ms 11280 KB Output is correct
9 Correct 10 ms 11476 KB Output is correct
10 Correct 10 ms 11348 KB Output is correct
11 Correct 9 ms 11348 KB Output is correct
12 Correct 8 ms 11276 KB Output is correct
13 Correct 10 ms 11476 KB Output is correct
14 Correct 9 ms 11624 KB Output is correct
15 Correct 8 ms 11516 KB Output is correct
16 Correct 10 ms 11476 KB Output is correct
17 Correct 9 ms 11428 KB Output is correct
18 Correct 10 ms 11424 KB Output is correct
19 Correct 10 ms 11412 KB Output is correct
20 Runtime error 253 ms 47220 KB Execution killed with signal 11
21 Halted 0 ms 0 KB -