#pragma GCC optimize("Ofast,unroll-loops")
//#pragma GCC optimize("conserve-stack")
#include<bits/stdc++.h>
#define int long long
#define ll long long
#define down cout<<'\n';
#define debug cout<<" cucuucucuuu",down
#define NHP ios_base::sync_with_stdio(0);cout.tie(0);cin.tie(0);
#define modwwe int t;cin>>t; while(t--)
#define bit(i,j) (i>>j&1)
#define sobit(a) __builtin_popcountll(a)
#define task "test"
#define fin(x) freopen(x".inp","r",stdin)
#define fou(x) freopen(x".ans","w",stdout)
#define pb push_back
#define checktime cerr << (double)clock() / CLOCKS_PER_SEC * 1000 << " ms";
using namespace std;
void phongbeo();
const int inf = 1e18;
const int mod2 = 1e9 + 7;
const int mod1 = 998244353;
const ll base=67;
int add(int x,int y)
{
assert(abs(x)<=mod2);
assert(abs(y)<=mod2);
if(x+y>=mod2) x-=mod2;
if(x+y<0) x+=mod2;
return x+y;
}
struct icd
{
long double a;
int b;
};
struct ib
{
int a;
int b;
};
struct ic
{
int a, b, c;
};
struct id
{
int a, b, c, d;
};
struct ie
{
int a, b, c, d, e;
};
ll n, m, s1, s2, s4, s3, sf, k, s5, s6, mx, s7, s8, s9, mx2, res, dem2 = 0, dem = 0, s33, dem3, dem4, mid, l2, r2, center;
int i, s10, s12,k1,k2,k3,s11,t,lim,w,l,r;
int kk;
int el = 19;
main()
{
if(fopen(task".inp","r"))
{
fin(task);
fou(task);
}
NHP
/// cin>>s1;
// modwwe
phongbeo();
// checktime
}
set<pair<int,int>>sold;
set<pair<int,int>> snew;
int cost[100001],d[100001],par[100001], dist[100001], d2[100001],costnew[100001],ans[100001],b[100001];
vector<ib> v[100001];
void dfs(int x,int y)
{
cost[x]=0;
d[x]=x;
int hihi=0;
par[x]=y;
/// neu canh co nhieu con thi uu tien con lon nhat duoc lay truoc
for(auto f:v[x])
if(f.a^y)
{
dfs(f.a,x);
cost[d[f.a]]+=f.b;
dist[f.a]=f.b;
if(cost[d[f.a]]>hihi)
d[x]=d[f.a],hihi=cost[d[f.a]],b[x]=f.a;
}
}
void add(ib a,int c)
{
pair<int,int> dd= {a.a,a.b};
if(sold.find(dd)!=sold.end())
{
sold.erase(dd);
dd.first+=c;
sold.insert(dd);
}
else
{
snew.erase(dd);
s4+=c;
dd.first+=c;
snew.insert(dd);
}
while(!sold.empty()&&!snew.empty()&&sold.rbegin()->first>snew.begin()->first)
{
pair<int,int> x=*sold.rbegin();
sold.erase(prev(sold.end()));
s4+=x.first;
snew.insert(x);
pair<int,int> y=*snew.begin();
s4-=y.first;
snew.erase(snew.begin());
sold.insert(y);
}
}
void kiki(int x,int y)///x->y
{
costnew[x]=costnew[par[x]]+dist[x];
d2[x]=d2[par[x]];
if(d2[x]==0)d2[x]=x;
for(auto f:v[x])
if(par[x]!=f.a&&f.a!=y)
{
if(cost[d[f.a]]>costnew[x])
{
d2[x]=d[f.a];
costnew[x]=cost[d[f.a]];
}
}
ib ditmemay= {costnew[x],d2[x]};
add(ditmemay,dist[y]);///upd costnew+dist[y]
ditmemay= {cost[d[y]],d[y]};
add(ditmemay,-dist[y]);///erase d[y]-=dist[y]
cost[d[y]]-=dist[y];
}
void del(int x,int y)/// y->x
{
ib ditmemay{costnew[x]+dist[y],d2[x]};
add(ditmemay,-dist[y]);/// erase costnew-=dist[y]
ditmemay= {cost[d[y]],d[y]};
add(ditmemay,dist[y]);///add d[y]+=dist[y];
cost[d[y]]+=dist[y];
}
void skibidi(int x,int y)///x->y
{
costnew[x]=costnew[par[x]]+dist[x];
d2[x]=d2[par[x]];
if(d2[x]==0)d2[x]=x;
if(cost[d[x]]>costnew[x])
{
d2[x]=d[x];
costnew[x]=cost[d[x]];
}
ib ditmemay= {costnew[x],d2[x]};
add(ditmemay,dist[y]);///upd costnew+dist[y]
ditmemay= {cost[d[y]],d[y]};
add(ditmemay,-dist[y]);///erase d[y]-=dist[y]
cost[d[y]]-=dist[y];
}
void dfs2(int x,int y)
{
ans[x]=s4;
if(b[x]!=0)
{
kiki(x,b[x]);
dfs2(b[x],x);
del(x,b[x]);
}
for(auto f:v[x])
if(f.a!=y&&f.a!=d[x])
{
skibidi(x,f.a);
dfs2(f.a,x);
del(x,f.a);
}
}
void phongbeo()
{
cin>>n>>k;
s9=0;
for(int i=1; i<=n; i++)
cin>>l>>r>>s2,v[l].pb({r,s2}),v[r].pb({l,s2}),s9+=s2;
if(n==k)
{
for(int i=1; i<=n; i++)
cout<<s9,down
exit(0);
}
dfs(1,0);
for(int i=1; i<=n; i++)
sold.insert({cost[i],i});
s4=0;
for(int i=1; i<=k; i++)
{
pair<int,int> x=*sold.rbegin();
snew.insert(x);
s4+=x.first;
sold.erase(prev(sold.end()));
}
dfs2(1,0);
for(int i=1; i<=n; i++)
cout<<ans[i],down
}
Compilation message
Main.cpp:58:1: warning: ISO C++ forbids declaration of 'main' with no type [-Wreturn-type]
58 | main()
| ^~~~
Main.cpp: In function 'int main()':
Main.cpp:13:23: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
13 | #define fin(x) freopen(x".inp","r",stdin)
| ~~~~~~~^~~~~~~~~~~~~~~~~~~
Main.cpp:62:9: note: in expansion of macro 'fin'
62 | fin(task);
| ^~~
Main.cpp:14:23: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
14 | #define fou(x) freopen(x".ans","w",stdout)
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~
Main.cpp:63:9: note: in expansion of macro 'fou'
63 | fou(task);
| ^~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
8784 KB |
Output is correct |
2 |
Incorrect |
2 ms |
8784 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
8784 KB |
Output is correct |
2 |
Incorrect |
2 ms |
8784 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
8784 KB |
Output is correct |
2 |
Incorrect |
2 ms |
8784 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
8784 KB |
Output is correct |
2 |
Incorrect |
2 ms |
8784 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Execution timed out |
1082 ms |
20308 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
8784 KB |
Output is correct |
2 |
Incorrect |
2 ms |
8784 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |