# |
Submission time |
Handle |
Problem |
Language |
Result |
Execution time |
Memory |
538055 |
2022-03-16T04:13:54 Z |
jamezzz |
Paths (RMI21_paths) |
C++17 |
|
289 ms |
13808 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){
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);
| ^
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
2644 KB |
Output is correct |
2 |
Correct |
2 ms |
2660 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
2644 KB |
Output is correct |
2 |
Correct |
2 ms |
2660 KB |
Output is correct |
3 |
Correct |
2 ms |
2656 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 |
2660 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
2644 KB |
Output is correct |
2 |
Correct |
2 ms |
2660 KB |
Output is correct |
3 |
Correct |
2 ms |
2656 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 |
2660 KB |
Output is correct |
8 |
Incorrect |
3 ms |
2672 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 |
2660 KB |
Output is correct |
3 |
Correct |
2 ms |
2656 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 |
2660 KB |
Output is correct |
8 |
Incorrect |
3 ms |
2672 KB |
Output isn't correct |
9 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
289 ms |
11564 KB |
Output is correct |
2 |
Correct |
233 ms |
13808 KB |
Output is correct |
3 |
Correct |
185 ms |
9264 KB |
Output is correct |
4 |
Correct |
232 ms |
11512 KB |
Output is correct |
5 |
Correct |
238 ms |
12688 KB |
Output is correct |
6 |
Correct |
222 ms |
11824 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
2644 KB |
Output is correct |
2 |
Correct |
2 ms |
2660 KB |
Output is correct |
3 |
Correct |
2 ms |
2656 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 |
2660 KB |
Output is correct |
8 |
Incorrect |
3 ms |
2672 KB |
Output isn't correct |
9 |
Halted |
0 ms |
0 KB |
- |