Submission #312993

# Submission time Handle Problem Language Result Execution time Memory
312993 2020-10-14T22:37:29 Z VROOM_VARUN Designated Cities (JOI19_designated_cities) C++14
0 / 100
370 ms 23408 KB
/*
ID: varunra2
LANG: C++
TASK: descit
*/

#include <bits/stdc++.h>
using namespace std;

#ifdef DEBUG
#include "lib/debug.h"
#define debug(...) cerr << "[" << #__VA_ARGS__ << "]:", debug_out(__VA_ARGS__)
#define debug_arr(...) \
  cerr << "[" << #__VA_ARGS__ << "]:", debug_arr(__VA_ARGS__)
#pragma GCC diagnostic ignored "-Wsign-compare"
//#pragma GCC diagnostic ignored "-Wunused-parameter"
//#pragma GCC diagnostic ignored "-Wunused-variable"
#else
#define debug(...) 42
#endif

#define EPS 1e-9
#define IN(A, B, C) assert(B <= A && A <= C)
#define INF (int)1e9
#define MEM(a, b) memset(a, (b), sizeof(a))
#define MOD 1000000007
#define MP make_pair
#define PB push_back
#define all(cont) cont.begin(), cont.end()
#define rall(cont) cont.end(), cont.begin()
#define x first
#define y second

const double PI = acos(-1.0);
typedef long long ll;
typedef long double ld;
typedef pair<int, int> PII;
typedef map<int, int> MPII;
typedef multiset<int> MSETI;
typedef set<int> SETI;
typedef set<string> SETS;
typedef vector<int> VI;
typedef vector<PII> VII;
typedef vector<VI> VVI;
typedef vector<string> VS;

#define rep(i, a, b) for (int i = a; i < (b); ++i)
#define trav(a, x) for (auto& a : x)
#define sz(x) (int)(x).size()
typedef pair<int, int> pii;
typedef vector<int> vi;
#pragma GCC diagnostic ignored "-Wsign-compare"
// util functions

int n, q;
vector<vector<pair<int, PII>>> adj;
//.x -> city
//.y.x -> cost to go to city
//.y.y -> cost to come here from city
VI ret;
VI delt;
VI deg;
vector<bool> vis;

void init() {
  adj.resize(n);
  ret.assign(n + 2, 0);
  delt.resize(n);
  vis.assign(n, false);
  deg.assign(n, 0);
}

// gets how much mr. k pays from pocket if he only chooses city 0
int dfs1(int u, int v) {
  int ret = 0;
  for (auto& x : adj[u]) {
    if (x.x == v) continue;
    ret += (x.y.x + dfs1(x.x, u));
  }
  return ret;
}

// how much change each node will have if they are selected instead of city 0
void dfs2(int u, int v) {
  for (auto& x : adj[u]) {
    if (x.x == v) continue;
    delt[x.x] = delt[u] + x.y.y - x.y.x;
    dfs2(x.x, u);
  }
}

void gen1() {
  // here we will calculate ret[1]
  int tot = dfs1(0, -1);
  dfs2(0, -1);
  ret[1] = tot + *min_element(all(delt));
}

PII genPar(int u) {
  for (auto& x : adj[u]) {
    if (vis[x.x]) continue;
    return MP(x.y.y, x.x);
  }
  return MP(-1, -1);
}

void gen2() {
  // here we calculate ret[2] ... ret[# of leafs]
  // ret[(# of leafs + 1) ... n] = 0

  priority_queue<PII, VII, greater<PII>> pq;

  for (int i = 0; i < n; i++) {
    if (deg[i] == 1) {
      pq.push(MP(genPar(i).x, i));
    }
  }

  // we only have leaf nodes

  while (sz(pq) > 2) {
    auto x = pq.top();
    pq.pop();

    int u, cost;

    u = x.y;
    cost = x.x;

    vis[u] = true;
    int par = genPar(u).y;
    deg[par]--;

    if (deg[par] == 1) {
      pq.push(MP(genPar(par).x + cost, par));
    } else {
      ret[sz(pq)] = ret[sz(pq) + 1] + cost;
    }
  }
}

int main() {
// #ifndef ONLINE_JUDGE
  // freopen("descit.in", "r", stdin);
  // freopen("descit.out", "w", stdout);
// #endif
  cin.sync_with_stdio(0);
  cin.tie(0);

  cin >> n;

  init();

  for (int i = 0; i < n - 1; i++) {
    int a, b, c, d;
    cin >> a >> b >> c >> d;
    a--, b--;
    adj[a].PB(MP(b, MP(c, d)));
    adj[b].PB(MP(a, MP(d, c)));
    deg[a]++;
    deg[b]++;
  }

  gen1();
  gen2();

  cin >> q;

  while (q--) {
    int u;
    cin >> u;
    cout << ret[u] << '\n';
  }

  return 0;
}
# Verdict Execution time Memory Grader output
1 Correct 1 ms 384 KB Output is correct
2 Incorrect 0 ms 384 KB Output isn't correct
3 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 1 ms 384 KB Output is correct
2 Incorrect 363 ms 23152 KB Output isn't correct
3 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 1 ms 384 KB Output is correct
2 Incorrect 370 ms 23408 KB Output isn't correct
3 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 1 ms 384 KB Output is correct
2 Incorrect 0 ms 384 KB Output isn't correct
3 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 1 ms 384 KB Output is correct
2 Incorrect 363 ms 23152 KB Output isn't correct
3 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 1 ms 384 KB Output is correct
2 Incorrect 0 ms 384 KB Output isn't correct
3 Halted 0 ms 0 KB -