답안 #1063326

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
1063326 2024-08-17T16:54:02 Z avighna Roadside Advertisements (NOI17_roadsideadverts) C++17
7 / 100
1000 ms 14776 KB
#include <bits/stdc++.h>

typedef long long ll;

struct Edge {
  ll v, w, i;
};

int main() {
  std::ios_base::sync_with_stdio(false);
  std::cin.tie(nullptr);

  ll n;
  std::cin >> n;
  std::vector<std::vector<Edge>> adj(n);
  std::vector<ll> weights(n - 1);
  for (ll i = 0, u, v, w; i < n - 1; ++i) {
    std::cin >> u >> v >> w;
    adj[u].push_back({v, w, i});
    adj[v].push_back({u, w, i});
    weights[i] = w;
  }

  ll q;
  std::cin >> q;
  while (q--) {
    std::vector<ll> nodes(5);
    for (auto &i : nodes) {
      std::cin >> i;
    }
    std::set<ll> st;
    for (ll i = 0; i < 5; ++i) {
      std::vector<ll> depth(n);
      std::vector<std::pair<ll, ll>> up(n);
      std::function<void(ll, ll)> dfs;
      dfs = [&](ll node, ll par) {
        for (auto &[i, w, idx] : adj[node]) {
          if (i == par) {
            continue;
          }
          depth[i] = depth[node] + 1;
          up[i] = {node, idx};
          dfs(i, node);
        }
      };
      dfs(nodes[i], -1);
      for (ll j = i + 1; j < 5; ++j) {
        ll node = nodes[j];
        while (node != nodes[i]) {
          st.insert(up[node].second);
          node = up[node].first;
        }
      }
    }
    ll ans = 0;
    for (auto &i : st) {
      ans += weights[i];
    }
    std::cout << ans << '\n';
  }
}
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 344 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Execution timed out 1060 ms 14776 KB Time limit exceeded
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 873 ms 7108 KB Output is correct
2 Execution timed out 1034 ms 13312 KB Time limit exceeded
3 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 344 KB Output is correct
2 Execution timed out 1060 ms 14776 KB Time limit exceeded
3 Halted 0 ms 0 KB -