답안 #626955

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
626955 2022-08-12T03:01:54 Z abcvuitunggio Paths (RMI21_paths) C++17
0 / 100
161 ms 20112 KB
#include <iostream>
#include <vector>
#include <set>
using namespace std;
int n,k,u,v,w,dp[100001],dp2[100001],dp3[100001],res[100001],x;
vector <pair <int, int> > ke[100001];
multiset <int> s,s2;
void add(int val){
    s.insert(val);
    x+=val;
    if (s.size()>k){
        s2.insert(*s.begin());
        x-=*s.begin();
        s.erase(s.begin());
    }
}
void del(int val){
    if (s2.find(val)!=s2.end()){
        s2.erase(s2.find(val));
        return;
    }
    s.erase(s.find(val));
    x-=val;
    s.insert(*--s2.end());
    x+=*--s2.end();
    s2.erase(--s2.end());
}
void dfs(int u, int p){
    for (auto [v,w]:ke[u])
        if (v!=p){
            dfs(v,u);
            dp[u]=max(dp[u],dp[v]+w);
        }
}
void dfs2(int u, int p){
    int mx=-1,mx2=-1,v1=-1,v2=-1;
    for (auto [v,w]:ke[u])
        if (v!=p){
            if (dp[v]+w>mx){
                mx2=mx;
                v2=v1;
                mx=dp[v]+w;
                v1=v;
            }
            else if (dp[v]+w>mx2){
                mx2=dp[v]+w;
                v2=v;
            }
        }
    dp3[u]=v1;
    for (auto [v,w]:ke[u])
        if (v!=p){
            dp2[v]=max(dp2[u],(v==v1?mx2:mx))+w;
            dfs2(v,u);
            if (v!=v1)
                add(dp[v]+w);
        }
}
void dfs3(int u, int p){
    res[u]=x;
    for (auto [v,w]:ke[u])
        if (v!=p){
            del(dp[v]+w);
            add(dp[v]);
            del(dp2[v]-w);
            add(dp2[v]);
            dfs3(v,u);
            add(dp[v]+w);
            del(dp[v]);
            add(dp2[v]-w);
            del(dp2[v]);
        }
}
int main(){
    cin >> n >> k;
    for (int i=1;i<n;i++){
        cin >> u >> v >> w;
        ke[u].push_back({v,w});
        ke[v].push_back({u,w});
    }
    dfs(1,1);
    add(dp[1]);
    dfs2(1,1);
    dfs3(1,1);
    for (int i=1;i<=n;i++)
        cout << res[i] << '\n';
}

Compilation message

Main.cpp: In function 'void add(int)':
Main.cpp:11:17: warning: comparison of integer expressions of different signedness: 'std::multiset<int>::size_type' {aka 'long unsigned int'} and 'int' [-Wsign-compare]
   11 |     if (s.size()>k){
      |         ~~~~~~~~^~
Main.cpp: In function 'void dfs2(int, int)':
Main.cpp:36:28: warning: variable 'v2' set but not used [-Wunused-but-set-variable]
   36 |     int mx=-1,mx2=-1,v1=-1,v2=-1;
      |                            ^~
# 결과 실행 시간 메모리 Grader output
1 Runtime error 4 ms 5204 KB Execution killed with signal 6
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Runtime error 4 ms 5204 KB Execution killed with signal 6
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Runtime error 4 ms 5204 KB Execution killed with signal 6
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Runtime error 4 ms 5204 KB Execution killed with signal 6
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Runtime error 161 ms 20112 KB Execution killed with signal 6
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Runtime error 4 ms 5204 KB Execution killed with signal 6
2 Halted 0 ms 0 KB -