Submission #330855

# Submission time Handle Problem Language Result Execution time Memory
330855 2020-11-26T18:44:27 Z VROOM_VARUN Election Campaign (JOI15_election_campaign) C++14
100 / 100
335 ms 46700 KB
/*
ID: varunra2
LANG: C++
TASK: election
*/

#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

struct LCA {
  VVI adj;
  VVI st;
  int n;
  VI tin;
  VI tout;
  int t = 0;
  const int bits = 20;

  void dfs(int u, int v) {
    st[u][0] = v;
    for (int i = 1; i < bits; i++) {
      int to = st[u][i - 1];
      if (to == -1)
        st[u][i] = -1;
      else
        st[u][i] = st[to][i - 1];
    }
    tin[u] = t++;
    for (auto& x : adj[u]) {
      if (x == v) continue;
      dfs(x, u);
    }
    tout[u] = t++;
  }

  void init(VVI& _adj) {
    adj = _adj;
    n = sz(adj);
    tin.resize(n);
    tout.resize(n);
    st.resize(n);
    trav(x, st) x.resize(bits);
    dfs(0, -1);
  }

  bool isAnc(int u, int v) {
    if (u == -1) return true;
    if (v == -1) return false;
    return tin[u] <= tin[v] and tout[u] >= tout[v];
  }

  int getLCA(int u, int v) {
    if (isAnc(u, v)) return u;
    if (isAnc(v, u)) return v;

    for (int i = bits - 1; ~i; i--) {
      if (!isAnc(st[u][i], v)) u = st[u][i];
    }

    return st[u][0];
  }
} lca;

struct BIT {
  // range update, point query
  VI fen;
  int n;

  void init(int _n) {
    n = _n;
    fen.resize(n + 1);
  }

  void upd(int ind, int val) {
    ind++;
    while (ind <= n) {
      fen[ind] += val;
      ind += (ind & -ind);
    }
  }

  void upd(int l, int r, int val) {
    upd(l, val);
    upd(r + 1, -val);
  }

  int qry(int ind) {
    int ret = 0;
    ind++;
    while (ind >= 1) {
      ret += fen[ind];
      ind -= (ind & -ind);
    }
    return ret;
  }
};

VVI adj;
VVI events;
VI ord;
VVI qrys;
VI pos;
VI dp;
VI par;
VI lran, rran;
int n, m;

void init() {
  events.resize(n);
  qrys.resize(m);
  pos.resize(n);
  lran.resize(n);
  rran.resize(n);
  par.resize(n);
  dp.resize(n);
}

void dfsord(int u, int v) {
  pos[u] = sz(ord);
  lran[u] = sz(ord);
  par[u] = v;
  ord.PB(u);
  trav(x, adj[u]) {
    if (x == v) continue;
    dfsord(x, u);
  }
  rran[u] = sz(ord) - 1;
}

BIT chldval, dpval;

void dpdfs(int u, int v) {
  int chldsum = 0;
  trav(x, adj[u]) {
    if (x == v) continue;
    dpdfs(x, u);
    chldsum += dp[x];
  }

  dp[u] = chldsum;

  trav(x, events[u]) {
    int uu, vv, c;
    uu = qrys[x][0], vv = qrys[x][1], c = qrys[x][2];
    int sum = 0;
    c += chldval.qry(pos[uu]);
    c += chldval.qry(pos[vv]);
    c -= dpval.qry(pos[uu]);
    c -= dpval.qry(pos[vv]);
    c += chldsum;
    dp[u] = max(dp[u], c);
  }

  chldval.upd(lran[u], rran[u], chldsum);
  dpval.upd(lran[u], rran[u], dp[u]);
}

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

  cin >> n;

  adj.resize(n);

  for (int i = 0; i < n - 1; i++) {
    int u, v;
    cin >> u >> v;
    u--, v--;
    adj[u].PB(v);
    adj[v].PB(u);
  }

  cin >> m;

  init();

  lca.init(adj);

  for (int i = 0; i < m; i++) {
    qrys[i].resize(3);
    cin >> qrys[i][0] >> qrys[i][1] >> qrys[i][2];
    qrys[i][0]--, qrys[i][1]--;
    int lc = lca.getLCA(qrys[i][0], qrys[i][1]);
    events[lc].PB(i);
  }

  dfsord(0, -1);

  chldval.init(n);

  dpval.init(n);

  dpdfs(0, -1);

  cout << dp[0] << '\n';

  return 0;
}

