Submission #565311

# Submission time Handle Problem Language Result Execution time Memory
565311 2022-05-20T16:33:12 Z SSRS Election Campaign (JOI15_election_campaign) C++14
100 / 100
234 ms 25808 KB
#include <bits/stdc++.h>
using namespace std;
struct binary_indexed_tree{
  int N;
  vector<int> BIT;
  binary_indexed_tree(){
  }
  binary_indexed_tree(int N): N(N), BIT(N + 1, 0){
  }
  void add(int i, int x){
    i++;
    while (i <= N){
      BIT[i] += x;
      i += i & -i;
    }
  }
  int sum(int i){
    int ans = 0;
    while (i > 0){
      ans += BIT[i];
      i -= i & -i;
    }
    return ans;
  }
  int sum(int L, int R){
    return sum(R) - sum(L);
  }
};
struct heavy_light_decomposition{
  vector<int> p, sz, in, next;
  binary_indexed_tree BIT;
  heavy_light_decomposition(vector<int> &p, vector<vector<int>> &c): p(p){
    int N = p.size();
    sz = vector<int>(N, 1);
    dfs1(c);
    in = vector<int>(N);
    next = vector<int>(N, 0);
    int t = 0;
    dfs2(c, t);
    BIT = binary_indexed_tree(N);
  }
  void dfs1(vector<vector<int>> &c, int v = 0){
    for (int &w : c[v]){
      dfs1(c, w);
      sz[v] += sz[w];
      if (sz[w] > sz[c[v][0]]){
        swap(w, c[v][0]);
      }
    }
  }
  void dfs2(vector<vector<int>> &c, int &t, int v = 0){
    in[v] = t;
    t++;
    for (int w : c[v]){
      if (w == c[v][0]){
        next[w] = next[v];
      } else {
        next[w] = w;
      }
      dfs2(c, t, w);
    }
  }
  int lca(int u, int v){
    while (true){
      if (in[u] > in[v]){
        swap(u, v);
      }
      if (next[u] == next[v]){
        return u;
      }
      v = p[next[v]];
    }
  }
  void add(int v, int x){
    BIT.add(in[v], x);
  }
  int path_sum(int u, int v){
    int w = lca(u, v);
    int ans = 0;
    while (next[u] != next[w]){
      ans += BIT.sum(in[next[u]], in[u] + 1);
      u = p[next[u]];
    }
    ans += BIT.sum(in[w], in[u] + 1);
    while (next[v] != next[w]){
      ans += BIT.sum(in[next[v]], in[v] + 1);
      v = p[next[v]];
    }
    ans += BIT.sum(in[w] + 1, in[v] + 1);
    return ans;
  }
};
int main(){
  int N;
  cin >> N;
  vector<vector<int>> E(N);
  for (int i = 0; i < N - 1; i++){
    int X, Y;
    cin >> X >> Y;
    X--;
    Y--;
    E[X].push_back(Y);
    E[Y].push_back(X);
  }
  int M;
  cin >> M;
  vector<int> A(M), B(M), C(M);
  for (int i = 0; i < M; i++){
    cin >> A[i] >> B[i] >> C[i];
    A[i]--;
    B[i]--;
  }
  vector<int> p(N, -1);
  vector<vector<int>> c(N);
  queue<int> Q;
  Q.push(0);
  vector<int> bfs;
  while (!Q.empty()){
    int v = Q.front();
    Q.pop();
    bfs.push_back(v);
    for (int w : E[v]){
      if (w != p[v]){
        p[w] = v;
        c[v].push_back(w);
        Q.push(w);
      }
    }
  }
  heavy_light_decomposition T(p, c);
  vector<vector<int>> id(N);
  for (int i = 0; i < M; i++){
    id[T.lca(A[i], B[i])].push_back(i);
  }
  reverse(bfs.begin(), bfs.end());
  vector<int> dp(N, 0);
  vector<int> dpsum(N, 0);
  for (int v : bfs){
    for (int w : c[v]){
      dpsum[v] += dp[w];
    }
    T.add(v, dpsum[v]);
    int mx = dpsum[v];
    for (int w : id[v]){
      mx = max(mx, T.path_sum(A[w], B[w]) + C[w]);
    }
    dp[v] = mx;
    T.add(v, -dp[v]);
  }
  cout << dp[0] << endl;
}
# Verdict Execution time Memory Grader output
1 Correct 0 ms 212 KB Output is correct
2 Correct 0 ms 212 KB Output is correct
3 Correct 1 ms 212 KB Output is correct
4 Correct 1 ms 340 KB Output is correct
5 Correct 118 ms 15736 KB Output is correct
6 Correct 77 ms 20664 KB Output is correct
7 Correct 117 ms 18692 KB Output is correct
8 Correct 124 ms 15140 KB Output is correct
9 Correct 113 ms 18092 KB Output is correct
10 Correct 82 ms 15180 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 212 KB Output is correct
2 Correct 1 ms 212 KB Output is correct
3 Correct 2 ms 468 KB Output is correct
4 Correct 179 ms 22944 KB Output is correct
5 Correct 164 ms 22956 KB Output is correct
6 Correct 161 ms 22972 KB Output is correct
7 Correct 161 ms 22996 KB Output is correct
8 Correct 167 ms 23020 KB Output is correct
9 Correct 170 ms 23056 KB Output is correct
10 Correct 159 ms 23052 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 212 KB Output is correct
2 Correct 1 ms 212 KB Output is correct
3 Correct 2 ms 468 KB Output is correct
4 Correct 179 ms 22944 KB Output is correct
5 Correct 164 ms 22956 KB Output is correct
6 Correct 161 ms 22972 KB Output is correct
7 Correct 161 ms 22996 KB Output is correct
8 Correct 167 ms 23020 KB Output is correct
9 Correct 170 ms 23056 KB Output is correct
10 Correct 159 ms 23052 KB Output is correct
11 Correct 17 ms 880 KB Output is correct
12 Correct 178 ms 23056 KB Output is correct
13 Correct 181 ms 23060 KB Output is correct
14 Correct 165 ms 23064 KB Output is correct
15 Correct 167 ms 23056 KB Output is correct
16 Correct 181 ms 23080 KB Output is correct
17 Correct 163 ms 23048 KB Output is correct
18 Correct 162 ms 22956 KB Output is correct
19 Correct 180 ms 23064 KB Output is correct
20 Correct 175 ms 22980 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 232 ms 17500 KB Output is correct
2 Correct 169 ms 25684 KB Output is correct
3 Correct 185 ms 23188 KB Output is correct
4 Correct 172 ms 19336 KB Output is correct
5 Correct 195 ms 23104 KB Output is correct
6 Correct 168 ms 19404 KB Output is correct
7 Correct 209 ms 22804 KB Output is correct
8 Correct 225 ms 20372 KB Output is correct
9 Correct 160 ms 25684 KB Output is correct
10 Correct 184 ms 22492 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 212 KB Output is correct
2 Correct 0 ms 212 KB Output is correct
3 Correct 1 ms 212 KB Output is correct
4 Correct 1 ms 340 KB Output is correct
5 Correct 118 ms 15736 KB Output is correct
6 Correct 77 ms 20664 KB Output is correct
7 Correct 117 ms 18692 KB Output is correct
8 Correct 124 ms 15140 KB Output is correct
9 Correct 113 ms 18092 KB Output is correct
10 Correct 82 ms 15180 KB Output is correct
11 Correct 2 ms 468 KB Output is correct
12 Correct 2 ms 468 KB Output is correct
13 Correct 2 ms 468 KB Output is correct
14 Correct 2 ms 468 KB Output is correct
15 Correct 2 ms 360 KB Output is correct
16 Correct 2 ms 468 KB Output is correct
17 Correct 2 ms 468 KB Output is correct
18 Correct 2 ms 468 KB Output is correct
19 Correct 2 ms 468 KB Output is correct
20 Correct 2 ms 468 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 212 KB Output is correct
2 Correct 0 ms 212 KB Output is correct
3 Correct 1 ms 212 KB Output is correct
4 Correct 1 ms 340 KB Output is correct
5 Correct 118 ms 15736 KB Output is correct
6 Correct 77 ms 20664 KB Output is correct
7 Correct 117 ms 18692 KB Output is correct
8 Correct 124 ms 15140 KB Output is correct
9 Correct 113 ms 18092 KB Output is correct
10 Correct 82 ms 15180 KB Output is correct
11 Correct 1 ms 212 KB Output is correct
12 Correct 1 ms 212 KB Output is correct
13 Correct 2 ms 468 KB Output is correct
14 Correct 179 ms 22944 KB Output is correct
15 Correct 164 ms 22956 KB Output is correct
16 Correct 161 ms 22972 KB Output is correct
17 Correct 161 ms 22996 KB Output is correct
18 Correct 167 ms 23020 KB Output is correct
19 Correct 170 ms 23056 KB Output is correct
20 Correct 159 ms 23052 KB Output is correct
21 Correct 17 ms 880 KB Output is correct
22 Correct 178 ms 23056 KB Output is correct
23 Correct 181 ms 23060 KB Output is correct
24 Correct 165 ms 23064 KB Output is correct
25 Correct 167 ms 23056 KB Output is correct
26 Correct 181 ms 23080 KB Output is correct
27 Correct 163 ms 23048 KB Output is correct
28 Correct 162 ms 22956 KB Output is correct
29 Correct 180 ms 23064 KB Output is correct
30 Correct 175 ms 22980 KB Output is correct
31 Correct 232 ms 17500 KB Output is correct
32 Correct 169 ms 25684 KB Output is correct
33 Correct 185 ms 23188 KB Output is correct
34 Correct 172 ms 19336 KB Output is correct
35 Correct 195 ms 23104 KB Output is correct
36 Correct 168 ms 19404 KB Output is correct
37 Correct 209 ms 22804 KB Output is correct
38 Correct 225 ms 20372 KB Output is correct
39 Correct 160 ms 25684 KB Output is correct
40 Correct 184 ms 22492 KB Output is correct
41 Correct 2 ms 468 KB Output is correct
42 Correct 2 ms 468 KB Output is correct
43 Correct 2 ms 468 KB Output is correct
44 Correct 2 ms 468 KB Output is correct
45 Correct 2 ms 360 KB Output is correct
46 Correct 2 ms 468 KB Output is correct
47 Correct 2 ms 468 KB Output is correct
48 Correct 2 ms 468 KB Output is correct
49 Correct 2 ms 468 KB Output is correct
50 Correct 2 ms 468 KB Output is correct
51 Correct 216 ms 20688 KB Output is correct
52 Correct 166 ms 25796 KB Output is correct
53 Correct 190 ms 22740 KB Output is correct
54 Correct 169 ms 19540 KB Output is correct
55 Correct 226 ms 20116 KB Output is correct
56 Correct 164 ms 25788 KB Output is correct
57 Correct 197 ms 23308 KB Output is correct
58 Correct 173 ms 19500 KB Output is correct
59 Correct 225 ms 20676 KB Output is correct
60 Correct 168 ms 25808 KB Output is correct
61 Correct 191 ms 23200 KB Output is correct
62 Correct 178 ms 19848 KB Output is correct
63 Correct 225 ms 20332 KB Output is correct
64 Correct 165 ms 25700 KB Output is correct
65 Correct 194 ms 23068 KB Output is correct
66 Correct 171 ms 19520 KB Output is correct
67 Correct 234 ms 20356 KB Output is correct
68 Correct 165 ms 25708 KB Output is correct
69 Correct 190 ms 22672 KB Output is correct
70 Correct 171 ms 19452 KB Output is correct