답안 #574053

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
574053 2022-06-07T16:17:04 Z urosk Paths (RMI21_paths) C++14
0 / 100
600 ms 58960 KB
//https://oj.uz/problem/view/RMI21_paths
#define here cerr<<"===========================================\n"
#include <bits/stdc++.h>
#define ld double
#define ll long long
#define llinf 100000000000000000LL // 10^17
#define pb push_back
#define popb pop_back
#define fi first
#define sc second
#define endl '\n'
#define pll pair<ll,ll>
#define pld pair<ld,ld>
#define sz(a) (ll)(a.size())
#define all(a) a.begin(),a.end()
#define ceri(a,l,r) {for(ll i_ = l;i_<=r;i_++) cerr<<a[i_]<< " ";cerr<<endl;}
#define daj_mi_malo_vremena ios_base::sync_with_stdio(false);cerr.tie(0);cout.tie(0);cin.tie(0);

using namespace std;

#define maxn 1000005
#define lg 32
ll n,k;
vector<pll> g[maxn];
ll ans[maxn];
ll dp[maxn];
ll st[maxn][lg];
ll val[maxn];
ll sum[maxn];
vector<ll> v;
void dfs(ll u,ll par){
    st[u][0] = par;
    dp[u] = u;
    for(pll p : g[u]){
        ll s = p.fi;
        ll w = p.sc;
        if(s==par) continue;
        sum[s] = sum[u] + w;
        dfs(s,u);
        if(sum[dp[u]]<sum[dp[s]] + w) dp[u] = dp[s];
    }
}
bool cmp(ll x,ll y){return val[x]>val[y];}
void reshi(ll rut){
    sum[rut] = 0;
    dfs(rut,rut);
    for(ll j = 1;j<lg;j++) for(ll i = 1;i<=n;i++) st[i][j] = st[st[i][j-1]][j-1];
    for(ll i = 1;i<=n;i++){
        ll e = 0;
        ll cur = i;
        for(ll j = lg-1;j>=0;j--){
            if(dp[st[cur][j]]==i) cur = st[cur][j];
        }
        val[i] = sum[i] - sum[st[cur][0]];
    }
    sort(all(v),cmp);
    for(ll i = 1;i<=k;i++) ans[rut]+=val[v[i-1]];
}
int main(){
	daj_mi_malo_vremena
    cin >> n >> k;
    ll sve = 0;
    for(ll i = 1;i<=n-1;i++){
        ll x,y,w; cin >> x >> y >> w;
        g[x].pb({y,w});
        g[y].pb({x,w});
        sve+=w;
    }
    for(ll i = 1;i<=n;i++) if(sz(g[i])==1) v.pb(i);
    if(sz(v)<=k){
        for(ll i = 1;i<=n;i++) cout<<sve<<endl;
        return 0;
    }
    for(ll i = 1;i<=n;i++) reshi(i);
    for(ll i = 1;i<=n;i++) cout<<ans[i]<<endl;
	return 0;
}

Compilation message

Main.cpp: In function 'void reshi(long long int)':
Main.cpp:49:12: warning: unused variable 'e' [-Wunused-variable]
   49 |         ll e = 0;
      |            ^
# 결과 실행 시간 메모리 Grader output
1 Correct 12 ms 23764 KB Output is correct
2 Incorrect 13 ms 23764 KB Output isn't correct
3 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 12 ms 23764 KB Output is correct
2 Incorrect 13 ms 23764 KB Output isn't correct
3 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 12 ms 23764 KB Output is correct
2 Incorrect 13 ms 23764 KB Output isn't correct
3 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 12 ms 23764 KB Output is correct
2 Incorrect 13 ms 23764 KB Output isn't correct
3 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Execution timed out 1079 ms 58960 KB Time limit exceeded
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 12 ms 23764 KB Output is correct
2 Incorrect 13 ms 23764 KB Output isn't correct
3 Halted 0 ms 0 KB -