Submission #961431

# Submission time Handle Problem Language Result Execution time Memory
961431 2024-04-12T05:40:46 Z rxlfd314 Election Campaign (JOI15_election_campaign) C++17
100 / 100
177 ms 34772 KB
#include <bits/stdc++.h>
using namespace std;
using ari2 = array<int, 2>;
using ari3 = array<int, 3>;

#define vt vector
#define size(x) (int((x).size()))
#define all(x) begin(x), end(x)

#define REP(a,b,c,d) for(int a=(b);(d)>0?a<=(c):a>=(c);a+=(d))
#define FOR(a,b,c) REP(a,b,c,1)
#define ROF(a,b,c) REP(a,b,c,-1)

constexpr int mxN = 100005;
int N, M;
vt<int> adj[mxN];

int szs[mxN], hson[mxN], parent[mxN], lift[mxN][17];
void dfs_szs(int i, int p) {
  FOR(j, 1, 16)
    lift[i][j] = lift[lift[i][j-1]][j-1];
  parent[i] = p;
  szs[i] = 1;
  hson[i] = -1;
  for (int j : adj[i])
    if (j != p) {
      lift[j][0] = i;
      dfs_szs(j, i);
      szs[i] += szs[j];
      if (hson[i] < 0 || szs[j] > szs[hson[i]])
        hson[i] = j;
    }
}

int head[mxN], tin[mxN], tout[mxN], timer;
void dfs_hld(int i) {
  tin[i] = timer++;
  if (hson[i] >= 0) {
    head[hson[i]] = head[i];
    dfs_hld(hson[i]);
  }
  for (int j : adj[i])
    if (j != parent[i] && j != hson[i]) {
      head[j] = j;
      dfs_hld(j);
    }
  tout[i] = timer-1;
}

bool ia(int a, int b) {
  return tin[a] <= tin[b] && tin[b] <= tout[a];
}

int LCA(int a, int b) {
  ROF(j, 16, 0)
    if (!ia(lift[a][j], b))
      a = lift[a][j];
  return ia(a, b) ? a : lift[a][0];
}

struct ST {
  int st[mxN<<1];
  void add(int i, int v) {
    for (st[i+=N] += v; i > 1; i >>= 1)
      st[i>>1] = st[i] + st[i^1];
  }
  void pset(int i, int v) {
    for (st[i+=N] = v; i > 1; i >>= 1)
      st[i>>1] = st[i] + st[i^1];
  }
  int query(int l, int r) {
    int ret = 0;
    for (l+=N, r+=N+1; l<r; l>>=1, r>>=1) {
      if (l & 1)
        ret += st[l++];
      if (r & 1)
        ret += st[--r];
    }
    return ret;
  }
} dp, sum_dp;

vt<ari3> paths[mxN];
void dfs(int i) {
  for (int j : adj[i])
    if (j != parent[i])
      dfs(j);
  
  int res = sum_dp.query(tin[i], tin[i]);
  for (auto [a, b, c] : paths[i]) {
    int x = sum_dp.query(tin[i], tin[i]) + c;
    FOR(_, 1, 2) {
      if (a != i) {
        for (; a >= 0 && tin[a] > tin[i]; a = parent[head[a]]) {
          int l = max(tin[head[a]], tin[i]+1);
          int r = tin[a];
          x += sum_dp.query(l, r);
          x -= dp.query(l, r);
        }
      }
      swap(a, b);
    }
    res = max(res, x);
  }
  #ifdef DEBUG
  cout << "dp[" << i+1 << "] = " << res << '\n';
  #endif
  
  dp.pset(tin[i], res);
  if (parent[i] >= 0)
    sum_dp.add(tin[parent[i]], res);
}

