Submission #361970

# Submission time Handle Problem Language Result Execution time Memory
361970 2021-02-01T12:32:35 Z AlexLuchianov Election Campaign (JOI15_election_campaign) C++14
100 / 100
266 ms 36504 KB
#include <iostream>
#include <vector>
#include <algorithm>
#include <cmath>
#include <cassert>

using ll = long long;
#define MIN(a, b) (((a) < (b)) ? (a) : (b))
#define MAX(a, b) (((a) < (b)) ? (b) : (a))

int const nmax = 100000;
int const lgmax = 20;
std::vector<int> g[1 + nmax];
int far[1 + lgmax][1 + nmax], level[1 + nmax];
int start[1 + nmax], stop[1 + nmax], ptr = 0;

void dfs(int node, int parent) {
  far[0][node] = parent;
  level[node] = level[parent] + 1;
  start[node] = ++ptr;

  for(int h = 0; h < g[node].size(); h++) {
    int to = g[node][h];
    if(to != parent)
      dfs(to, node);
  }
  stop[node] = ptr;
}

int getlca(int x, int y) {
  if(level[x] < level[y])
    std::swap(x, y);
  for(int h = lgmax; 0 <= h; h--)
    if(level[y] + (1 << h) <= level[x])
      x = far[h][x];
  if(x == y)
    return x;
  for(int h = lgmax; 0 <= h; h--)
    if(far[h][x] != far[h][y]) {
      x = far[h][x];
      y = far[h][y];
    }
  return far[0][x];
}

class FenwickTree{
private:
  std::vector<ll> aib;
  int n;
public:
  FenwickTree(int n_) {
    n = n_;
    aib.resize(1 + 4 * n);
  }
  void update(int pos, ll val) {
    for(int x = pos; x <= n; x += (x ^ (x & (x - 1))))
      aib[x] += val;
  }
  ll query(int pos) {
    ll result = 0;
    for(int x = pos; 0 < x; x = (x & (x - 1)))
      result += aib[x];
    return result;
  }
};

ll dp[1 + nmax], sum[1 + nmax];

struct Event{
  int x;
  int y;
  int cost;
};
std::vector<Event> events[1 + nmax];

void solve(int node, int parent, FenwickTree &aib) {
  for(int h = 0; h < g[node].size(); h++) {
    int to = g[node][h];
    if(to != parent) {
      solve(to, node, aib);
      sum[node] += dp[to];
    }
  }
  for(int h = 0; h < events[node].size(); h++) {
    Event e = events[node][h];
    ll result = aib.query(start[e.x]) + aib.query(start[e.y]) + e.cost + sum[node];
    dp[node] = std::max(dp[node], result);
  }
  dp[node] = std::max(dp[node], sum[node]);

  aib.update(start[node], sum[node] - dp[node]);
  aib.update(stop[node] + 1, -(sum[node] - dp[node]));
}

int main() {
  std::ios::sync_with_stdio(0);
  std::cin.tie(0);

  int n;
  std::cin >> n;
  for(int i = 1;i < n; i++) {
    int x, y;
    std::cin >> x >> y;
    g[x].push_back(y);
    g[y].push_back(x);
  }
  dfs(1, 0);
  
  for(int h = 1; h <= lgmax; h++)
    for(int i = 1;i <= n; i++)
      far[h][i] = far[h - 1][far[h - 1][i]];

  int m;
  std::cin >> m;
  for(int i = 1;i <= m; i++) {
    int x, y, cost;
    std::cin >> x >> y >> cost;
    events[getlca(x, y)].push_back({x, y, cost});
  }

  FenwickTree aib(1 + n);
  solve(1, 0, aib);
  std::cout << dp[1];

  return 0;
}

Compilation message

