Submission #118905

# Submission time Handle Problem Language Result Execution time Memory
118905 2019-06-20T02:43:45 Z IOrtroiii Election Campaign (JOI15_election_campaign) C++14
100 / 100
478 ms 30632 KB
#include <bits/stdc++.h>

using namespace std;

const int N = 100100;

struct Path {
   int from, to, cost;
   Path(int from, int to, int cost) : from(from), to(to), cost(cost){}
};

int n;
vector<int> g[N];
int dep[N], par[17][N];
int tin[N], tout[N], tt;
int f[N], sf[N];
vector<Path> ps[N];

void dfs(int u, int p) {
   tin[u] = ++tt;
   for (int i = 1; i < 17; ++i) {
      par[i][u] = par[i - 1][par[i - 1][u]];
   }
   for (int v : g[u]) if (v != p) {
      dep[v] = dep[u] + 1;
      par[0][v] = u;
      dfs(v, u);
   }
   tout[u] = tt;
}

int lca(int u, int v) {
   if (dep[u] > dep[v]) swap(u, v);
   for (int i = 16; i >= 0; --i) {
      if (dep[par[i][v]] >= dep[u]) {
         v = par[i][v];
      }
   }
   if (u == v) return u;
   for (int i = 16; i >= 0; --i) {
      if (par[i][u] != par[i][v]) {
         u = par[i][u];
         v = par[i][v];
      }
   }
   return par[0][u];
}

int t[N << 2];

void add(int v, int l, int r, int L, int R, int val) {
   if (L <= l && r <= R) {
      t[v] += val;
      return;
   }
   int md = (l + r) >> 1;
   if (L <= md) add(v << 1, l, md, L, R, val);
   if (md < R) add(v << 1 | 1, md + 1, r, L, R, val);
}

int get(int v, int l, int r, int p) {
   if (l == r) {
      return t[v];
   }
   int md = (l + r) >> 1;
   int ans = t[v];
   if (p <= md) ans += get(v << 1, l, md, p);
   else ans += get(v << 1 | 1, md + 1, r, p);
   return ans;
}

void solve(int u) {
   for (int v : g[u]) if (v != par[0][u]) {
      solve(v);
      sf[u] += f[v];
   }
   for (int v : g[u]) if (v != par[0][u]) {
      add(1, 1, n, tin[v], tout[v], sf[u] - f[v]);
   }
   f[u] = sf[u];
   for (auto p : ps[u]) {
      int from = p.from, to = p.to, cost = p.cost;
      int val = sf[from] + sf[to] - sf[u] + cost;
      val += get(1, 1, n, tin[from]);
      val += get(1, 1, n, tin[to]);
      f[u] = max(f[u], val);
   }
}

