답안 #538058

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
538058 2022-03-16T04:18:04 Z jamezzz Paths (RMI21_paths) C++17
100 / 100
322 ms 14764 KB
#include <bits/stdc++.h>
using namespace std;
 
#ifdef DEBUG
#define dbg(...) printf(__VA_ARGS__);
#define getchar_unlocked getchar
#else
#define dbg(...)
#endif
#define sf scanf
#define pf printf
#define fi first
#define se second
#define pb push_back
#define sz(x) (int)x.size()
#define mnto(x,y) x=min(x,(__typeof__(x))y)
#define mxto(x,y) x=max(x,(__typeof__(x))y)
#define INF 1023456789
#define LINF 1023456789123456789
#define all(x) x.begin(), x.end()
#define disc(x) sort(all(x));x.resize(unique(all(x))-x.begin());
typedef long long ll;
typedef long double ld;
typedef pair<int, int> ii;
typedef pair<ll, int> pll;
typedef tuple<int, int, int> iii;
typedef tuple<int, int, int, int> iiii;
typedef vector<int> vi;
typedef vector<ii> vii;
typedef vector<pll> vll;
mt19937 rng(time(0));
 
#define mod 1000000007
 
inline int add(int a,int b){
	int r=a+b;
	while(r>=mod)r-=mod;
	while(r<0)r+=mod;
	return r;
}
 
inline int mult(int a,int b){
	return (int)(((ll)(a*b))%mod);
}
 
inline int rd(){
	int x=0;
	char ch=getchar_unlocked();
	while(!(ch&16))ch=getchar();//keep reading while current character is whitespace
    while(ch&16){//this will break when ‘\n’ or ‘ ‘ is encountered
		x=(x<<3)+(x<<1)+(ch&15);
		ch=getchar_unlocked();
	}
	return x;
}
 
#define maxn 100005
 
int n,k;
vii AL[maxn];
ll ans[maxn],mx[maxn],cur;
multiset<ll> s1,s2;
 
void add(ll x){
	s1.insert(x);
	cur+=x;
	if(sz(s1)>k){
		cur-=*s1.begin();
		s2.insert(*s1.begin());
		s1.erase(s1.begin());
	}
}
 
void rem(ll x){
	if(s1.find(x)!=s1.end()){
		cur-=x;
		s1.erase(s1.find(x));
		if(!s2.empty()){
			ll v=*(--s2.end());
			cur+=v;
			s1.insert(v);
			s2.erase(--s2.end());
		}
	}
	else{
		if(s2.find(x)!=s2.end())s2.erase(s2.find(x));
	}
}
 
void dfs(int u,int p){
	mx[u]=0;
	if(sz(AL[u])==1&&p!=-1){
		add(0);
		return;
	}
	for(auto[v,w]:AL[u]){
		if(v==p)continue;
		dfs(v,u);
		rem(mx[v]);
		add(mx[v]+w);
		mxto(mx[u],mx[v]+w);
	}
}
 
void reroot(int u,int p){
	//pf("reroot: %d %d\n",u,p);
	ans[u]=cur;
	pll f={0,u},s={0,u};
	for(auto[v,w]:AL[u]){
		if(mx[v]+w>=f.fi){
			s=f;
			f={mx[v]+w,v};
		}
		else if(mx[v]+w>s.fi){
			s={mx[v]+w,v};
		}
	}
	for(auto[v,w]:AL[u]){
		if(v==p)continue;
		//pf("%d\n",v);
		ll t=mx[u];
		if(f.se==v)mx[u]=s.fi;
		else mx[u]=f.fi;
		rem(mx[v]+w);
		add(mx[v]);
		rem(mx[u]);
		add(mx[u]+w);
		reroot(v,u);
		rem(mx[u]+w);
		add(mx[u]);
		rem(mx[v]);
		add(mx[v]+w);
		mx[u]=t;
		//pf("%d:\n",u);
		//for(ll i:s1)pf("%lld ",i);pf("\n");
		//for(ll i:s2)pf("%lld ",i);pf("\n");
	}
}
 
int main(){
	sf("%d%d",&n,&k);
	for(int i=1;i<n;++i){
		int x,y,c;
		sf("%d%d%d",&x,&y,&c);
		AL[x].pb({y,c});
		AL[y].pb({x,c});
	}
	dfs(1,-1);
	reroot(1,-1);
	for(int i=1;i<=n;++i)pf("%lld\n",ans[i]);
}
 
/*
11 3
1 2 5
2 3 3
2 6 5
3 4 4
3 5 2
1 7 6
7 8 4
7 9 5
1 10 1
10 11 1
*/

