답안 #537778

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
537778 2022-03-15T13:40:45 Z tqbfjotld Paths (RMI21_paths) C++14
31 / 100
302 ms 18128 KB
#include <bits/stdc++.h>
using namespace std;
#define int long long

int chainlengths[100005];
int curans = 0;
int ch[100005];
int ans[100005];

vector<pair<int,int> > adjl[100005];

int n,K;

multiset<int> s1,s2;

void add(int num){
    //printf("add %lld\n",num);
    if (s2.empty() || num>(*--s2.end())){
        s1.insert(num);
        curans += num;
    }
    else{
        s2.insert(num);
    }
    if (s1.size()>K){
        s2.insert(*s1.begin());
        curans -= *s1.begin();
        s1.erase(s1.begin());
    }
}

void rem(int num){
    //printf("rem %lld\n",num);
    auto it = s1.lower_bound(num);
    if (it!=s1.end() && (*it)==num){
        s1.erase(s1.lower_bound(num));
        curans -= num;
    }
    else{
        assert((*s2.lower_bound(num))==num);
        s2.erase(s2.lower_bound(num));
    }
    if (s1.size()<K && s2.size()>0){
        curans += *--s2.end();
        s1.insert(*--s2.end());
        s2.erase(--s2.end());
    }
}

pair<int,int> dfs1(int node, int pa){
    int cch = node;
    int cval = 0;
    for (auto x : adjl[node]){
        if (x.first==pa){
            continue;
        }
        auto res = dfs1(x.first,node);
        res.first += x.second;
        chainlengths[res.second] = res.first;
        if (res.first>cval){
            cch = res.second;
            cval = res.first;
        }
    }
    ch[node] = cch;
    return {cval,cch};
}

void dfs2(int node, int pa){
    int orig = ch[node];
    int l1 = 0;
    int l2 = 0;
    int l1n = node;
    int l2n = node;
    for (auto x : adjl[node]){
        if (chainlengths[ch[x.first]]>=l1){
            l2 = l1;
            l2n = l1n;
            l1 = chainlengths[ch[x.first]];
            l1n = x.first;
        }
        else if (chainlengths[ch[x.first]]>=l2){
            l2 = chainlengths[ch[x.first]];
            l2n = x.first;
        }
    }
    ans[node] = curans;
    assert(s1.size()<=K);
    for (auto x : adjl[node]){
        if (x.first==pa) continue;
        int chid;
        if (x.first==l1n){
            chid = ch[l2n];
        }
        else{
            chid = ch[l1n];
        }
        rem(chainlengths[chid]);
        chainlengths[chid]+=x.second;
        ch[node] = chid;
        add(chainlengths[chid]);
        rem(chainlengths[ch[x.first]]);
        chainlengths[ch[x.first]] -= x.second;
        add(chainlengths[ch[x.first]]);
        dfs2(x.first,node);
        rem(chainlengths[chid]);
        chainlengths[chid]-=x.second;
        add(chainlengths[chid]);
        rem(chainlengths[ch[x.first]]);
        chainlengths[ch[x.first]] += x.second;
        add(chainlengths[ch[x.first]]);
    }
    ch[node] = orig;
}

 main(){
    scanf("%lld%lld",&n,&K);
    for (int x = 0; x<n-1; x++){
        int a,b,c;
        scanf("%lld%lld%lld",&a,&b,&c);
        adjl[a].push_back({b,c});
        adjl[b].push_back({a,c});
    }
    auto res1 = dfs1(1,0);
    for (int x = 0; x<n; x++){
        add(chainlengths[x+1]);
    }
    dfs2(1,0);
    for (int x = 1; x<=n; x++){
        printf("%lld\n",ans[x]);
    }
}

Compilation message

Main.cpp: In function 'void add(long long int)':
Main.cpp:25:18: warning: comparison of integer expressions of different signedness: 'std::multiset<long long int>::size_type' {aka 'long unsigned int'} and 'long long int' [-Wsign-compare]
   25 |     if (s1.size()>K){
      |         ~~~~~~~~~^~
Main.cpp: In function 'void rem(long long int)':
Main.cpp:43:18: warning: comparison of integer expressions of different signedness: 'std::multiset<long long int>::size_type' {aka 'long unsigned int'} and 'long long int' [-Wsign-compare]
   43 |     if (s1.size()<K && s2.size()>0){
      |         ~~~~~~~~~^~
In file included from /usr/include/c++/10/cassert:44,
                 from /usr/include/x86_64-linux-gnu/c++/10/bits/stdc++.h:33,
                 from Main.cpp:1:
Main.cpp: In function 'void dfs2(long long int, long long int)':
Main.cpp:88:21: warning: comparison of integer expressions of different signedness: 'std::multiset<long long int>::size_type' {aka 'long unsigned int'} and 'long long int' [-Wsign-compare]
   88 |     assert(s1.size()<=K);
      |            ~~~~~~~~~^~~
Main.cpp: At global scope:
Main.cpp:116:2: warning: ISO C++ forbids declaration of 'main' with no type [-Wreturn-type]
  116 |  main(){
      |  ^~~~
Main.cpp: In function 'int main()':
Main.cpp:124:10: warning: variable 'res1' set but not used [-Wunused-but-set-variable]
  124 |     auto res1 = dfs1(1,0);
      |          ^~~~
Main.cpp:117:10: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
  117 |     scanf("%lld%lld",&n,&K);
      |     ~~~~~^~~~~~~~~~~~~~~~~~
Main.cpp:120:14: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
  120 |         scanf("%lld%lld%lld",&a,&b,&c);
      |         ~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 2644 KB Output is correct
2 Correct 2 ms 2644 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 2644 KB Output is correct
2 Correct 2 ms 2644 KB Output is correct
3 Correct 2 ms 2660 KB Output is correct
4 Correct 3 ms 2644 KB Output is correct
5 Correct 2 ms 2644 KB Output is correct
6 Correct 3 ms 2656 KB Output is correct
7 Correct 3 ms 2644 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 2644 KB Output is correct
2 Correct 2 ms 2644 KB Output is correct
3 Correct 2 ms 2660 KB Output is correct
4 Correct 3 ms 2644 KB Output is correct
5 Correct 2 ms 2644 KB Output is correct
6 Correct 3 ms 2656 KB Output is correct
7 Correct 3 ms 2644 KB Output is correct
8 Incorrect 3 ms 2772 KB Output isn't correct
9 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 2644 KB Output is correct
2 Correct 2 ms 2644 KB Output is correct
3 Correct 2 ms 2660 KB Output is correct
4 Correct 3 ms 2644 KB Output is correct
5 Correct 2 ms 2644 KB Output is correct
6 Correct 3 ms 2656 KB Output is correct
7 Correct 3 ms 2644 KB Output is correct
8 Incorrect 3 ms 2772 KB Output isn't correct
9 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 283 ms 15964 KB Output is correct
2 Correct 302 ms 18128 KB Output is correct
3 Correct 195 ms 15692 KB Output is correct
4 Correct 276 ms 15796 KB Output is correct
5 Correct 285 ms 16720 KB Output is correct
6 Correct 240 ms 15868 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 2644 KB Output is correct
2 Correct 2 ms 2644 KB Output is correct
3 Correct 2 ms 2660 KB Output is correct
4 Correct 3 ms 2644 KB Output is correct
5 Correct 2 ms 2644 KB Output is correct
6 Correct 3 ms 2656 KB Output is correct
7 Correct 3 ms 2644 KB Output is correct
8 Incorrect 3 ms 2772 KB Output isn't correct
9 Halted 0 ms 0 KB -