답안 #961142

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
961142 2024-04-11T14:48:55 Z Pety Parkovi (COCI22_parkovi) C++14
0 / 110
390 ms 33360 KB
#include <bits/stdc++.h>
 
using namespace std;
 
const int N = 1e6+2;
using ll = long long;
const int mod = 1e9 + 7;

ll lim;
ll dist1[200002], dist2[200002], cnt, k, n;
bool viz[200002], ok[200002];
vector<pair<int, int>>G[200002];

void dfs (int nod, int par, ll last) {
  dist1[nod] = 0;
  dist2[nod] = -1;
  for (auto [u, x] : G[nod]) {
    if (u == par)
      continue;
    dfs(u, nod, x);
    if (ok[u])
      dist2[nod] = max(dist2[nod], lim - (x + dist2[u]));
    else 
      dist1[nod] = max(dist1[nod], x +  dist1[nod]);
  }
  if (dist1[nod] <= dist2[nod]) {
    ok[nod] = 1;
    return;
  }
  if (dist1[nod] + last > lim) {
    ok[nod] = 1;
    viz[nod] = 1;
    cnt++;
    return;
  }
  ok[nod] = 0;
  return;
}

bool solve () {
  cnt = 0;
  memset(viz, 0, sizeof(viz));
  dfs(1, 0, 1e17);
  return (cnt <= k);
}

void cb () {
  ll st = 0, dr = 1e17, ans;
  while (st <= dr) {
    lim = (st + dr) / 2; 
    if (solve()) {
      ans = lim;
      dr = lim - 1;
    }
    else
      st = lim +1;
  }
  cout << ans << "\n";
  lim = ans;
}


int main () 
{
  ios_base::sync_with_stdio(false);
  cin.tie(0); cout.tie(0);
  cin >> n >> k;
  for (int i = 1; i < n; i++) {
    int x, y, z;
    cin >> x >> y >> z;
    G[x].push_back({y, z});
    G[y].push_back({x, z});
  }
  cb();
  memset(viz, 0, sizeof(viz));
  cnt = 0;
  dfs(1, 0, 1e17);
  for (int i = 1; i <= n; i++)
    if (viz[i])
      cout << i << " ";
  for (int i = 1; i <= n && cnt < k; i++)
    if (!viz[i])
      cout << i << " ", cnt++;
  
  return 0;
}

Compilation message

Main.cpp: In function 'void dfs(int, int, ll)':
Main.cpp:17:13: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
   17 |   for (auto [u, x] : G[nod]) {
      |             ^
Main.cpp: In function 'void cb()':
Main.cpp:59:7: warning: 'ans' may be used uninitialized in this function [-Wmaybe-uninitialized]
   59 |   lim = ans;
      |   ~~~~^~~~~
# 결과 실행 시간 메모리 Grader output
1 Incorrect 2 ms 6748 KB Output isn't correct
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Incorrect 390 ms 31944 KB Output isn't correct
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Incorrect 382 ms 33360 KB Output isn't correct
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Incorrect 2 ms 6748 KB Output isn't correct
2 Halted 0 ms 0 KB -