election_campaign.cpp: In function 'void dfs(int, int)':
election_campaign.cpp:22:20: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   22 |   for(int h = 0; h < g[node].size(); h++) {
      |                  ~~^~~~~~~~~~~~~~~~
election_campaign.cpp: In function 'void solve(int, int, FenwickTree&)':
election_campaign.cpp:77:20: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   77 |   for(int h = 0; h < g[node].size(); h++) {
      |                  ~~^~~~~~~~~~~~~~~~
election_campaign.cpp:84:20: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<Event>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   84 |   for(int h = 0; h < events[node].size(); h++) {
      |                  ~~^~~~~~~~~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 4 ms 5100 KB Output is correct
2 Correct 4 ms 5100 KB Output is correct
3 Correct 4 ms 5228 KB Output is correct
4 Correct 4 ms 5356 KB Output is correct
5 Correct 98 ms 23660 KB Output is correct
6 Correct 53 ms 32876 KB Output is correct
7 Correct 96 ms 29548 KB Output is correct
8 Correct 80 ms 23148 KB Output is correct
9 Correct 97 ms 27756 KB Output is correct
10 Correct 82 ms 23148 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 4 ms 5120 KB Output is correct
2 Correct 4 ms 5228 KB Output is correct
3 Correct 4 ms 5484 KB Output is correct
4 Correct 141 ms 36076 KB Output is correct
5 Correct 135 ms 36504 KB Output is correct
6 Correct 112 ms 36076 KB Output is correct
7 Correct 137 ms 36076 KB Output is correct
8 Correct 131 ms 36204 KB Output is correct
9 Correct 117 ms 36076 KB Output is correct
10 Correct 134 ms 36204 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 4 ms 5120 KB Output is correct
2 Correct 4 ms 5228 KB Output is correct
3 Correct 4 ms 5484 KB Output is correct
4 Correct 141 ms 36076 KB Output is correct
5 Correct 135 ms 36504 KB Output is correct
6 Correct 112 ms 36076 KB Output is correct
7 Correct 137 ms 36076 KB Output is correct
8 Correct 131 ms 36204 KB Output is correct
9 Correct 117 ms 36076 KB Output is correct
10 Correct 134 ms 36204 KB Output is correct
11 Correct 17 ms 6252 KB Output is correct
12 Correct 140 ms 36460 KB Output is correct
13 Correct 129 ms 36480 KB Output is correct
14 Correct 127 ms 36460 KB Output is correct
15 Correct 132 ms 36460 KB Output is correct
16 Correct 118 ms 36460 KB Output is correct
17 Correct 133 ms 36332 KB Output is correct
18 Correct 133 ms 36332 KB Output is correct
19 Correct 117 ms 36460 KB Output is correct
20 Correct 132 ms 36332 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 190 ms 26352 KB Output is correct
2 Correct 145 ms 36204 KB Output is correct
3 Correct 266 ms 32492 KB Output is correct
4 Correct 124 ms 26148 KB Output is correct
5 Correct 229 ms 31852 KB Output is correct
6 Correct 121 ms 26208 KB Output is correct
7 Correct 237 ms 31528 KB Output is correct
8 Correct 211 ms 26732 KB Output is correct
9 Correct 114 ms 36076 KB Output is correct
10 Correct 250 ms 30492 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 4 ms 5100 KB Output is correct
2 Correct 4 ms 5100 KB Output is correct
3 Correct 4 ms 5228 KB Output is correct
4 Correct 4 ms 5356 KB Output is correct
5 Correct 98 ms 23660 KB Output is correct
6 Correct 53 ms 32876 KB Output is correct
7 Correct 96 ms 29548 KB Output is correct
8 Correct 80 ms 23148 KB Output is correct
9 Correct 97 ms 27756 KB Output is correct
10 Correct 82 ms 23148 KB Output is correct
11 Correct 5 ms 5356 KB Output is correct
12 Correct 4 ms 5484 KB Output is correct
13 Correct 4 ms 5484 KB Output is correct
14 Correct 5 ms 5356 KB Output is correct
15 Correct 5 ms 5356 KB Output is correct
16 Correct 6 ms 5356 KB Output is correct
17 Correct 6 ms 5356 KB Output is correct
18 Correct 5 ms 5484 KB Output is correct
19 Correct 5 ms 5356 KB Output is correct
20 Correct 5 ms 5484 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 4 ms 5100 KB Output is correct
2 Correct 4 ms 5100 KB Output is correct
3 Correct 4 ms 5228 KB Output is correct
4 Correct 4 ms 5356 KB Output is correct
5 Correct 98 ms 23660 KB Output is correct
6 Correct 53 ms 32876 KB Output is correct
7 Correct 96 ms 29548 KB Output is correct
8 Correct 80 ms 23148 KB Output is correct
9 Correct 97 ms 27756 KB Output is correct
10 Correct 82 ms 23148 KB Output is correct
11 Correct 4 ms 5120 KB Output is correct
12 Correct 4 ms 5228 KB Output is correct
13 Correct 4 ms 5484 KB Output is correct
14 Correct 141 ms 36076 KB Output is correct
15 Correct 135 ms 36504 KB Output is correct
16 Correct 112 ms 36076 KB Output is correct
17 Correct 137 ms 36076 KB Output is correct
18 Correct 131 ms 36204 KB Output is correct
19 Correct 117 ms 36076 KB Output is correct
20 Correct 134 ms 36204 KB Output is correct
21 Correct 17 ms 6252 KB Output is correct
22 Correct 140 ms 36460 KB Output is correct
23 Correct 129 ms 36480 KB Output is correct
24 Correct 127 ms 36460 KB Output is correct
25 Correct 132 ms 36460 KB Output is correct
26 Correct 118 ms 36460 KB Output is correct
27 Correct 133 ms 36332 KB Output is correct
28 Correct 133 ms 36332 KB Output is correct
29 Correct 117 ms 36460 KB Output is correct
30 Correct 132 ms 36332 KB Output is correct
31 Correct 190 ms 26352 KB Output is correct
32 Correct 145 ms 36204 KB Output is correct
33 Correct 266 ms 32492 KB Output is correct
34 Correct 124 ms 26148 KB Output is correct
35 Correct 229 ms 31852 KB Output is correct
36 Correct 121 ms 26208 KB Output is correct
37 Correct 237 ms 31528 KB Output is correct
38 Correct 211 ms 26732 KB Output is correct
39 Correct 114 ms 36076 KB Output is correct
40 Correct 250 ms 30492 KB Output is correct
41 Correct 5 ms 5356 KB Output is correct
42 Correct 4 ms 5484 KB Output is correct
43 Correct 4 ms 5484 KB Output is correct
44 Correct 5 ms 5356 KB Output is correct
45 Correct 5 ms 5356 KB Output is correct
46 Correct 6 ms 5356 KB Output is correct
47 Correct 6 ms 5356 KB Output is correct
48 Correct 5 ms 5484 KB Output is correct
49 Correct 5 ms 5356 KB Output is correct
50 Correct 5 ms 5484 KB Output is correct
51 Correct 176 ms 26860 KB Output is correct
52 Correct 139 ms 36460 KB Output is correct
53 Correct 249 ms 30900 KB Output is correct
54 Correct 130 ms 26280 KB Output is correct
55 Correct 189 ms 26660 KB Output is correct
56 Correct 136 ms 36460 KB Output is correct
57 Correct 208 ms 31596 KB Output is correct
58 Correct 143 ms 26404 KB Output is correct
59 Correct 186 ms 27076 KB Output is correct
60 Correct 127 ms 36332 KB Output is correct
61 Correct 227 ms 31724 KB Output is correct
62 Correct 135 ms 26316 KB Output is correct
63 Correct 187 ms 26516 KB Output is correct
64 Correct 127 ms 36332 KB Output is correct
65 Correct 259 ms 31656 KB Output is correct
66 Correct 128 ms 26280 KB Output is correct
67 Correct 194 ms 26744 KB Output is correct
68 Correct 132 ms 36460 KB Output is correct
69 Correct 204 ms 30316 KB Output is correct
70 Correct 130 ms 26448 KB Output is correct