Compilation message

election_campaign.cpp: In function 'void dpdfs(int, int)':
election_campaign.cpp:190:9: warning: unused variable 'sum' [-Wunused-variable]
  190 |     int sum = 0;
      |         ^~~
# Verdict Execution time Memory Grader output
1 Correct 1 ms 364 KB Output is correct
2 Correct 1 ms 364 KB Output is correct
3 Correct 1 ms 364 KB Output is correct
4 Correct 1 ms 620 KB Output is correct
5 Correct 167 ms 30820 KB Output is correct
6 Correct 85 ms 38372 KB Output is correct
7 Correct 231 ms 35684 KB Output is correct
8 Correct 124 ms 31468 KB Output is correct
9 Correct 193 ms 34148 KB Output is correct
10 Correct 125 ms 31460 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 364 KB Output is correct
2 Correct 1 ms 364 KB Output is correct
3 Correct 2 ms 748 KB Output is correct
4 Correct 162 ms 46180 KB Output is correct
5 Correct 159 ms 46188 KB Output is correct
6 Correct 159 ms 46460 KB Output is correct
7 Correct 167 ms 46400 KB Output is correct
8 Correct 154 ms 46184 KB Output is correct
9 Correct 159 ms 46316 KB Output is correct
10 Correct 168 ms 46180 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 364 KB Output is correct
2 Correct 1 ms 364 KB Output is correct
3 Correct 2 ms 748 KB Output is correct
4 Correct 162 ms 46180 KB Output is correct
5 Correct 159 ms 46188 KB Output is correct
6 Correct 159 ms 46460 KB Output is correct
7 Correct 167 ms 46400 KB Output is correct
8 Correct 154 ms 46184 KB Output is correct
9 Correct 159 ms 46316 KB Output is correct
10 Correct 168 ms 46180 KB Output is correct
11 Correct 15 ms 2540 KB Output is correct
12 Correct 170 ms 46436 KB Output is correct
13 Correct 159 ms 46436 KB Output is correct
14 Correct 159 ms 46700 KB Output is correct
15 Correct 172 ms 46596 KB Output is correct
16 Correct 161 ms 46600 KB Output is correct
17 Correct 167 ms 46692 KB Output is correct
18 Correct 161 ms 46436 KB Output is correct
19 Correct 160 ms 46596 KB Output is correct
20 Correct 168 ms 46436 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 256 ms 38116 KB Output is correct
2 Correct 159 ms 46312 KB Output is correct
3 Correct 335 ms 43116 KB Output is correct
4 Correct 214 ms 38756 KB Output is correct
5 Correct 301 ms 42852 KB Output is correct
6 Correct 196 ms 38756 KB Output is correct
7 Correct 329 ms 42476 KB Output is correct
8 Correct 267 ms 38512 KB Output is correct
9 Correct 152 ms 46316 KB Output is correct
10 Correct 326 ms 41828 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 364 KB Output is correct
2 Correct 1 ms 364 KB Output is correct
3 Correct 1 ms 364 KB Output is correct
4 Correct 1 ms 620 KB Output is correct
5 Correct 167 ms 30820 KB Output is correct
6 Correct 85 ms 38372 KB Output is correct
7 Correct 231 ms 35684 KB Output is correct
8 Correct 124 ms 31468 KB Output is correct
9 Correct 193 ms 34148 KB Output is correct
10 Correct 125 ms 31460 KB Output is correct
11 Correct 2 ms 748 KB Output is correct
12 Correct 2 ms 748 KB Output is correct
13 Correct 2 ms 748 KB Output is correct
14 Correct 2 ms 748 KB Output is correct
15 Correct 2 ms 748 KB Output is correct
16 Correct 2 ms 748 KB Output is correct
17 Correct 2 ms 748 KB Output is correct
18 Correct 2 ms 748 KB Output is correct
19 Correct 2 ms 748 KB Output is correct
20 Correct 2 ms 748 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 364 KB Output is correct
2 Correct 1 ms 364 KB Output is correct
3 Correct 1 ms 364 KB Output is correct
4 Correct 1 ms 620 KB Output is correct
5 Correct 167 ms 30820 KB Output is correct
6 Correct 85 ms 38372 KB Output is correct
7 Correct 231 ms 35684 KB Output is correct
8 Correct 124 ms 31468 KB Output is correct
9 Correct 193 ms 34148 KB Output is correct
10 Correct 125 ms 31460 KB Output is correct
11 Correct 1 ms 364 KB Output is correct
12 Correct 1 ms 364 KB Output is correct
13 Correct 2 ms 748 KB Output is correct
14 Correct 162 ms 46180 KB Output is correct
15 Correct 159 ms 46188 KB Output is correct
16 Correct 159 ms 46460 KB Output is correct
17 Correct 167 ms 46400 KB Output is correct
18 Correct 154 ms 46184 KB Output is correct
19 Correct 159 ms 46316 KB Output is correct
20 Correct 168 ms 46180 KB Output is correct
21 Correct 15 ms 2540 KB Output is correct
22 Correct 170 ms 46436 KB Output is correct
23 Correct 159 ms 46436 KB Output is correct
24 Correct 159 ms 46700 KB Output is correct
25 Correct 172 ms 46596 KB Output is correct
26 Correct 161 ms 46600 KB Output is correct
27 Correct 167 ms 46692 KB Output is correct
28 Correct 161 ms 46436 KB Output is correct
29 Correct 160 ms 46596 KB Output is correct
30 Correct 168 ms 46436 KB Output is correct
31 Correct 256 ms 38116 KB Output is correct
32 Correct 159 ms 46312 KB Output is correct
33 Correct 335 ms 43116 KB Output is correct
34 Correct 214 ms 38756 KB Output is correct
35 Correct 301 ms 42852 KB Output is correct
36 Correct 196 ms 38756 KB Output is correct
37 Correct 329 ms 42476 KB Output is correct
38 Correct 267 ms 38512 KB Output is correct
39 Correct 152 ms 46316 KB Output is correct
40 Correct 326 ms 41828 KB Output is correct
41 Correct 2 ms 748 KB Output is correct
42 Correct 2 ms 748 KB Output is correct
43 Correct 2 ms 748 KB Output is correct
44 Correct 2 ms 748 KB Output is correct
45 Correct 2 ms 748 KB Output is correct
46 Correct 2 ms 748 KB Output is correct
47 Correct 2 ms 748 KB Output is correct
48 Correct 2 ms 748 KB Output is correct
49 Correct 2 ms 748 KB Output is correct
50 Correct 2 ms 748 KB Output is correct
51 Correct 271 ms 38752 KB Output is correct
52 Correct 162 ms 46444 KB Output is correct
53 Correct 324 ms 42048 KB Output is correct
54 Correct 189 ms 39012 KB Output is correct
55 Correct 249 ms 38372 KB Output is correct
56 Correct 157 ms 46440 KB Output is correct
57 Correct 302 ms 42724 KB Output is correct
58 Correct 201 ms 39140 KB Output is correct
59 Correct 256 ms 38888 KB Output is correct
60 Correct 160 ms 46436 KB Output is correct
61 Correct 323 ms 42852 KB Output is correct
62 Correct 188 ms 39008 KB Output is correct
63 Correct 238 ms 38372 KB Output is correct
64 Correct 158 ms 46440 KB Output is correct
65 Correct 322 ms 42600 KB Output is correct
66 Correct 193 ms 39144 KB Output is correct
67 Correct 243 ms 38372 KB Output is correct
68 Correct 157 ms 46436 KB Output is correct
69 Correct 297 ms 41572 KB Output is correct
70 Correct 199 ms 39012 KB Output is correct