Submission #529241

# Submission time Handle Problem Language Result Execution time Memory
529241 2022-02-22T14:19:01 Z AdamGS Election Campaign (JOI15_election_campaign) C++17
100 / 100
192 ms 25604 KB
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
#define rep(a, b) for(int a = 0; a < (b); ++a)
#define st first
#define nd second
#define pb push_back
#define all(a) a.begin(), a.end()
const int LIM=1e5+7;
vector<int>V[LIM];
vector<pair<pair<int,int>,ll>>P[LIM];
int ojciec[LIM], pierwszy[LIM], odleglosc[LIM], rozmiar[LIM], numer[LIM], akt;
ll tr[4*LIM], dp[LIM], N=1; // w wierzcholku trzymam sum(dp[syn])-dp[x]
void DFS(int x, int o) {
  rozmiar[x]=1;
  ojciec[x]=o;
  for(auto i : V[x]) if(i!=o) {
    odleglosc[i]=odleglosc[x]+1;
    DFS(i, x);
    rozmiar[x]+=rozmiar[i];
  }
  rep(i, V[x].size()) if(V[x][0]==o || rozmiar[V[x][i]]>rozmiar[V[x][0]])
    swap(V[x][0], V[x][i]);
}
void DFS2(int x, int o) {
  numer[x]=akt; ++akt;
  if(V[x][0]!=o) pierwszy[V[x][0]]=pierwszy[x];
  for(auto i : V[x]) if(i!=o) DFS2(i, x);
}
void upd(int v, ll x) {
  v+=N;
  while(v) {
    tr[v]+=x;
    v/=2;
  }
}
ll cnt(int l, int r) {
  l+=N; r+=N;
  ll ans=tr[l];
  if(l!=r) ans+=tr[r];
  while(l/2!=r/2) {
    if(l%2==0) ans+=tr[l+1];
    if(r%2==1) ans+=tr[r-1];
    l/=2; r/=2;
  }
  return ans;
}
int lca(int a, int b) {
  while(pierwszy[a]!=pierwszy[b]) {
    if(odleglosc[pierwszy[a]]<odleglosc[pierwszy[b]]) swap(a, b);
    a=ojciec[pierwszy[a]];
  }
  if(odleglosc[a]>odleglosc[b]) swap(a, b);
  return a;
}
ll sciezka(int a, int b) {
  ll ans=0;
  while(pierwszy[a]!=pierwszy[b]) {
    if(odleglosc[pierwszy[a]]<odleglosc[pierwszy[b]]) swap(a, b);
    ans+=cnt(numer[pierwszy[a]], numer[a]);
    a=ojciec[pierwszy[a]];
  }
  if(odleglosc[a]>odleglosc[b]) swap(a, b);
  ans+=cnt(numer[a], numer[b]);
  return ans;
}
void DFS3(int x, int o) {
  for(auto i : V[x]) if(i!=o) {
    DFS3(i, x);
    dp[x]+=dp[i];
  }
  ll ma=0;
  for(auto i : P[x]) ma=max(ma, sciezka(i.st.st, i.st.nd)+i.nd);
  dp[x]=max(dp[x], ma);
  upd(numer[x], -dp[x]);
  if(x) upd(numer[ojciec[x]], dp[x]);
}
int main() {
  ios_base::sync_with_stdio(0); cin.tie(0);
  int n;
  cin >> n;
  while(N<n) N*=2;
  rep(i, n-1) {
    int a, b;
    cin >> a >> b; --a; --b;
    V[a].pb(b);
    V[b].pb(a);
  }
  DFS(0, 0);
  rep(i, n) pierwszy[i]=i;
  DFS2(0, 0);
  int m;
  cin >> m;
  while(m--) {
    int a, b, c;
    cin >> a >> b >> c; --a; --b;
    P[lca(a, b)].pb({{a, b}, c});
  }
  DFS3(0, 0);
  cout << dp[0] << '\n';
}

Compilation message

