# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
537539 | jamezzz | Paths (RMI21_paths) | C++17 | 1086 ms | 9036 KiB |
This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <bits/stdc++.h>
using namespace std;
#define sf scanf
#define pf printf
#define fi first
#define se second
#define pb push_back
typedef long long ll;
typedef pair<int,int> ii;
#define maxn 100005
int n,k,x[maxn],y[maxn],c[maxn],p[maxn];
ll d[maxn];
int use[maxn];
vector<ii> AL[maxn];
void dfs(int u){
for(ii pr:AL[u]){
int i=pr.fi,v=pr.se;
if(v==p[u])continue;
p[v]=u;
d[v]=d[u];
if(!use[i])d[v]+=c[i];
dfs(v);
}
}
bool dfs2(int u,int t){
if(u==t)return true;
for(ii pr:AL[u]){
int i=pr.fi,v=pr.se;
if(v==p[u])continue;
if(dfs2(v,t)){
use[i]=1;
return true;
}
}
return false;
}
int main(){
sf("%d%d",&n,&k);
for(int i=0;i<n-1;++i){
sf("%d%d%d",&x[i],&y[i],&c[i]);
AL[x[i]].pb({i,y[i]});
AL[y[i]].pb({i,x[i]});
}
for(int i=1;i<=n;++i){
memset(use,0,sizeof use);
ll ans=0;
for(int j=0;j<k;++j){
p[i]=0;d[i]=0;dfs(i);
int mx=i;
for(int l=1;l<=n;++l){
if(d[l]>d[mx])mx=l;
}
ans+=d[mx];
dfs2(i,mx);
}
pf("%lld\n",ans);
}
}
/*
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 (stderr)
# | 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... |