int main() {
   ios_base::sync_with_stdio(false);
   cin >> n;
   for (int i = 1; i < n; ++i) {
      int u, v;
      cin >> u >> v;
      g[u].emplace_back(v);
      g[v].emplace_back(u);
   }
   par[0][1] = 1;
   dfs(1, 1);
   int m;
   cin >> m;
   for (int i = 1; i <= m; ++i) {
      int u, v, w;
      cin >> u >> v >> w;
      int a = lca(u, v);
      ps[a].emplace_back(u, v, w);
   }
   solve(1);
   cout << f[1] << '\n';
}
# Verdict Execution time Memory Grader output
1 Correct 6 ms 5120 KB Output is correct
2 Correct 5 ms 5120 KB Output is correct
3 Correct 6 ms 5120 KB Output is correct
4 Correct 7 ms 5248 KB Output is correct
5 Correct 141 ms 19332 KB Output is correct
6 Correct 67 ms 26744 KB Output is correct
7 Correct 137 ms 24156 KB Output is correct
8 Correct 127 ms 18936 KB Output is correct
9 Correct 151 ms 22736 KB Output is correct
10 Correct 171 ms 19100 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 6 ms 5120 KB Output is correct
2 Correct 6 ms 5248 KB Output is correct
3 Correct 8 ms 5376 KB Output is correct
4 Correct 202 ms 30128 KB Output is correct
5 Correct 198 ms 30328 KB Output is correct
6 Correct 184 ms 30200 KB Output is correct
7 Correct 191 ms 30200 KB Output is correct
8 Correct 194 ms 30232 KB Output is correct
9 Correct 174 ms 30200 KB Output is correct
10 Correct 185 ms 30084 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 6 ms 5120 KB Output is correct
2 Correct 6 ms 5248 KB Output is correct
3 Correct 8 ms 5376 KB Output is correct
4 Correct 202 ms 30128 KB Output is correct
5 Correct 198 ms 30328 KB Output is correct
6 Correct 184 ms 30200 KB Output is correct
7 Correct 191 ms 30200 KB Output is correct
8 Correct 194 ms 30232 KB Output is correct
9 Correct 174 ms 30200 KB Output is correct
10 Correct 185 ms 30084 KB Output is correct
11 Correct 18 ms 6144 KB Output is correct
12 Correct 186 ms 30440 KB Output is correct
13 Correct 198 ms 30456 KB Output is correct
14 Correct 211 ms 30456 KB Output is correct
15 Correct 255 ms 30508 KB Output is correct
16 Correct 212 ms 30456 KB Output is correct
17 Correct 191 ms 30476 KB Output is correct
18 Correct 205 ms 30584 KB Output is correct
19 Correct 187 ms 30432 KB Output is correct
20 Correct 189 ms 30456 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 253 ms 22064 KB Output is correct
2 Correct 188 ms 30272 KB Output is correct
3 Correct 339 ms 27000 KB Output is correct
4 Correct 219 ms 21804 KB Output is correct
5 Correct 352 ms 26616 KB Output is correct
6 Correct 194 ms 21868 KB Output is correct
7 Correct 364 ms 26396 KB Output is correct
8 Correct 284 ms 22136 KB Output is correct
9 Correct 172 ms 30200 KB Output is correct
10 Correct 350 ms 25396 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 6 ms 5120 KB Output is correct
2 Correct 5 ms 5120 KB Output is correct
3 Correct 6 ms 5120 KB Output is correct
4 Correct 7 ms 5248 KB Output is correct
5 Correct 141 ms 19332 KB Output is correct
6 Correct 67 ms 26744 KB Output is correct
7 Correct 137 ms 24156 KB Output is correct
8 Correct 127 ms 18936 KB Output is correct
9 Correct 151 ms 22736 KB Output is correct
10 Correct 171 ms 19100 KB Output is correct
11 Correct 7 ms 5248 KB Output is correct
12 Correct 7 ms 5376 KB Output is correct
13 Correct 7 ms 5376 KB Output is correct
14 Correct 6 ms 5376 KB Output is correct
15 Correct 7 ms 5248 KB Output is correct
16 Correct 7 ms 5376 KB Output is correct
17 Correct 7 ms 5376 KB Output is correct
18 Correct 7 ms 5376 KB Output is correct
19 Correct 7 ms 5376 KB Output is correct
20 Correct 7 ms 5376 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 6 ms 5120 KB Output is correct
2 Correct 5 ms 5120 KB Output is correct
3 Correct 6 ms 5120 KB Output is correct
4 Correct 7 ms 5248 KB Output is correct
5 Correct 141 ms 19332 KB Output is correct
6 Correct 67 ms 26744 KB Output is correct
7 Correct 137 ms 24156 KB Output is correct
8 Correct 127 ms 18936 KB Output is correct
9 Correct 151 ms 22736 KB Output is correct
10 Correct 171 ms 19100 KB Output is correct
11 Correct 6 ms 5120 KB Output is correct
12 Correct 6 ms 5248 KB Output is correct
13 Correct 8 ms 5376 KB Output is correct
14 Correct 202 ms 30128 KB Output is correct
15 Correct 198 ms 30328 KB Output is correct
16 Correct 184 ms 30200 KB Output is correct
17 Correct 191 ms 30200 KB Output is correct
18 Correct 194 ms 30232 KB Output is correct
19 Correct 174 ms 30200 KB Output is correct
20 Correct 185 ms 30084 KB Output is correct
21 Correct 18 ms 6144 KB Output is correct
22 Correct 186 ms 30440 KB Output is correct
23 Correct 198 ms 30456 KB Output is correct
24 Correct 211 ms 30456 KB Output is correct
25 Correct 255 ms 30508 KB Output is correct
26 Correct 212 ms 30456 KB Output is correct
27 Correct 191 ms 30476 KB Output is correct
28 Correct 205 ms 30584 KB Output is correct
29 Correct 187 ms 30432 KB Output is correct
30 Correct 189 ms 30456 KB Output is correct
31 Correct 253 ms 22064 KB Output is correct
32 Correct 188 ms 30272 KB Output is correct
33 Correct 339 ms 27000 KB Output is correct
34 Correct 219 ms 21804 KB Output is correct
35 Correct 352 ms 26616 KB Output is correct
36 Correct 194 ms 21868 KB Output is correct
37 Correct 364 ms 26396 KB Output is correct
38 Correct 284 ms 22136 KB Output is correct
39 Correct 172 ms 30200 KB Output is correct
40 Correct 350 ms 25396 KB Output is correct
41 Correct 7 ms 5248 KB Output is correct
42 Correct 7 ms 5376 KB Output is correct
43 Correct 7 ms 5376 KB Output is correct
44 Correct 6 ms 5376 KB Output is correct
45 Correct 7 ms 5248 KB Output is correct
46 Correct 7 ms 5376 KB Output is correct
47 Correct 7 ms 5376 KB Output is correct
48 Correct 7 ms 5376 KB Output is correct
49 Correct 7 ms 5376 KB Output is correct
50 Correct 7 ms 5376 KB Output is correct
51 Correct 341 ms 22568 KB Output is correct
52 Correct 204 ms 30632 KB Output is correct
53 Correct 437 ms 25712 KB Output is correct
54 Correct 311 ms 22360 KB Output is correct
55 Correct 273 ms 22320 KB Output is correct
56 Correct 211 ms 30452 KB Output is correct
57 Correct 409 ms 26616 KB Output is correct
58 Correct 196 ms 22208 KB Output is correct
59 Correct 361 ms 22572 KB Output is correct
60 Correct 233 ms 30548 KB Output is correct
61 Correct 478 ms 26616 KB Output is correct
62 Correct 210 ms 21968 KB Output is correct
63 Correct 308 ms 22252 KB Output is correct
64 Correct 232 ms 30556 KB Output is correct
65 Correct 358 ms 26284 KB Output is correct
66 Correct 264 ms 22324 KB Output is correct
67 Correct 290 ms 22236 KB Output is correct
68 Correct 187 ms 30328 KB Output is correct
69 Correct 427 ms 25288 KB Output is correct
70 Correct 204 ms 22268 KB Output is correct