Submission #1020500

# Submission time Handle Problem Language Result Execution time Memory
1020500 2024-07-12T05:58:14 Z NintsiChkhaidze Paths (RMI21_paths) C++17
100 / 100
335 ms 17620 KB
#include <bits/stdc++.h>
#include <ostream>
#define pb push_back
#define s second
#define f first
#define pb push_back
#define pii pair <int,int>
#define ll long long
#define left h*2,l,(l + r)/2
#define right h*2+1,(l + r)/2 + 1,r
#define int ll
using namespace std;
 
const int N = 1e5 + 5,inf = 1e18;
vector <pii> v[N];
int n,k,val[N],total,ans[N];
multiset <int> s1,s2;

//s1,s2
void upd(int x,int dl){
  if (dl == 1){
    s1.insert(x);
    total += x;
  }else {
    if (s2.count(x)) s2.erase(s2.find(x));
    else if (s1.count(x)) s1.erase(s1.find(x)),total -= x;
    else assert(0); ///fail
  }

  while (s1.size() < k && s2.size()){
    int x = *(--s2.end());
    s1.insert(x);
    total += x;
    s2.erase(--s2.end());
  }

  while (s1.size() > k){
    int x = *s1.begin();
    s2.insert(x);
    s1.erase(s1.begin());
    total -= x;
  }
}

void dfs(int x,int par){

  for (auto [to,w]: v[x]){
    if (to == par) continue;
    dfs(to,x);
    val[x] = max(val[x],val[to] + w);
  }

  bool foundmax=0;
  for (auto [to,w]: v[x]){
    if (to == par) continue;
    if ((!foundmax) && val[to] + w == val[x]){
      foundmax = 1;
    }else{
      upd(val[to] + w,1);
    }
  }
}

void DFS(int x,int par,int mx){
  ans[x] = total;
  int m1=mx,m2=-1;
  for (auto [to,w]: v[x]){
    if (to==par) continue;
    if (val[to] + w >= m1) m2 = m1,m1 = val[to] + w;
    else if (val[to] + w >= m2) m2 = val[to] + w;
  }

  for (auto [to,w]: v[x]){
    if (to == par) continue;
    int m = m1;
    if (val[to] + w == m){
      m = m2;
    }

    if (m >= 0){
      upd(m, -1);
      upd(m + w,+1);
    }
    upd(val[to] + w,-1);
    upd(val[to], +1);

    DFS(to,x,m + w);

    upd(val[to], -1);
    upd(val[to] + w,+1);

    if (m >= 0){
      upd(m + w,-1);
      upd(m, +1);
    }
  }
}

void printans(){
  for (int i=1;i<=n;i++)
    cout<<ans[i]<<endl;
}
signed main() {
  ios_base::sync_with_stdio(0),cin.tie(NULL),cout.tie(NULL);
  
  cin>>n>>k;
  for (int i = 1; i < n; i++){
    int a,b,c;
    cin>>a>>b>>c;
    v[a].pb({b,c});
    v[b].pb({a,c});
  }
  
  dfs(1,1);
  upd(val[1],+1);
  upd(0,+1);
  DFS(1,1,0);
  printans();
}

Compilation message