signed main() {
#ifndef DEBUG
  ios_base::sync_with_stdio(false);
  cin.tie(nullptr);
#endif
  cin >> N;
  FOR(_, 2, N) {
    int a, b;
    cin >> a >> b, a--, b--;
    adj[a].push_back(b);
    adj[b].push_back(a);
  }
  dfs_szs(0, -1);
  dfs_hld(0);
  
  cin >> M;
  FOR(_, 1, M) {
    int a, b, c;
    cin >> a >> b >> c, a--, b--;
    paths[LCA(a, b)].push_back({a, b, c});
    #ifdef DEBUG
    cout << "LCA(" << a+1 << ", " << b+1 << ") = " << LCA(a, b)+1 << '\n';
    #endif
  }
  
  dfs(0);
  cout << dp.query(0, 0) << '\n';
}
# Verdict Execution time Memory Grader output
1 Correct 2 ms 9564 KB Output is correct
2 Correct 2 ms 9564 KB Output is correct
3 Correct 2 ms 9564 KB Output is correct
4 Correct 2 ms 9568 KB Output is correct
5 Correct 48 ms 20160 KB Output is correct
6 Correct 41 ms 30836 KB Output is correct
7 Correct 72 ms 27096 KB Output is correct
8 Correct 47 ms 20400 KB Output is correct
9 Correct 70 ms 24976 KB Output is correct
10 Correct 43 ms 20316 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 9560 KB Output is correct
2 Correct 2 ms 9564 KB Output is correct
3 Correct 2 ms 9560 KB Output is correct
4 Correct 107 ms 34160 KB Output is correct
5 Correct 111 ms 34160 KB Output is correct
6 Correct 92 ms 34240 KB Output is correct
7 Correct 113 ms 34096 KB Output is correct
8 Correct 130 ms 34100 KB Output is correct
9 Correct 91 ms 34192 KB Output is correct
10 Correct 106 ms 34240 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 9560 KB Output is correct
2 Correct 2 ms 9564 KB Output is correct
3 Correct 2 ms 9560 KB Output is correct
4 Correct 107 ms 34160 KB Output is correct
5 Correct 111 ms 34160 KB Output is correct
6 Correct 92 ms 34240 KB Output is correct
7 Correct 113 ms 34096 KB Output is correct
8 Correct 130 ms 34100 KB Output is correct
9 Correct 91 ms 34192 KB Output is correct
10 Correct 106 ms 34240 KB Output is correct
11 Correct 12 ms 10584 KB Output is correct
12 Correct 109 ms 34420 KB Output is correct
13 Correct 119 ms 34772 KB Output is correct
14 Correct 99 ms 34488 KB Output is correct
15 Correct 110 ms 34488 KB Output is correct
16 Correct 100 ms 34364 KB Output is correct
17 Correct 110 ms 34476 KB Output is correct
18 Correct 108 ms 34388 KB Output is correct
19 Correct 101 ms 34384 KB Output is correct
20 Correct 111 ms 34412 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 127 ms 22896 KB Output is correct
2 Correct 91 ms 34060 KB Output is correct
3 Correct 164 ms 29984 KB Output is correct
4 Correct 80 ms 23332 KB Output is correct
5 Correct 136 ms 29124 KB Output is correct
6 Correct 87 ms 23420 KB Output is correct
7 Correct 161 ms 28916 KB Output is correct
8 Correct 117 ms 23156 KB Output is correct
9 Correct 91 ms 34268 KB Output is correct
10 Correct 162 ms 27524 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 9564 KB Output is correct
2 Correct 2 ms 9564 KB Output is correct
3 Correct 2 ms 9564 KB Output is correct
4 Correct 2 ms 9568 KB Output is correct
5 Correct 48 ms 20160 KB Output is correct
6 Correct 41 ms 30836 KB Output is correct
7 Correct 72 ms 27096 KB Output is correct
8 Correct 47 ms 20400 KB Output is correct
9 Correct 70 ms 24976 KB Output is correct
10 Correct 43 ms 20316 KB Output is correct
11 Correct 3 ms 9564 KB Output is correct
12 Correct 3 ms 9840 KB Output is correct
13 Correct 3 ms 9796 KB Output is correct
14 Correct 2 ms 9564 KB Output is correct
15 Correct 3 ms 9564 KB Output is correct
16 Correct 3 ms 9564 KB Output is correct
17 Correct 3 ms 9564 KB Output is correct
18 Correct 3 ms 9572 KB Output is correct
19 Correct 2 ms 9564 KB Output is correct
20 Correct 3 ms 9828 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 9564 KB Output is correct
2 Correct 2 ms 9564 KB Output is correct
3 Correct 2 ms 9564 KB Output is correct
4 Correct 2 ms 9568 KB Output is correct
5 Correct 48 ms 20160 KB Output is correct
6 Correct 41 ms 30836 KB Output is correct
7 Correct 72 ms 27096 KB Output is correct
8 Correct 47 ms 20400 KB Output is correct
9 Correct 70 ms 24976 KB Output is correct
10 Correct 43 ms 20316 KB Output is correct
11 Correct 2 ms 9560 KB Output is correct
12 Correct 2 ms 9564 KB Output is correct
13 Correct 2 ms 9560 KB Output is correct
14 Correct 107 ms 34160 KB Output is correct
15 Correct 111 ms 34160 KB Output is correct
16 Correct 92 ms 34240 KB Output is correct
17 Correct 113 ms 34096 KB Output is correct
18 Correct 130 ms 34100 KB Output is correct
19 Correct 91 ms 34192 KB Output is correct
20 Correct 106 ms 34240 KB Output is correct
21 Correct 12 ms 10584 KB Output is correct
22 Correct 109 ms 34420 KB Output is correct
23 Correct 119 ms 34772 KB Output is correct
24 Correct 99 ms 34488 KB Output is correct
25 Correct 110 ms 34488 KB Output is correct
26 Correct 100 ms 34364 KB Output is correct
27 Correct 110 ms 34476 KB Output is correct
28 Correct 108 ms 34388 KB Output is correct
29 Correct 101 ms 34384 KB Output is correct
30 Correct 111 ms 34412 KB Output is correct
31 Correct 127 ms 22896 KB Output is correct
32 Correct 91 ms 34060 KB Output is correct
33 Correct 164 ms 29984 KB Output is correct
34 Correct 80 ms 23332 KB Output is correct
35 Correct 136 ms 29124 KB Output is correct
36 Correct 87 ms 23420 KB Output is correct
37 Correct 161 ms 28916 KB Output is correct
38 Correct 117 ms 23156 KB Output is correct
39 Correct 91 ms 34268 KB Output is correct
40 Correct 162 ms 27524 KB Output is correct
41 Correct 3 ms 9564 KB Output is correct
42 Correct 3 ms 9840 KB Output is correct
43 Correct 3 ms 9796 KB Output is correct
44 Correct 2 ms 9564 KB Output is correct
45 Correct 3 ms 9564 KB Output is correct
46 Correct 3 ms 9564 KB Output is correct
47 Correct 3 ms 9564 KB Output is correct
48 Correct 3 ms 9572 KB Output is correct
49 Correct 2 ms 9564 KB Output is correct
50 Correct 3 ms 9828 KB Output is correct
51 Correct 136 ms 23328 KB Output is correct
52 Correct 109 ms 34408 KB Output is correct
53 Correct 167 ms 27912 KB Output is correct
54 Correct 85 ms 23444 KB Output is correct
55 Correct 156 ms 23172 KB Output is correct
56 Correct 113 ms 34388 KB Output is correct
57 Correct 148 ms 28752 KB Output is correct
58 Correct 82 ms 23580 KB Output is correct
59 Correct 110 ms 23380 KB Output is correct
60 Correct 114 ms 34520 KB Output is correct
61 Correct 133 ms 29004 KB Output is correct
62 Correct 84 ms 23480 KB Output is correct
63 Correct 116 ms 23104 KB Output is correct
64 Correct 112 ms 34488 KB Output is correct
65 Correct 177 ms 28916 KB Output is correct
66 Correct 79 ms 23504 KB Output is correct
67 Correct 117 ms 23324 KB Output is correct
68 Correct 112 ms 34384 KB Output is correct
69 Correct 134 ms 27220 KB Output is correct
70 Correct 92 ms 23604 KB Output is correct