답안 #538027

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
538027 2022-03-16T03:25:19 Z jamezzz Paths (RMI21_paths) C++17
31 / 100
600 ms 13516 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{
		s2.erase(s2.find(x));
	}
}
 
void dfs(int u,int p){
	mx[u]=0;
	if(sz(AL[u])==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(int i=1;i<=n;++i)pf("%lld ",mx[i]);pf("\n");
	//pf("%lld %d %lld %d\n\n",f.fi,f.se,s.fi,s.se);
	//pf("%d:\n",u);
	//for(ll i:s1)pf("%lld ",i);pf("\n");
	//for(ll i:s2)pf("%lld ",i);pf("\n");
	for(auto [v,w]:AL[u]){
		if(v==p)continue;
		//pf("%d\n",v);
		if(f.se==v){
			//pf("case 1: %d\n",w);
			ll t=mx[u];
			mx[u]=s.fi;
			rem(mx[v]+w);add(mx[v]);
			add(mx[u]+w);
			if(mx[u]>0)rem(mx[u]);
			reroot(v,u);
			rem(mx[u]+w);
			if(mx[u]>0)add(mx[u]);
			add(mx[v]+w);rem(mx[v]);
			mx[u]=t;
		}
		else{
			//pf("case 2: %d\n",w);
			ll t=mx[u];
			mx[u]=f.fi;
			rem(mx[v]+w);add(mx[v]);
			add(mx[u]+w);
			if(mx[u]>0)rem(mx[u]);
			reroot(v,u);
			rem(mx[u]+w);
			if(mx[u]>0)add(mx[u]);
			add(mx[v]+w);rem(mx[v]);
			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:159:4: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
  159 |  sf("%d%d",&n,&k);
      |    ^
Main.cpp:162:5: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
  162 |   sf("%d%d%d",&x,&y,&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 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 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 2644 KB Output is correct
7 Correct 2 ms 2644 KB Output is correct
8 Execution timed out 1088 ms 2644 KB Time limit exceeded
9 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 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 2644 KB Output is correct
7 Correct 2 ms 2644 KB Output is correct
8 Execution timed out 1088 ms 2644 KB Time limit exceeded
9 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 235 ms 11468 KB Output is correct
2 Correct 280 ms 13516 KB Output is correct
3 Correct 171 ms 9116 KB Output is correct
4 Correct 220 ms 11468 KB Output is correct
5 Correct 227 ms 12492 KB Output is correct
6 Correct 222 ms 11564 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 2644 KB Output is correct
7 Correct 2 ms 2644 KB Output is correct
8 Execution timed out 1088 ms 2644 KB Time limit exceeded
9 Halted 0 ms 0 KB -