# | Submission time | Handle | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
318634 | 2020-11-02T14:32:56 Z | CaroLinda | Election Campaign (JOI15_election_campaign) | C++14 | 254 ms | 30648 KB |
#include <bits/stdc++.h> #define sz(x) (int)(x.size() ) #define ll long long #define all(x) x.begin(),x.end() const int MAXN = 1e5+10 ; const int LOG = 20 ; using namespace std ; int n , m ; int tin[MAXN] , tout[MAXN] , lvl[MAXN] ; int dp[LOG][MAXN] ; int dpPlan[MAXN] ; vector<int> adj[MAXN] ; vector<tuple<int,int,int> > myPlans[MAXN] ; struct Bit { int bit[MAXN] ; void upd(int pos, int val ) { for(int i = pos ;i < MAXN ; i += i&-i ) bit[i] += val ; } int qry(int pos ) { int tot = 0 ; for(int i = pos ; i > 0 ; i -= i&-i ) tot += bit[i] ; return tot ; } } bit ; int curTime ; void dfs(int x) { tin[x] = tout[x] = ++curTime ; for(auto e : adj[x] ) { if(e == dp[0][x]) continue ; lvl[e] = lvl[x]+1 ; dp[0][e] = x ; dfs(e) ; tout[x] = tout[e] ; } } int getLca(int x, int y ) { if(lvl[x] < lvl[y] ) swap(x,y) ; for(int i = LOG-1 ; i >= 0 ; i-- ) if( lvl[x] - (1<<i ) >= lvl[y] ) x = dp[i][x] ; if(x == y ) return x ; for(int i = LOG-1 ; i >= 0 ; i-- ) if( dp[i][x] != -1 && dp[i][y] != dp[i][x] ) { x = dp[i][x] ; y = dp[i][y] ; } return dp[0][x] ; } void calcDp(int x ) { int soma = 0 ; for(auto e : adj[x] ) if(e != dp[0][x] ) { calcDp(e) ; soma += dpPlan[e] ; } for(auto e : myPlans[x] ) { int a = get<0>(e) ; int b = get<1>(e) ; a = tin[a] ; b = tin[b] ; int sominha = bit.qry(a) + bit.qry(b) + soma + get<2>(e) ; dpPlan[x] = max(dpPlan[x] , sominha ) ; } dpPlan[x] = max(dpPlan[x] , soma ) ; bit.upd(tin[x] , soma - dpPlan[x] ) ; bit.upd( tout[x] + 1 , dpPlan[x] - soma ) ; } int main() { scanf("%d", &n ) ; for(int i = 0 , a ,b ; i < n-1 ; i++ ) { scanf("%d %d", &a, &b ) ; adj[a].push_back(b) ; adj[b].push_back(a) ; } memset(dp,-1,sizeof dp ); dfs(1) ; for(int i = 1 ; i < LOG ; i++ ) for(int j = 1 ; j <= n ; j++ ) if( dp[i-1][j] != -1 ) dp[i][j] = dp[i-1][dp[i-1][j]] ; scanf("%d", &m ) ; for(int i = 0 , a , b , c ; i < m ; i++ ) { scanf("%d %d %d", &a, &b, &c ) ; myPlans[ getLca(a,b) ].push_back( make_tuple(a,b,c) ) ; } calcDp(1) ; printf("%d\n", dpPlan[1] ) ; }
Compilation message
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
1 | Correct | 8 ms | 12908 KB | Output is correct |
2 | Correct | 8 ms | 12908 KB | Output is correct |
3 | Correct | 8 ms | 12908 KB | Output is correct |
4 | Correct | 9 ms | 12908 KB | Output is correct |
5 | Correct | 103 ms | 19300 KB | Output is correct |
6 | Correct | 55 ms | 26856 KB | Output is correct |
7 | Correct | 88 ms | 24292 KB | Output is correct |
8 | Correct | 76 ms | 19556 KB | Output is correct |
9 | Correct | 89 ms | 22628 KB | Output is correct |
10 | Correct | 75 ms | 19556 KB | Output is correct |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
1 | Correct | 8 ms | 12908 KB | Output is correct |
2 | Correct | 8 ms | 12908 KB | Output is correct |
3 | Correct | 9 ms | 13036 KB | Output is correct |
4 | Correct | 131 ms | 30180 KB | Output is correct |
5 | Correct | 133 ms | 30180 KB | Output is correct |
6 | Correct | 119 ms | 30204 KB | Output is correct |
7 | Correct | 140 ms | 30280 KB | Output is correct |
8 | Correct | 131 ms | 30180 KB | Output is correct |
9 | Correct | 119 ms | 30180 KB | Output is correct |
10 | Correct | 137 ms | 30400 KB | Output is correct |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
1 | Correct | 8 ms | 12908 KB | Output is correct |
2 | Correct | 8 ms | 12908 KB | Output is correct |
3 | Correct | 9 ms | 13036 KB | Output is correct |
4 | Correct | 131 ms | 30180 KB | Output is correct |
5 | Correct | 133 ms | 30180 KB | Output is correct |
6 | Correct | 119 ms | 30204 KB | Output is correct |
7 | Correct | 140 ms | 30280 KB | Output is correct |
8 | Correct | 131 ms | 30180 KB | Output is correct |
9 | Correct | 119 ms | 30180 KB | Output is correct |
10 | Correct | 137 ms | 30400 KB | Output is correct |
11 | Correct | 19 ms | 13804 KB | Output is correct |
12 | Correct | 131 ms | 30564 KB | Output is correct |
13 | Correct | 144 ms | 30564 KB | Output is correct |
14 | Correct | 124 ms | 30564 KB | Output is correct |
15 | Correct | 137 ms | 30640 KB | Output is correct |
16 | Correct | 120 ms | 30564 KB | Output is correct |
17 | Correct | 134 ms | 30564 KB | Output is correct |
18 | Correct | 136 ms | 30564 KB | Output is correct |
19 | Correct | 122 ms | 30564 KB | Output is correct |
20 | Correct | 133 ms | 30564 KB | Output is correct |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
1 | Correct | 188 ms | 22044 KB | Output is correct |
2 | Correct | 116 ms | 30180 KB | Output is correct |
3 | Correct | 254 ms | 27116 KB | Output is correct |
4 | Correct | 127 ms | 22556 KB | Output is correct |
5 | Correct | 187 ms | 26724 KB | Output is correct |
6 | Correct | 125 ms | 22484 KB | Output is correct |
7 | Correct | 245 ms | 26532 KB | Output is correct |
8 | Correct | 177 ms | 22244 KB | Output is correct |
9 | Correct | 116 ms | 30184 KB | Output is correct |
10 | Correct | 239 ms | 25568 KB | Output is correct |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
1 | Correct | 8 ms | 12908 KB | Output is correct |
2 | Correct | 8 ms | 12908 KB | Output is correct |
3 | Correct | 8 ms | 12908 KB | Output is correct |
4 | Correct | 9 ms | 12908 KB | Output is correct |
5 | Correct | 103 ms | 19300 KB | Output is correct |
6 | Correct | 55 ms | 26856 KB | Output is correct |
7 | Correct | 88 ms | 24292 KB | Output is correct |
8 | Correct | 76 ms | 19556 KB | Output is correct |
9 | Correct | 89 ms | 22628 KB | Output is correct |
10 | Correct | 75 ms | 19556 KB | Output is correct |
11 | Correct | 9 ms | 13036 KB | Output is correct |
12 | Correct | 9 ms | 13036 KB | Output is correct |
13 | Correct | 9 ms | 13036 KB | Output is correct |
14 | Correct | 9 ms | 13036 KB | Output is correct |
15 | Correct | 9 ms | 13036 KB | Output is correct |
16 | Correct | 9 ms | 13036 KB | Output is correct |
17 | Correct | 9 ms | 13036 KB | Output is correct |
18 | Correct | 9 ms | 13036 KB | Output is correct |
19 | Correct | 9 ms | 13036 KB | Output is correct |
20 | Correct | 9 ms | 13036 KB | Output is correct |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
1 | Correct | 8 ms | 12908 KB | Output is correct |
2 | Correct | 8 ms | 12908 KB | Output is correct |
3 | Correct | 8 ms | 12908 KB | Output is correct |
4 | Correct | 9 ms | 12908 KB | Output is correct |
5 | Correct | 103 ms | 19300 KB | Output is correct |
6 | Correct | 55 ms | 26856 KB | Output is correct |
7 | Correct | 88 ms | 24292 KB | Output is correct |
8 | Correct | 76 ms | 19556 KB | Output is correct |
9 | Correct | 89 ms | 22628 KB | Output is correct |
10 | Correct | 75 ms | 19556 KB | Output is correct |
11 | Correct | 8 ms | 12908 KB | Output is correct |
12 | Correct | 8 ms | 12908 KB | Output is correct |
13 | Correct | 9 ms | 13036 KB | Output is correct |
14 | Correct | 131 ms | 30180 KB | Output is correct |
15 | Correct | 133 ms | 30180 KB | Output is correct |
16 | Correct | 119 ms | 30204 KB | Output is correct |
17 | Correct | 140 ms | 30280 KB | Output is correct |
18 | Correct | 131 ms | 30180 KB | Output is correct |
19 | Correct | 119 ms | 30180 KB | Output is correct |
20 | Correct | 137 ms | 30400 KB | Output is correct |
21 | Correct | 19 ms | 13804 KB | Output is correct |
22 | Correct | 131 ms | 30564 KB | Output is correct |
23 | Correct | 144 ms | 30564 KB | Output is correct |
24 | Correct | 124 ms | 30564 KB | Output is correct |
25 | Correct | 137 ms | 30640 KB | Output is correct |
26 | Correct | 120 ms | 30564 KB | Output is correct |
27 | Correct | 134 ms | 30564 KB | Output is correct |
28 | Correct | 136 ms | 30564 KB | Output is correct |
29 | Correct | 122 ms | 30564 KB | Output is correct |
30 | Correct | 133 ms | 30564 KB | Output is correct |
31 | Correct | 188 ms | 22044 KB | Output is correct |
32 | Correct | 116 ms | 30180 KB | Output is correct |
33 | Correct | 254 ms | 27116 KB | Output is correct |
34 | Correct | 127 ms | 22556 KB | Output is correct |
35 | Correct | 187 ms | 26724 KB | Output is correct |
36 | Correct | 125 ms | 22484 KB | Output is correct |
37 | Correct | 245 ms | 26532 KB | Output is correct |
38 | Correct | 177 ms | 22244 KB | Output is correct |
39 | Correct | 116 ms | 30184 KB | Output is correct |
40 | Correct | 239 ms | 25568 KB | Output is correct |
41 | Correct | 9 ms | 13036 KB | Output is correct |
42 | Correct | 9 ms | 13036 KB | Output is correct |
43 | Correct | 9 ms | 13036 KB | Output is correct |
44 | Correct | 9 ms | 13036 KB | Output is correct |
45 | Correct | 9 ms | 13036 KB | Output is correct |
46 | Correct | 9 ms | 13036 KB | Output is correct |
47 | Correct | 9 ms | 13036 KB | Output is correct |
48 | Correct | 9 ms | 13036 KB | Output is correct |
49 | Correct | 9 ms | 13036 KB | Output is correct |
50 | Correct | 9 ms | 13036 KB | Output is correct |
51 | Correct | 181 ms | 22684 KB | Output is correct |
52 | Correct | 139 ms | 30564 KB | Output is correct |
53 | Correct | 244 ms | 25752 KB | Output is correct |
54 | Correct | 127 ms | 22812 KB | Output is correct |
55 | Correct | 182 ms | 22428 KB | Output is correct |
56 | Correct | 134 ms | 30564 KB | Output is correct |
57 | Correct | 185 ms | 26596 KB | Output is correct |
58 | Correct | 127 ms | 22816 KB | Output is correct |
59 | Correct | 180 ms | 22628 KB | Output is correct |
60 | Correct | 136 ms | 30436 KB | Output is correct |
61 | Correct | 189 ms | 26720 KB | Output is correct |
62 | Correct | 126 ms | 22464 KB | Output is correct |
63 | Correct | 183 ms | 22344 KB | Output is correct |
64 | Correct | 132 ms | 30648 KB | Output is correct |
65 | Correct | 249 ms | 26528 KB | Output is correct |
66 | Correct | 121 ms | 22816 KB | Output is correct |
67 | Correct | 186 ms | 22364 KB | Output is correct |
68 | Correct | 132 ms | 30564 KB | Output is correct |
69 | Correct | 185 ms | 25312 KB | Output is correct |
70 | Correct | 129 ms | 22856 KB | Output is correct |