답안 #538948

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
538948 2022-03-18T05:22:57 Z jamielim Paths (RMI21_paths) C++14
100 / 100
357 ms 16052 KB
#include <bits/stdc++.h>
using namespace std;

#define fi first
#define se second
#define mp make_pair
#define pb emplace_back
#define ALL(x) x.begin(),x.end()
#define SZ(x) (int)x.size()
typedef long long ll;
typedef pair<int,int> ii;
typedef pair<ii,ii> i4;
const int MOD=1000000007;
const int INF=1012345678;
const ll LLINF=1012345678012345678LL;
const double PI=3.1415926536;
const double EPS=1e-14;

const int MAXN=100005;

// problem paths: choose k vertices and take sum of all the vertices
// that you visit by travelling from root to those vertices
// across all possible roots
int n,k;
vector<ii> adj[MAXN];
multiset<ll> take,notake; // (path-to-leaf)s. take stores the top <=k
ll ans[MAXN],cur=0;
ll path[MAXN]; // the best path for this subtree (subtree based on depth from new root)

void add(ll x){
	take.insert(x);
	cur+=x;
	if(SZ(take)>k){
		ll y=(*take.begin());
		take.erase(take.begin()); // multiset
		cur-=y;
		notake.insert(y);
	}
}
void rem(ll x){
	if(take.find(x)!=take.end()){
		take.erase(take.find(x));
		cur-=x;
		while(!notake.empty()&&SZ(take)<k){
			ll y=(*(--notake.end()));
			take.insert(y);
			cur+=y;
			notake.erase(--notake.end());
		}
	}else{
		if(notake.find(x)!=notake.end())notake.erase(notake.find(x));
	}
}

void dfs(int x,int p){
	path[x]=0;
	if(SZ(adj[x])==1&&p!=0){ // is a leaf
		add(0);
		return;
	}
	for(ii i:adj[x]){
		if(i.fi==p)continue;
		dfs(i.fi,x);
		rem(path[i.fi]);
		add(path[i.fi]+i.se);
		path[x]=max(path[x],path[i.fi]+i.se);
		// only the best path that starts from x will progress to p
		// similarly all the paths that started from i.fi stay there,
		// except for the best one that progresses to x
		// (progress -> can consider in take/notake)
	}
}

void reroot(int x,int p){
	ans[x]=cur; // take/notake currently considers x as the root
	pair<ll,int> best,sec;
	for(ii i:adj[x]){ // finding the longest and second longest paths leading to the current root
		if(path[i.fi]+i.se>=best.fi){
			sec=best;
			best=mp(path[i.fi]+i.se,i.fi);
		}else if(path[i.fi]+i.se>sec.fi){
			sec=mp(path[i.fi]+i.se,i.fi);
		}
	}
	ll old=path[x];
	for(ii i:adj[x]){ // try all children as possible roots
		if(i.fi==p)continue;
		if(best.se==i.fi){ // this child was the best so we will extend the second best
			path[x]=sec.fi;
		}else{ // extend the best
			path[x]=best.fi;
		}
		rem(path[x]); add(path[x]+i.se);
		rem(path[i.fi]+i.se); add(path[i.fi]);
		reroot(i.fi,x);
		rem(path[i.fi]); add(path[i.fi]+i.se);
		rem(path[x]+i.se); add(path[x]);
		path[x]=old;
	}
}

int main(){
	scanf("%d%d",&n,&k);
	if(n==1){printf("0\n");return 0;}
	int a,b,c;
	for(int i=1;i<n;i++){
		scanf("%d%d%d",&a,&b,&c);
		adj[a].pb(b,c);
		adj[b].pb(a,c);
	}
	dfs(1,0);
	reroot(1,0);
	for(int i=1;i<=n;i++)printf("%lld\n",ans[i]);
}

Compilation message

Main.cpp: In function 'int main()':
Main.cpp:103:7: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
  103 |  scanf("%d%d",&n,&k);
      |  ~~~~~^~~~~~~~~~~~~~
Main.cpp:107:8: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
  107 |   scanf("%d%d%d",&a,&b,&c);
      |   ~~~~~^~~~~~~~~~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 2644 KB Output is correct
