Submission #551622

# Submission time Handle Problem Language Result Execution time Memory
551622 2022-04-21T07:57:53 Z Soumya1 Election Campaign (JOI15_election_campaign) C++17
100 / 100
232 ms 31948 KB
#include <bits/stdc++.h>
#ifdef __LOCAL__
#include <debug_local.h>
#endif
using namespace std;
const int mxN = 1e5 + 5;
const int lg = 18;
vector<int> ad[mxN];
vector<tuple<int, int, int>> ll[mxN];
int par[mxN][lg];
int depth[mxN];
int dp[mxN];
int in[mxN], out[mxN];
int timer;
int t[4 * mxN];
int n;
void update(int x, int lx, int rx, int l, int r, int v) {
  if (l > rx || lx > r) return;
  if (l <= lx && r >= rx) {
    t[x] += v;
    return;
  }
  int mx = (lx + rx) >> 1;
  update(x << 1, lx, mx, l, r, v);
  update(x << 1 | 1, mx + 1, rx, l, r, v);
}
void update(int u, int v) {
  update(1, 1, n, in[u], out[u], v);
}
int query(int x, int lx, int rx, int i) {
  if (lx == rx) return t[x];
  int mx = (lx + rx) >> 1;
  if (i <= mx) return t[x] + query(x << 1, lx, mx, i);
  return t[x] + query(x << 1 | 1, mx + 1, rx, i);
}
int query(int u) {
  return query(1, 1, n, in[u]);
}
void dfs(int u, int p) {
  in[u] = ++timer;
  par[u][0] = p;
  depth[u] = depth[p] + 1;
  for (int i = 1; i < lg; i++) par[u][i] = par[par[u][i - 1]][i - 1];
  for (int v : ad[u]) {
    if (v == p) continue;
    dfs(v, u);
  }
  out[u] = timer;
}
int __lca(int u, int v) {
  if (depth[u] > depth[v]) swap(u, v);
  for (int j = lg - 1; j >= 0; j--) {
    if (depth[par[v][j]] >= depth[u]) v = par[v][j];
  }
  if (u == v) return u;
  for (int j = lg - 1; j >= 0; j--) {
    if (par[v][j] != par[u][j]) u = par[u][j], v = par[v][j];
  }
  return par[v][0];
}
void dfs1(int u, int p) {
  dp[u] = 0;
  int s = 0;
  for (int v : ad[u]) {
    if (v == p) continue;
    dfs1(v, u);
    s += dp[v];
  }
  dp[u] = s;
  for (auto [a, b, c] : ll[u]) {
    dp[u] = max(dp[u], s + query(a) + query(b) + c);
  }
  update(u, s - dp[u]);
}
void testCase() {
  cin >> n;
  for (int i = 1; i < n; i++) {
    int u, v;
    cin >> u >> v;
    ad[u].push_back(v);
    ad[v].push_back(u);
  }
  dfs(1, 0);
  int m;
  cin >> m;
  while (m--) {
    int u, v, c;
    cin >> u >> v >> c;
    int lca = __lca(u, v);
    ll[lca].push_back({u, v, c});
  }
  dfs1(1, 0);
  cout << dp[1] << "\n";
}
int main() {
  ios::sync_with_stdio(false);
  cin.tie(nullptr);
  testCase();
  return 0;
}
# Verdict Execution time Memory Grader output
1 Correct 3 ms 4948 KB Output is correct
2 Correct 3 ms 4948 KB Output is correct
3 Correct 3 ms 5028 KB Output is correct
4 Correct 3 ms 5076 KB Output is correct
5 Correct 90 ms 19092 KB Output is correct
6 Correct 50 ms 28236 KB Output is correct
7 Correct 98 ms 25036 KB Output is correct
8 Correct 66 ms 19240 KB Output is correct
9 Correct 88 ms 23144 KB Output is correct
10 Correct 69 ms 19208 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 3 ms 4948 KB Output is correct
2 Correct 3 ms 4948 KB Output is correct
3 Correct 3 ms 5204 KB Output is correct
4 Correct 128 ms 31532 KB Output is correct
5 Correct 117 ms 31588 KB Output is correct
6 Correct 106 ms 31592 KB Output is correct
7 Correct 117 ms 31544 KB Output is correct
8 Correct 119 ms 31592 KB Output is correct
9 Correct 112 ms 31540 KB Output is correct
10 Correct 118 ms 31620 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 3 ms 4948 KB Output is correct
2 Correct 3 ms 4948 KB Output is correct
3 Correct 3 ms 5204 KB Output is correct
4 Correct 128 ms 31532 KB Output is correct
5 Correct 117 ms 31588 KB Output is correct
6 Correct 106 ms 31592 KB Output is correct
7 Correct 117 ms 31544 KB Output is correct
8 Correct 119 ms 31592 KB Output is correct
9 Correct 112 ms 31540 KB Output is correct
10 Correct 118 ms 31620 KB Output is correct
11 Correct 13 ms 5972 KB Output is correct
12 Correct 122 ms 31916 KB Output is correct
13 Correct 124 ms 31820 KB Output is correct
14 Correct 112 ms 31816 KB Output is correct
15 Correct 124 ms 31948 KB Output is correct
16 Correct 119 ms 31872 KB Output is correct
17 Correct 132 ms 31944 KB Output is correct
18 Correct 129 ms 31904 KB Output is correct
19 Correct 116 ms 31816 KB Output is correct
20 Correct 119 ms 31820 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 163 ms 21808 KB Output is correct
2 Correct 113 ms 31640 KB Output is correct
3 Correct 211 ms 27968 KB Output is correct
4 Correct 124 ms 22128 KB Output is correct
5 Correct 189 ms 27296 KB Output is correct
6 Correct 127 ms 22128 KB Output is correct
7 Correct 201 ms 26952 KB Output is correct
8 Correct 157 ms 22140 KB Output is correct
9 Correct 109 ms 31564 KB Output is correct
10 Correct 208 ms 25820 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 3 ms 4948 KB Output is correct
2 Correct 3 ms 4948 KB Output is correct
3 Correct 3 ms 5028 KB Output is correct
4 Correct 3 ms 5076 KB Output is correct
5 Correct 90 ms 19092 KB Output is correct
6 Correct 50 ms 28236 KB Output is correct
7 Correct 98 ms 25036 KB Output is correct
8 Correct 66 ms 19240 KB Output is correct
9 Correct 88 ms 23144 KB Output is correct
10 Correct 69 ms 19208 KB Output is correct
11 Correct 4 ms 5204 KB Output is correct
12 Correct 4 ms 5224 KB Output is correct
13 Correct 4 ms 5168 KB Output is correct
14 Correct 3 ms 5204 KB Output is correct
15 Correct 4 ms 5204 KB Output is correct
16 Correct 3 ms 5204 KB Output is correct
17 Correct 3 ms 5204 KB Output is correct
18 Correct 4 ms 5204 KB Output is correct
19 Correct 4 ms 5164 KB Output is correct
20 Correct 4 ms 5268 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 3 ms 4948 KB Output is correct
2 Correct 3 ms 4948 KB Output is correct
3 Correct 3 ms 5028 KB Output is correct
4 Correct 3 ms 5076 KB Output is correct
5 Correct 90 ms 19092 KB Output is correct
6 Correct 50 ms 28236 KB Output is correct
7 Correct 98 ms 25036 KB Output is correct
8 Correct 66 ms 19240 KB Output is correct
9 Correct 88 ms 23144 KB Output is correct
10 Correct 69 ms 19208 KB Output is correct
11 Correct 3 ms 4948 KB Output is correct
12 Correct 3 ms 4948 KB Output is correct
13 Correct 3 ms 5204 KB Output is correct
14 Correct 128 ms 31532 KB Output is correct
15 Correct 117 ms 31588 KB Output is correct
16 Correct 106 ms 31592 KB Output is correct
17 Correct 117 ms 31544 KB Output is correct
18 Correct 119 ms 31592 KB Output is correct
19 Correct 112 ms 31540 KB Output is correct
20 Correct 118 ms 31620 KB Output is correct
21 Correct 13 ms 5972 KB Output is correct
22 Correct 122 ms 31916 KB Output is correct
23 Correct 124 ms 31820 KB Output is correct
24 Correct 112 ms 31816 KB Output is correct
25 Correct 124 ms 31948 KB Output is correct
26 Correct 119 ms 31872 KB Output is correct
27 Correct 132 ms 31944 KB Output is correct
28 Correct 129 ms 31904 KB Output is correct
29 Correct 116 ms 31816 KB Output is correct
30 Correct 119 ms 31820 KB Output is correct
31 Correct 163 ms 21808 KB Output is correct
32 Correct 113 ms 31640 KB Output is correct
33 Correct 211 ms 27968 KB Output is correct
34 Correct 124 ms 22128 KB Output is correct
35 Correct 189 ms 27296 KB Output is correct
36 Correct 127 ms 22128 KB Output is correct
37 Correct 201 ms 26952 KB Output is correct
38 Correct 157 ms 22140 KB Output is correct
39 Correct 109 ms 31564 KB Output is correct
40 Correct 208 ms 25820 KB Output is correct
41 Correct 4 ms 5204 KB Output is correct
42 Correct 4 ms 5224 KB Output is correct
43 Correct 4 ms 5168 KB Output is correct
44 Correct 3 ms 5204 KB Output is correct
45 Correct 4 ms 5204 KB Output is correct
46 Correct 3 ms 5204 KB Output is correct
47 Correct 3 ms 5204 KB Output is correct
48 Correct 4 ms 5204 KB Output is correct
49 Correct 4 ms 5164 KB Output is correct
50 Correct 4 ms 5268 KB Output is correct
51 Correct 168 ms 22324 KB Output is correct
52 Correct 118 ms 31796 KB Output is correct
53 Correct 232 ms 26392 KB Output is correct
54 Correct 121 ms 22528 KB Output is correct
55 Correct 166 ms 22216 KB Output is correct
56 Correct 118 ms 31880 KB Output is correct
57 Correct 193 ms 27008 KB Output is correct
58 Correct 130 ms 22380 KB Output is correct
59 Correct 164 ms 22432 KB Output is correct
60 Correct 131 ms 31896 KB Output is correct
61 Correct 210 ms 27240 KB Output is correct
62 Correct 129 ms 22124 KB Output is correct
63 Correct 161 ms 21940 KB Output is correct
64 Correct 124 ms 31820 KB Output is correct
65 Correct 212 ms 26968 KB Output is correct
66 Correct 126 ms 22412 KB Output is correct
67 Correct 162 ms 22016 KB Output is correct
68 Correct 136 ms 31820 KB Output is correct
69 Correct 209 ms 25684 KB Output is correct
70 Correct 128 ms 22472 KB Output is correct