Submission #102855

#TimeUsernameProblemLanguageResultExecution timeMemory
102855wxh010910Dreaming (IOI13_dreaming)C++17
100 / 100
117 ms13132 KiB
#include <bits/stdc++.h>
#include "dreaming.h"

using namespace std;

int travelTime(int N, int M, int L, int A[], int B[], int T[]) {
  vector<vector<pair<int, int>>> adj(N);
  for (int i = 0; i < M; ++i) {
    adj[A[i]].emplace_back(B[i], T[i]);
    adj[B[i]].emplace_back(A[i], T[i]);
  }
  vector<bool> visit(N);
  vector<int> depth(N);
  vector<int> pr(N);
  vector<int> order;
  function<void(int)> dfs = [&](int x) {
    order.push_back(x);
    visit[x] = true;
    for (auto p : adj[x]) {
      int y = p.first, w = p.second;
      if (y != pr[x]) {
        depth[y] = depth[x] + w;
        pr[y] = x;
        dfs(y);
      }
    }
  };
  vector<int> trees;
  int ans = 0;
  for (int i = 0; i < N; ++i) {
    if (!visit[i]) {
      order.clear();
      depth[i] = 0;
      pr[i] = -1;
      dfs(i);
      int x = i;
      for (auto u : order) {
        if (depth[u] > depth[x]) {
          x = u;
        }
      }
      order.clear();
      depth[x] = 0;
      pr[x] = -1;
      dfs(x);
      int y = x;
      for (auto u : order) {
        if (depth[u] > depth[y]) {
          y = u;
        }
      }
      ans = max(ans, depth[y]);
      int half = depth[y];
      for (int u = y; ~u; u = pr[u]) {
        half = min(half, max(depth[u], depth[y] - depth[u]));
      }
      trees.push_back(half);
    }
  }
  sort(trees.begin(), trees.end(), greater<int>());
  if ((int) trees.size() >= 2) {
    ans = max(ans, trees[0] + trees[1] + L);
  }
  if ((int) trees.size() >= 3) {
    ans = max(ans, trees[1] + trees[2] + L * 2);
  }
  return ans;
}
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...