Submission #798432

# Submission time Handle Problem Language Result Execution time Memory
798432 2023-07-30T17:22:35 Z HunterXD Roadside Advertisements (NOI17_roadsideadverts) C++17
7 / 100
80 ms 20404 KB
#include <bits/stdc++.h>
using namespace std;

typedef int ll;
typedef vector<ll> vl;
typedef vector<vl> vvl;

#define all(x) x.begin(), x.end()
#define pb push_back

const char nd = '\n';

struct edge {
  ll v, w;
};

vector<vector<edge>> graph;
vl level;
vvl sparse, dist;

void dfs(ll u, ll p) {
  sparse[u][0] = p;
  for (auto v : graph[u]) {
    if (v.v == p) continue;
    dist[v.v][0] = v.w;
    level[v.v] = level[u] + 1;
    dfs(v.v, u);
  }
}

void calc(ll n) {
  for (ll i = 1; i < 30; i++) {
    for (ll j = 1; j <= n; j++) {
      sparse[j][i] = sparse[sparse[j][i - 1]][i - 1];
      dist[j][i] = dist[j][i - 1] + dist[sparse[j][i - 1]][i - 1];
    }
  }
}

pair<ll, ll> up(ll u, ll d) {
  ll i = 0;
  ll suma = 0;

  while (d) {
    if (d & 1) {
      suma += dist[u][i];
      u = sparse[u][i];
    }
    i++;
    d >>= 1;
  }

  return make_pair(u, suma);
}

signed main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  //
  ll n;
  cin >> n;

  graph.assign(n + 1, vector<edge>());
  level.assign(n + 1, 0);
  sparse.assign(n + 1, vl(30, 0));
  dist.assign(n + 1, vl(30, 0));

  ll u, v, w;
  for (ll i = 1; i < n; i++) {
    cin >> u >> v >> w;
    u++, v++;
    graph[u].pb({v, w});
    graph[v].pb({u, w});
  }

  dfs(1, 0);

  ll q;
  cin >> q;

  vl a(5);

  calc(n);

  while (q--) {
    for (auto &v : a) cin >> v, v++;

    ll res = 0;
    pair<ll, ll> temp;
    for (auto v : a) {
      ll ans = 0, lev = -1000000;
      for (auto u : a) {
        if (level[v] - level[u] > 0 && level[u] > lev) {
          temp = up(v, level[v] - level[u]);
          if (temp.first == u) {
            ans = temp.second;
            lev = level[u];
          }
        }
      }
      res += ans;
    }
    cout << res << nd;
  }
}
# Verdict Execution time Memory Grader output
1 Correct 1 ms 324 KB Output is correct
# Verdict Execution time Memory Grader output
1 Incorrect 80 ms 20404 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 70 ms 19064 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 1 ms 324 KB Output is correct
2 Incorrect 80 ms 20404 KB Output isn't correct
3 Halted 0 ms 0 KB -