election_campaign.cpp: In function 'void DFS(int, int)':
election_campaign.cpp:4:36: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
    4 | #define rep(a, b) for(int a = 0; a < (b); ++a)
      |                                    ^
election_campaign.cpp:22:3: note: in expansion of macro 'rep'
   22 |   rep(i, V[x].size()) if(V[x][0]==o || rozmiar[V[x][i]]>rozmiar[V[x][0]])
      |   ^~~
# Verdict Execution time Memory Grader output
1 Correct 3 ms 4940 KB Output is correct
2 Correct 3 ms 5068 KB Output is correct
3 Correct 3 ms 4940 KB Output is correct
4 Correct 3 ms 5016 KB Output is correct
5 Correct 67 ms 13736 KB Output is correct
6 Correct 40 ms 21368 KB Output is correct
7 Correct 63 ms 18620 KB Output is correct
8 Correct 52 ms 14052 KB Output is correct
9 Correct 83 ms 17220 KB Output is correct
10 Correct 48 ms 14024 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 4 ms 5020 KB Output is correct
2 Correct 3 ms 4940 KB Output is correct
3 Correct 3 ms 5196 KB Output is correct
4 Correct 89 ms 25264 KB Output is correct
5 Correct 85 ms 25232 KB Output is correct
6 Correct 85 ms 25472 KB Output is correct
7 Correct 87 ms 25292 KB Output is correct
8 Correct 86 ms 25260 KB Output is correct
9 Correct 101 ms 25316 KB Output is correct
10 Correct 84 ms 25192 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 4 ms 5020 KB Output is correct
2 Correct 3 ms 4940 KB Output is correct
3 Correct 3 ms 5196 KB Output is correct
4 Correct 89 ms 25264 KB Output is correct
5 Correct 85 ms 25232 KB Output is correct
6 Correct 85 ms 25472 KB Output is correct
7 Correct 87 ms 25292 KB Output is correct
8 Correct 86 ms 25260 KB Output is correct
9 Correct 101 ms 25316 KB Output is correct
10 Correct 84 ms 25192 KB Output is correct
11 Correct 11 ms 6092 KB Output is correct
12 Correct 127 ms 25500 KB Output is correct
13 Correct 88 ms 25504 KB Output is correct
14 Correct 88 ms 25568 KB Output is correct
15 Correct 117 ms 25544 KB Output is correct
16 Correct 79 ms 25584 KB Output is correct
17 Correct 92 ms 25516 KB Output is correct
18 Correct 121 ms 25604 KB Output is correct
19 Correct 84 ms 25552 KB Output is correct
20 Correct 106 ms 25504 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 150 ms 16992 KB Output is correct
2 Correct 100 ms 25248 KB Output is correct
3 Correct 160 ms 22180 KB Output is correct
4 Correct 89 ms 17500 KB Output is correct
5 Correct 109 ms 21680 KB Output is correct
6 Correct 116 ms 17328 KB Output is correct
7 Correct 120 ms 21288 KB Output is correct
8 Correct 140 ms 17376 KB Output is correct
9 Correct 77 ms 25344 KB Output is correct
10 Correct 137 ms 20348 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 3 ms 4940 KB Output is correct
2 Correct 3 ms 5068 KB Output is correct
3 Correct 3 ms 4940 KB Output is correct
4 Correct 3 ms 5016 KB Output is correct
5 Correct 67 ms 13736 KB Output is correct
6 Correct 40 ms 21368 KB Output is correct
7 Correct 63 ms 18620 KB Output is correct
8 Correct 52 ms 14052 KB Output is correct
9 Correct 83 ms 17220 KB Output is correct
10 Correct 48 ms 14024 KB Output is correct
11 Correct 3 ms 5068 KB Output is correct
12 Correct 4 ms 5184 KB Output is correct
13 Correct 4 ms 5196 KB Output is correct
14 Correct 3 ms 5068 KB Output is correct
15 Correct 4 ms 5160 KB Output is correct
16 Correct 4 ms 5196 KB Output is correct
17 Correct 3 ms 5068 KB Output is correct
18 Correct 4 ms 5196 KB Output is correct
19 Correct 3 ms 5068 KB Output is correct
20 Correct 4 ms 5176 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 3 ms 4940 KB Output is correct
2 Correct 3 ms 5068 KB Output is correct
3 Correct 3 ms 4940 KB Output is correct
4 Correct 3 ms 5016 KB Output is correct
5 Correct 67 ms 13736 KB Output is correct
6 Correct 40 ms 21368 KB Output is correct
7 Correct 63 ms 18620 KB Output is correct
8 Correct 52 ms 14052 KB Output is correct
9 Correct 83 ms 17220 KB Output is correct
10 Correct 48 ms 14024 KB Output is correct
11 Correct 4 ms 5020 KB Output is correct
12 Correct 3 ms 4940 KB Output is correct
13 Correct 3 ms 5196 KB Output is correct
14 Correct 89 ms 25264 KB Output is correct
15 Correct 85 ms 25232 KB Output is correct
16 Correct 85 ms 25472 KB Output is correct
17 Correct 87 ms 25292 KB Output is correct
18 Correct 86 ms 25260 KB Output is correct
19 Correct 101 ms 25316 KB Output is correct
20 Correct 84 ms 25192 KB Output is correct
21 Correct 11 ms 6092 KB Output is correct
22 Correct 127 ms 25500 KB Output is correct
23 Correct 88 ms 25504 KB Output is correct
24 Correct 88 ms 25568 KB Output is correct
25 Correct 117 ms 25544 KB Output is correct
26 Correct 79 ms 25584 KB Output is correct
27 Correct 92 ms 25516 KB Output is correct
28 Correct 121 ms 25604 KB Output is correct
29 Correct 84 ms 25552 KB Output is correct
30 Correct 106 ms 25504 KB Output is correct
31 Correct 150 ms 16992 KB Output is correct
32 Correct 100 ms 25248 KB Output is correct
33 Correct 160 ms 22180 KB Output is correct
34 Correct 89 ms 17500 KB Output is correct
35 Correct 109 ms 21680 KB Output is correct
36 Correct 116 ms 17328 KB Output is correct
37 Correct 120 ms 21288 KB Output is correct
38 Correct 140 ms 17376 KB Output is correct
39 Correct 77 ms 25344 KB Output is correct
40 Correct 137 ms 20348 KB Output is correct
41 Correct 3 ms 5068 KB Output is correct
42 Correct 4 ms 5184 KB Output is correct
43 Correct 4 ms 5196 KB Output is correct
44 Correct 3 ms 5068 KB Output is correct
45 Correct 4 ms 5160 KB Output is correct
46 Correct 4 ms 5196 KB Output is correct
47 Correct 3 ms 5068 KB Output is correct
48 Correct 4 ms 5196 KB Output is correct
49 Correct 3 ms 5068 KB Output is correct
50 Correct 4 ms 5176 KB Output is correct
51 Correct 192 ms 17636 KB Output is correct
52 Correct 90 ms 25592 KB Output is correct
53 Correct 141 ms 20684 KB Output is correct
54 Correct 129 ms 17596 KB Output is correct
55 Correct 151 ms 17272 KB Output is correct
56 Correct 104 ms 25540 KB Output is correct
57 Correct 120 ms 21540 KB Output is correct
58 Correct 112 ms 17672 KB Output is correct
59 Correct 135 ms 17568 KB Output is correct
60 Correct 89 ms 25508 KB Output is correct
61 Correct 108 ms 21580 KB Output is correct
62 Correct 93 ms 17356 KB Output is correct
63 Correct 178 ms 17104 KB Output is correct
64 Correct 86 ms 25584 KB Output is correct
65 Correct 119 ms 21368 KB Output is correct
66 Correct 101 ms 17528 KB Output is correct
67 Correct 143 ms 17276 KB Output is correct
68 Correct 91 ms 25540 KB Output is correct
69 Correct 124 ms 20368 KB Output is correct
70 Correct 84 ms 17264 KB Output is correct