Compilation message

Main.cpp: In function 'int main()':
Main.cpp:141:4: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
  141 |  sf("%d%d",&n,&k);
      |    ^
Main.cpp:144:5: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
  144 |   sf("%d%d%d",&x,&y,&c);
      |     ^
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 2644 KB Output is correct
2 Correct 2 ms 2660 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 2644 KB Output is correct
2 Correct 2 ms 2660 KB Output is correct
3 Correct 2 ms 2644 KB Output is correct
4 Correct 3 ms 2656 KB Output is correct
5 Correct 3 ms 2656 KB Output is correct
6 Correct 2 ms 2644 KB Output is correct
7 Correct 2 ms 2644 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 2644 KB Output is correct
2 Correct 2 ms 2660 KB Output is correct
3 Correct 2 ms 2644 KB Output is correct
4 Correct 3 ms 2656 KB Output is correct
5 Correct 3 ms 2656 KB Output is correct
6 Correct 2 ms 2644 KB Output is correct
7 Correct 2 ms 2644 KB Output is correct
8 Correct 3 ms 2644 KB Output is correct
9 Correct 4 ms 2772 KB Output is correct
10 Correct 3 ms 2644 KB Output is correct
11 Correct 3 ms 2668 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 2660 KB Output is correct
3 Correct 2 ms 2644 KB Output is correct
4 Correct 3 ms 2656 KB Output is correct
5 Correct 3 ms 2656 KB Output is correct
6 Correct 2 ms 2644 KB Output is correct
7 Correct 2 ms 2644 KB Output is correct
8 Correct 3 ms 2644 KB Output is correct
9 Correct 4 ms 2772 KB Output is correct
10 Correct 3 ms 2644 KB Output is correct
11 Correct 3 ms 2668 KB Output is correct
12 Correct 3 ms 2644 KB Output is correct
13 Correct 6 ms 2772 KB Output is correct
14 Correct 7 ms 2900 KB Output is correct
15 Correct 5 ms 2772 KB Output is correct
16 Correct 5 ms 2772 KB Output is correct
17 Correct 5 ms 2772 KB Output is correct
18 Correct 4 ms 2800 KB Output is correct
19 Correct 5 ms 2796 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 244 ms 11488 KB Output is correct
2 Correct 283 ms 13564 KB Output is correct
3 Correct 168 ms 9172 KB Output is correct
4 Correct 296 ms 11468 KB Output is correct
5 Correct 227 ms 12492 KB Output is correct
6 Correct 280 ms 11672 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 2644 KB Output is correct
2 Correct 2 ms 2660 KB Output is correct
3 Correct 2 ms 2644 KB Output is correct
4 Correct 3 ms 2656 KB Output is correct
5 Correct 3 ms 2656 KB Output is correct
6 Correct 2 ms 2644 KB Output is correct
7 Correct 2 ms 2644 KB Output is correct
8 Correct 3 ms 2644 KB Output is correct
9 Correct 4 ms 2772 KB Output is correct
10 Correct 3 ms 2644 KB Output is correct
11 Correct 3 ms 2668 KB Output is correct
12 Correct 3 ms 2644 KB Output is correct
13 Correct 6 ms 2772 KB Output is correct
14 Correct 7 ms 2900 KB Output is correct
15 Correct 5 ms 2772 KB Output is correct
16 Correct 5 ms 2772 KB Output is correct
17 Correct 5 ms 2772 KB Output is correct
18 Correct 4 ms 2800 KB Output is correct
19 Correct 5 ms 2796 KB Output is correct
20 Correct 244 ms 11488 KB Output is correct
21 Correct 283 ms 13564 KB Output is correct
22 Correct 168 ms 9172 KB Output is correct
23 Correct 296 ms 11468 KB Output is correct
24 Correct 227 ms 12492 KB Output is correct
25 Correct 280 ms 11672 KB Output is correct
26 Correct 266 ms 12308 KB Output is correct
27 Correct 321 ms 14312 KB Output is correct
28 Correct 262 ms 14764 KB Output is correct
29 Correct 207 ms 10176 KB Output is correct
30 Correct 280 ms 12716 KB Output is correct
31 Correct 290 ms 11516 KB Output is correct
32 Correct 322 ms 13644 KB Output is correct
33 Correct 288 ms 12704 KB Output is correct
34 Correct 181 ms 9800 KB Output is correct
35 Correct 282 ms 12732 KB Output is correct
36 Correct 252 ms 14704 KB Output is correct