Main.cpp: In function 'void upd(long long int, long long int)':
Main.cpp:30:20: 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]
   30 |   while (s1.size() < k && s2.size()){
      |          ~~~~~~~~~~^~~
Main.cpp:37:20: 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]
   37 |   while (s1.size() > k){
      |          ~~~~~~~~~~^~~
# Verdict Execution time Memory Grader output
1 Correct 2 ms 2652 KB Output is correct
2 Correct 1 ms 2652 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 2652 KB Output is correct
2 Correct 1 ms 2652 KB Output is correct
3 Correct 2 ms 2652 KB Output is correct
4 Correct 2 ms 2652 KB Output is correct
5 Correct 2 ms 2652 KB Output is correct
6 Correct 2 ms 2652 KB Output is correct
7 Correct 2 ms 2832 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 2652 KB Output is correct
2 Correct 1 ms 2652 KB Output is correct
3 Correct 2 ms 2652 KB Output is correct
4 Correct 2 ms 2652 KB Output is correct
5 Correct 2 ms 2652 KB Output is correct
6 Correct 2 ms 2652 KB Output is correct
7 Correct 2 ms 2832 KB Output is correct
8 Correct 3 ms 2908 KB Output is correct
9 Correct 4 ms 2908 KB Output is correct
10 Correct 3 ms 2908 KB Output is correct
11 Correct 5 ms 2904 KB Output is correct
12 Correct 4 ms 2924 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 2652 KB Output is correct
2 Correct 1 ms 2652 KB Output is correct
3 Correct 2 ms 2652 KB Output is correct
4 Correct 2 ms 2652 KB Output is correct
5 Correct 2 ms 2652 KB Output is correct
6 Correct 2 ms 2652 KB Output is correct
7 Correct 2 ms 2832 KB Output is correct
8 Correct 3 ms 2908 KB Output is correct
9 Correct 4 ms 2908 KB Output is correct
10 Correct 3 ms 2908 KB Output is correct
11 Correct 5 ms 2904 KB Output is correct
12 Correct 4 ms 2924 KB Output is correct
13 Correct 6 ms 2824 KB Output is correct
14 Correct 5 ms 2908 KB Output is correct
15 Correct 5 ms 2908 KB Output is correct
16 Correct 5 ms 2908 KB Output is correct
17 Correct 5 ms 2908 KB Output is correct
18 Correct 5 ms 3000 KB Output is correct
19 Correct 9 ms 3040 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 298 ms 15048 KB Output is correct
2 Correct 268 ms 16932 KB Output is correct
3 Correct 224 ms 12880 KB Output is correct
4 Correct 289 ms 14832 KB Output is correct
5 Correct 265 ms 15972 KB Output is correct
6 Correct 289 ms 15500 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 2652 KB Output is correct
2 Correct 1 ms 2652 KB Output is correct
3 Correct 2 ms 2652 KB Output is correct
4 Correct 2 ms 2652 KB Output is correct
5 Correct 2 ms 2652 KB Output is correct
6 Correct 2 ms 2652 KB Output is correct
7 Correct 2 ms 2832 KB Output is correct
8 Correct 3 ms 2908 KB Output is correct
9 Correct 4 ms 2908 KB Output is correct
10 Correct 3 ms 2908 KB Output is correct
11 Correct 5 ms 2904 KB Output is correct
12 Correct 4 ms 2924 KB Output is correct
13 Correct 6 ms 2824 KB Output is correct
14 Correct 5 ms 2908 KB Output is correct
15 Correct 5 ms 2908 KB Output is correct
16 Correct 5 ms 2908 KB Output is correct
17 Correct 5 ms 2908 KB Output is correct
18 Correct 5 ms 3000 KB Output is correct
19 Correct 9 ms 3040 KB Output is correct
20 Correct 298 ms 15048 KB Output is correct
21 Correct 268 ms 16932 KB Output is correct
22 Correct 224 ms 12880 KB Output is correct
23 Correct 289 ms 14832 KB Output is correct
24 Correct 265 ms 15972 KB Output is correct
25 Correct 289 ms 15500 KB Output is correct
26 Correct 335 ms 15444 KB Output is correct
27 Correct 258 ms 17008 KB Output is correct
28 Correct 256 ms 17620 KB Output is correct
29 Correct 214 ms 13136 KB Output is correct
30 Correct 294 ms 15420 KB Output is correct
31 Correct 330 ms 13904 KB Output is correct
32 Correct 259 ms 16212 KB Output is correct
33 Correct 266 ms 15440 KB Output is correct
34 Correct 252 ms 12884 KB Output is correct
35 Correct 269 ms 15308 KB Output is correct
36 Correct 256 ms 17472 KB Output is correct