This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#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];
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]];
}
}
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;
ib ditmemay= {costnew[x],d2[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]];
ditmemay= {cost[d[f.a]],d[f.a]};
}
}
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;
ib ditmemay= {costnew[x],d2[x]};
if(cost[d[x]]>costnew[x])
{
ditmemay= {cost[d[x]],d[x]};
d2[x]=d[x];
costnew[x]=cost[d[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(d[x]!=0)
{
kiki(x,d[x]);
dfs2(x,d[x]);
del(x,d[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);
}
for(int g=1; g<=n; g++)
{
sold.clear();
snew.clear();
dfs(g,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()));
}
cout<<s4,down
}
// for(int i=1; i<=n; i++)
// cout<<ans[i],down
}
Compilation message (stderr)
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);
| ^~~
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |