Submission #538026

# Submission time Handle Problem Language Result Execution time Memory
538026 2022-03-16T03:21:16 Z jamezzz Paths (RMI21_paths) C++17
31 / 100
291 ms 13816 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){
	auto it=s1.find(x);
	if(it!=s1.end()){
		cur-=x;
		s1.erase(it);
		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){
		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[v],t2=mx[u];
			mx[u]=s.fi;
			mx[v]=max(mx[v],s.fi+w);
			rem(s.fi);add(s.fi+w);
			rem(t+w);add(t);
			reroot(v,u);
			add(s.fi);rem(s.fi+w);
			add(t+w);rem(t);
			mx[v]=t;
			mx[u]=t2;
		}
		else{
			//pf("case 2: %d\n",w);
			ll t=mx[v],t2=mx[u];
			mx[u]=f.fi;
			mx[v]=f.fi+w;
			rem(f.fi);add(f.fi+w);
			//pf("here\n");
			rem(t+w);add(t);
			//pf("here\n");
			reroot(v,u);
			add(f.fi);rem(f.fi+w);
			add(t+w);rem(t);
			mx[v]=t;
			mx[u]=t2;
		}
		//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:162:4: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
  162 |  sf("%d%d",&n,&k);
      |    ^
Main.cpp:165:5: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
  165 |   sf("%d%d%d",&x,&y,&c);
      |     ^
# Verdict Execution time Memory Grader output
1 Correct 2 ms 2644 KB Output is correct
2 Correct 2 ms 2644 KB Output is correct
# Verdict Execution time Memory 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 3 ms 2644 KB Output is correct
7 Correct 3 ms 2644 KB Output is correct
# Verdict Execution time Memory 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 3 ms 2644 KB Output is correct
7 Correct 3 ms 2644 KB Output is correct
8 Incorrect 4 ms 2772 KB Output isn't correct
9 Halted 0 ms 0 KB -
# Verdict Execution time Memory 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 3 ms 2644 KB Output is correct
7 Correct 3 ms 2644 KB Output is correct
8 Incorrect 4 ms 2772 KB Output isn't correct
9 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 255 ms 11496 KB Output is correct
2 Correct 272 ms 13816 KB Output is correct
3 Correct 173 ms 9088 KB Output is correct
4 Correct 291 ms 11408 KB Output is correct
5 Correct 225 ms 12528 KB Output is correct
6 Correct 242 ms 11620 KB Output is correct
# Verdict Execution time Memory 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 3 ms 2644 KB Output is correct
7 Correct 3 ms 2644 KB Output is correct
8 Incorrect 4 ms 2772 KB Output isn't correct
9 Halted 0 ms 0 KB -