답안 #871279

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
871279 2023-11-10T11:43:00 Z Ahmed57 Paths (RMI21_paths) C++17
8 / 100
230 ms 22008 KB
#include <bits/stdc++.h>
using namespace std;
pair<long long,long long> vl[100001];
long long ans[100001];set<int> leafs;
vector<pair<int,long long>> adj[100001];
void dfs(int i,int pr,int la){
    if(adj[i].size()==1)leafs.insert(i);
    if(adj[i].size()==1&&i!=1){
        ans[i] = la;vl[i] = {la,i};
        return ;
    }
    for(auto j:adj[i]){
        if(j.first==pr)continue;
        dfs(j.first,i,j.second);
        vl[i] = max(vl[i],vl[j.first]);
    }
    ans[vl[i].second]+=la;
    vl[i].first+=la;
}multiset<long long> used,unused;
long long all = 0;int n,k;
void add(long long x){
    all+=x;
    used.insert(x);
    if(used.size()>k){
        all-=*used.begin();
        unused.insert(*used.begin());
        used.erase(used.begin());
    }
}
void rem(long long x){
    if(x<(*used.begin())){
        unused.erase(unused.lower_bound(x));
    }else{
        all-=x;
        used.erase(used.lower_bound(x));
        while(used.size()<k&&unused.size()>0){
            auto it = unused.end();it--;
            used.insert(*it);
            all+=(*it);
            unused.erase(it);
        }
    }
}
long long v[100001];
pair<long long,long long> pref[100001],suf[100001];
void reroot(int i,int pr,pair<long long,long long> pa){
    v[i] = all;
    if(adj[i].size()==1&&i!=1)return;
    if(n==1&&i==1)return;
    if(i==1&&adj[i].size()==1){
        pa = {0,i};
    }
    pair<long long,long long> ma = pa;
    for(auto j:adj[i]){
        if(j.first==pr)continue;
        pref[j.first] = ma;
        ma = max(ma,make_pair(vl[j.first].first,vl[j.first].second));
    }
    ma = pa;
    reverse(adj[i].begin(),adj[i].end());
    for(auto j:adj[i]){
        if(j.first==pr)continue;
        suf[j.first] = ma;
        ma = max(ma,make_pair(vl[j.first].first,vl[j.first].second));
    }
    reverse(adj[i].begin(),adj[i].end());
    for(auto j:adj[i]){
        if(j.first==pr)continue;
        rem(ans[vl[j.first].second]);
        ans[vl[j.first].second]-=j.second;
        add(ans[vl[j.first].second]);
        pair<int,int> xd = max(pref[j.first],suf[j.first]);
        rem(ans[xd.second]);
        ans[xd.second]+=j.second;
        add(ans[xd.second]);
        xd.first+=j.second;
        reroot(j.first,i,xd);
        rem(ans[xd.second]);
        ans[xd.second]-=j.second;
        add(ans[xd.second]);
        rem(ans[vl[j.first].second]);
        ans[vl[j.first].second]+=j.second;
        add(ans[vl[j.first].second]);
    }
}
int main(){
    cin>>n>>k;
    for(int i =0;i<n-1;i++){
        int a,b,c;cin>>a>>b>>c;
        adj[a].push_back({b,c});
        adj[b].push_back({a,c});
    }
    dfs(1,0,0);
    for(auto i:leafs){
        add(ans[i]);
    }
    reroot(1,0,{0,0});
    for(int i = 1;i<=n;i++){
        cout<<v[i]<<"\n";
    }
}
/*
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 'void add(long long int)':
Main.cpp:24:19: warning: comparison of integer expressions of different signedness: 'std::multiset<long long int>::size_type' {aka 'long unsigned int'} and 'int' [-Wsign-compare]
   24 |     if(used.size()>k){
      |        ~~~~~~~~~~~^~
Main.cpp: In function 'void rem(long long int)':
Main.cpp:36:26: warning: comparison of integer expressions of different signedness: 'std::multiset<long long int>::size_type' {aka 'long unsigned int'} and 'int' [-Wsign-compare]
   36 |         while(used.size()<k&&unused.size()>0){
      |               ~~~~~~~~~~~^~
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 8540 KB Output is correct
2 Correct 1 ms 8540 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 8540 KB Output is correct
2 Correct 1 ms 8540 KB Output is correct
3 Incorrect 2 ms 8540 KB Output isn't correct
4 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 8540 KB Output is correct
2 Correct 1 ms 8540 KB Output is correct
3 Incorrect 2 ms 8540 KB Output isn't correct
4 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 8540 KB Output is correct
2 Correct 1 ms 8540 KB Output is correct
3 Incorrect 2 ms 8540 KB Output isn't correct
4 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Incorrect 230 ms 22008 KB Output isn't correct
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 8540 KB Output is correct
2 Correct 1 ms 8540 KB Output is correct
3 Incorrect 2 ms 8540 KB Output isn't correct
4 Halted 0 ms 0 KB -