2 Correct 2 ms 2644 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 2644 KB Output is correct
2 Correct 2 ms 2644 KB Output is correct
3 Correct 2 ms 2644 KB Output is correct
4 Correct 2 ms 2644 KB Output is correct
5 Correct 2 ms 2644 KB Output is correct
6 Correct 2 ms 2652 KB Output is correct
7 Correct 3 ms 2644 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 2644 KB Output is correct
2 Correct 2 ms 2644 KB Output is correct
3 Correct 2 ms 2644 KB Output is correct
4 Correct 2 ms 2644 KB Output is correct
5 Correct 2 ms 2644 KB Output is correct
6 Correct 2 ms 2652 KB Output is correct
7 Correct 3 ms 2644 KB Output is correct
8 Correct 4 ms 2644 KB Output is correct
9 Correct 3 ms 2772 KB Output is correct
10 Correct 4 ms 2644 KB Output is correct
11 Correct 4 ms 2772 KB Output is correct
12 Correct 3 ms 2644 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 2644 KB Output is correct
2 Correct 2 ms 2644 KB Output is correct
3 Correct 2 ms 2644 KB Output is correct
4 Correct 2 ms 2644 KB Output is correct
5 Correct 2 ms 2644 KB Output is correct
6 Correct 2 ms 2652 KB Output is correct
7 Correct 3 ms 2644 KB Output is correct
8 Correct 4 ms 2644 KB Output is correct
9 Correct 3 ms 2772 KB Output is correct
10 Correct 4 ms 2644 KB Output is correct
11 Correct 4 ms 2772 KB Output is correct
12 Correct 3 ms 2644 KB Output is correct
13 Correct 6 ms 2792 KB Output is correct
14 Correct 5 ms 2952 KB Output is correct
15 Correct 6 ms 2760 KB Output is correct
16 Correct 6 ms 2860 KB Output is correct
17 Correct 5 ms 2792 KB Output is correct
18 Correct 5 ms 2772 KB Output is correct
19 Correct 5 ms 2852 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 270 ms 13460 KB Output is correct
2 Correct 246 ms 15620 KB Output is correct
3 Correct 182 ms 11164 KB Output is correct
4 Correct 292 ms 13492 KB Output is correct
5 Correct 240 ms 14516 KB Output is correct
6 Correct 357 ms 13648 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 2644 KB Output is correct
2 Correct 2 ms 2644 KB Output is correct
3 Correct 2 ms 2644 KB Output is correct
4 Correct 2 ms 2644 KB Output is correct
5 Correct 2 ms 2644 KB Output is correct
6 Correct 2 ms 2652 KB Output is correct
7 Correct 3 ms 2644 KB Output is correct
8 Correct 4 ms 2644 KB Output is correct
9 Correct 3 ms 2772 KB Output is correct
10 Correct 4 ms 2644 KB Output is correct
11 Correct 4 ms 2772 KB Output is correct
12 Correct 3 ms 2644 KB Output is correct
13 Correct 6 ms 2792 KB Output is correct
14 Correct 5 ms 2952 KB Output is correct
15 Correct 6 ms 2760 KB Output is correct
16 Correct 6 ms 2860 KB Output is correct
17 Correct 5 ms 2792 KB Output is correct
18 Correct 5 ms 2772 KB Output is correct
19 Correct 5 ms 2852 KB Output is correct
20 Correct 270 ms 13460 KB Output is correct
21 Correct 246 ms 15620 KB Output is correct
22 Correct 182 ms 11164 KB Output is correct
23 Correct 292 ms 13492 KB Output is correct
24 Correct 240 ms 14516 KB Output is correct
25 Correct 357 ms 13648 KB Output is correct
26 Correct 278 ms 13836 KB Output is correct
27 Correct 317 ms 15576 KB Output is correct
28 Correct 265 ms 15932 KB Output is correct
29 Correct 207 ms 11328 KB Output is correct
30 Correct 289 ms 13928 KB Output is correct
31 Correct 287 ms 12308 KB Output is correct
32 Correct 281 ms 14696 KB Output is correct
33 Correct 276 ms 13852 KB Output is correct
34 Correct 185 ms 11104 KB Output is correct
35 Correct 307 ms 13896 KB Output is correct
36 Correct 247 ms 16052 KB Output is correct