Submission #623133

# Submission time Handle Problem Language Result Execution time Memory
623133 2022-08-05T08:48:21 Z MilosMilutinovic Designated Cities (JOI19_designated_cities) C++14
7 / 100
1327 ms 88516 KB
/**
 *    author:  wxhtzdy
 *    created: 05.08.2022 10:18:20
**/
#include <bits/stdc++.h>

using namespace std;

int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);  
  int n;
  cin >> n;
  vector<vector<array<int, 3>>> g(n);
  long long total = 0;
  for (int i = 0; i < n - 1; i++) {
    int x, y, w0, w1;
    cin >> x >> y >> w0 >> w1;
    --x; --y;
    g[x].push_back({y, w1, w0});
    g[y].push_back({x, w0, w1});
    total += w0;
    total += w1;
  }                       
  vector<long long> dp(n);
  vector<int> que(1, 0); 
  vector<bool> vis(n);
  vis[0] = true; 
  for (int b = 0; b < (int) que.size(); b++) {
    int i = que[b];
    for (auto& p : g[i]) {
      int j = p[0];
      if (!vis[j]) {
        dp[0] += p[1];
        que.push_back(j);
        vis[j] = true;
      }
    }
  }
  function<void(int, int)> Dp = [&](int v, int pr) {
    for (auto& p : g[v]) {
      int u = p[0];
      if (u != pr) {
        dp[u] = dp[v] - p[1] + p[2];
        Dp(u, v);
      }
    }
  };
  Dp(0, 0);
  vector<long long> ans(n + 1);
  vector<int> sz(n);
  vector<bool> was(n);
  function<void(int, int)> Dfs = [&](int v, int pr) {
    sz[v] = 1;
    for (auto& p : g[v]) {
      int u = p[0];
      if (!was[u] && u != pr) {
        Dfs(u, v);
        sz[v] += sz[u];
      }
    }
  };
  function<int(int, int, int)> Find = [&](int v, int pr, int n) {
    for (auto& p : g[v]) {
      int u = p[0];
      if (!was[u] && u != pr && sz[u] * 2 >= n) {
        return Find(u, v, n);
      }
    }
    return v;
  };                 
  vector<vector<pair<int, int>>> ch(n);
  vector<int> t;
  vector<int> nxt(n);
  function<void(int, int)> Go0 = [&](int v, int pr) {
    bool has = false;
    for (auto& p : g[v]) {
      int u = p[0];
      int w = p[1];
      if (!was[u] && u != pr) {
        Go0(u, v);         
        ch[v].push_back({p[2], u});
        has = true;
      }
    }
    if (has) {
      t.push_back(v);
    }
  };
  function<void(int)> Solve = [&](int v) {
    Dfs(v, v);
    v = Find(v, v, sz[v]);
    was[v] = true;
    long long cost = dp[v];
    Go0(v, v);
    ans[1] = max(ans[1], cost);
    /*set<pair<int, int>> edges;
    for (int i : t) {
      sort(ch[i].rbegin(), ch[i].rend());
      if (!ch[i].empty()) {
        edges.emplace(ch[i][0]);
        nxt[i] = 1;
      }
    }        
    for (int i = 1; i <= n; i++) {
      ans[i] = max(ans[i], cost);
      if (edges.empty()) {
        break;
      } else {
        auto it = prev(edges.end());
        cost += it->first;
        int u = it->second;
        edges.erase(it);  
        if (nxt[u] < (int) ch[u].size()) {
          edges.emplace(ch[u][nxt[u]]);
          nxt[u] += 1;
        }
      }
    }           
    for (int i : t) {
      ch[i].clear();
      nxt[i] = 0;
    }
    t.clear();*/
    for (auto& p : g[v]) {
      int u = p[0];
      if (!was[u]) {
        Solve(u);
      }
    }
  };
  Solve(0);
  int q;
  cin >> q;
  while (q--) {
    int x;
    cin >> x;
    cout << total - ans[x] << '\n'; 
  }                                     
  return 0;
}

Compilation message

designated_cities.cpp: In lambda function:
designated_cities.cpp:79:11: warning: unused variable 'w' [-Wunused-variable]
   79 |       int w = p[1];
      |           ^
# Verdict Execution time Memory Grader output
1 Incorrect 0 ms 212 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 0 ms 212 KB Output is correct
2 Correct 767 ms 56012 KB Output is correct
3 Correct 1327 ms 81572 KB Output is correct
4 Correct 863 ms 62012 KB Output is correct
5 Correct 392 ms 39232 KB Output is correct
6 Correct 885 ms 69104 KB Output is correct
7 Correct 261 ms 35208 KB Output is correct
8 Correct 1200 ms 88516 KB Output is correct
9 Correct 171 ms 34044 KB Output is correct
# Verdict Execution time Memory Grader output
1 Incorrect 0 ms 212 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 0 ms 212 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 0 ms 212 KB Output is correct
2 Correct 767 ms 56012 KB Output is correct
3 Correct 1327 ms 81572 KB Output is correct
4 Correct 863 ms 62012 KB Output is correct
5 Correct 392 ms 39232 KB Output is correct
6 Correct 885 ms 69104 KB Output is correct
7 Correct 261 ms 35208 KB Output is correct
8 Correct 1200 ms 88516 KB Output is correct
9 Correct 171 ms 34044 KB Output is correct
10 Incorrect 0 ms 212 KB Output isn't correct
11 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 0 ms 212 KB Output isn't correct
2 Halted 0 ms 0 KB -