#include <bits/stdc++.h>
#define fi first
#define se second
#define mp make_pair
#define pb push_back
using namespace std;
typedef long long ll;
const int maxn = 1e5 + 5;
int U[maxn], V[maxn], W[maxn];
vector<int> have[maxn];
int N, M, depth[maxn], par[maxn][20];
vector<int> adj[maxn];
int f[maxn], sum[maxn];
int tin[maxn], tout[maxn];
class fenwick
{
public:
vector<int> cnt; int n;
void init(int _n)
{
n = _n;
cnt.assign(n + 5, 0);
}
void upd(int i, int v)
{
for (; i <= n; i += i & -i){
cnt[i] += v;
}
}
int get(int i)
{
int sum = 0;
for (; i; i -= i & -i){
sum += cnt[i];
}
return sum;
}
}ft;
void prepare(int u, int p = -1)
{
static int nTime = 0;
tin[u] = ++nTime;
for (int i = 1; (1 << i) <= N; ++i){
par[u][i] = par[par[u][i - 1]][i - 1];
}
for (int v : adj[u]){
if (v == p) continue;
depth[v] = depth[u] + 1;
par[v][0] = u;
prepare(v, u);
}
tout[u] = nTime;
}
bool In(int u, int v)
{
return tin[u] <= tin[v] && tout[v] <= tout[u];
}
int LCA(int x, int y)
{
if (depth[y] > depth[x]) swap(x, y);
if (In(y, x)) return y;
for (int k = 19; k >= 0; --k){
if (depth[par[x][k]] >= depth[y]){
x = par[x][k];
}
}
if (x == y) return x;
for (int k = 19; k >= 0; --k){
if (par[x][k] != par[y][k]){
x = par[x][k];
y = par[y][k];
}
}
return par[x][0];
}
int Can(int u, int v)
{
int l = (u == 1 ? 0 : 1), r = adj[u].size() - 1;
while (l <= r){
int mid = (l + r) / 2;
int p = adj[u][mid];
if (tout[p] < tin[v])
l = mid + 1;
else
r = mid - 1;
}
assert(In(adj[u][l], v));
return adj[u][l];
}
void solve(int u, int p = -1)
{
for (int v : adj[u]){
if (v == p) continue;
solve(v, u);
sum[u] += f[v];
}
f[u] = sum[u];
//cerr << u << ' ' << sum[u] << '\n';
for (auto & i : have[u]){
f[u] = max(f[u], sum[u] + W[i] + ft.get(tin[U[i]]) + ft.get(tin[V[i]]));
}
ft.upd(tin[u], sum[u] - f[u]);
ft.upd(tout[u] + 1, f[u] - sum[u]);
///cerr << u << ' ' << f[u] << '\n';
}
signed main(void)
{
ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
if (fopen("A.INP", "r")){
freopen("A.INP", "r", stdin);
freopen("A.OUT", "w", stdout);
}
cin >> N; ft.init(maxn);
for (int i = 1; i < N; ++i){
int u, v; cin >> u >> v;
adj[u].pb(v); adj[v].pb(u);
}
cin >> M;
for (int i = 1; i <= M; ++i){
cin >> U[i] >> V[i] >> W[i];
}
depth[1] = 1;
prepare(1);
for (int i = 1; i <= M; ++i){
have[LCA(U[i], V[i])].pb(i);
}
for (int i = 1; i <= N; ++i){
sort(adj[i].begin(), adj[i].end(),
[&](const int & x, const int & y){
return tin[x] < tin[y];
}
);
}
solve(1);
cout << f[1];
}
Compilation message
election_campaign.cpp: In function 'int main()':
election_campaign.cpp:121:12: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)', declared with attribute warn_unused_result [-Wunused-result]
freopen("A.INP", "r", stdin);
~~~~~~~^~~~~~~~~~~~~~~~~~~~~
election_campaign.cpp:122:12: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)', declared with attribute warn_unused_result [-Wunused-result]
freopen("A.OUT", "w", stdout);
~~~~~~~^~~~~~~~~~~~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
8 ms |
5496 KB |
Output is correct |
2 |
Correct |
8 ms |
5496 KB |
Output is correct |
3 |
Correct |
9 ms |
5496 KB |
Output is correct |
4 |
Correct |
8 ms |
5624 KB |
Output is correct |
5 |
Correct |
126 ms |
19704 KB |
Output is correct |
6 |
Correct |
73 ms |
27384 KB |
Output is correct |
7 |
Correct |
134 ms |
24708 KB |
Output is correct |
8 |
Correct |
90 ms |
19704 KB |
Output is correct |
9 |
Correct |
123 ms |
23032 KB |
Output is correct |
10 |
Correct |
90 ms |
19704 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
8 ms |
5496 KB |
Output is correct |
2 |
Correct |
9 ms |
5496 KB |
Output is correct |
3 |
Correct |
8 ms |
5624 KB |
Output is correct |
4 |
Correct |
116 ms |
28640 KB |
Output is correct |
5 |
Correct |
117 ms |
28664 KB |
Output is correct |
6 |
Correct |
122 ms |
28664 KB |
Output is correct |
7 |
Correct |
119 ms |
28664 KB |
Output is correct |
8 |
Correct |
113 ms |
28664 KB |
Output is correct |
9 |
Correct |
109 ms |
28792 KB |
Output is correct |
10 |
Correct |
122 ms |
28840 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
8 ms |
5496 KB |
Output is correct |
2 |
Correct |
9 ms |
5496 KB |
Output is correct |
3 |
Correct |
8 ms |
5624 KB |
Output is correct |
4 |
Correct |
116 ms |
28640 KB |
Output is correct |
5 |
Correct |
117 ms |
28664 KB |
Output is correct |
6 |
Correct |
122 ms |
28664 KB |
Output is correct |
7 |
Correct |
119 ms |
28664 KB |
Output is correct |
8 |
Correct |
113 ms |
28664 KB |
Output is correct |
9 |
Correct |
109 ms |
28792 KB |
Output is correct |
10 |
Correct |
122 ms |
28840 KB |
Output is correct |
11 |
Correct |
17 ms |
6392 KB |
Output is correct |
12 |
Correct |
118 ms |
28664 KB |
Output is correct |
13 |
Correct |
122 ms |
28820 KB |
Output is correct |
14 |
Correct |
118 ms |
28792 KB |
Output is correct |
15 |
Correct |
118 ms |
28764 KB |
Output is correct |
16 |
Correct |
118 ms |
28792 KB |
Output is correct |
17 |
Correct |
118 ms |
28664 KB |
Output is correct |
18 |
Correct |
118 ms |
28664 KB |
Output is correct |
19 |
Correct |
116 ms |
28720 KB |
Output is correct |
20 |
Correct |
118 ms |
28664 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
208 ms |
20472 KB |
Output is correct |
2 |
Correct |
112 ms |
30968 KB |
Output is correct |
3 |
Correct |
239 ms |
27896 KB |
Output is correct |
4 |
Correct |
152 ms |
22644 KB |
Output is correct |
5 |
Correct |
215 ms |
27512 KB |
Output is correct |
6 |
Correct |
152 ms |
22844 KB |
Output is correct |
7 |
Correct |
232 ms |
27128 KB |
Output is correct |
8 |
Correct |
194 ms |
23164 KB |
Output is correct |
9 |
Correct |
118 ms |
30968 KB |
Output is correct |
10 |
Correct |
240 ms |
26228 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
8 ms |
5496 KB |
Output is correct |
2 |
Correct |
8 ms |
5496 KB |
Output is correct |
3 |
Correct |
9 ms |
5496 KB |
Output is correct |
4 |
Correct |
8 ms |
5624 KB |
Output is correct |
5 |
Correct |
126 ms |
19704 KB |
Output is correct |
6 |
Correct |
73 ms |
27384 KB |
Output is correct |
7 |
Correct |
134 ms |
24708 KB |
Output is correct |
8 |
Correct |
90 ms |
19704 KB |
Output is correct |
9 |
Correct |
123 ms |
23032 KB |
Output is correct |
10 |
Correct |
90 ms |
19704 KB |
Output is correct |
11 |
Correct |
9 ms |
5624 KB |
Output is correct |
12 |
Correct |
8 ms |
5752 KB |
Output is correct |
13 |
Correct |
9 ms |
5624 KB |
Output is correct |
14 |
Correct |
9 ms |
5624 KB |
Output is correct |
15 |
Correct |
9 ms |
5624 KB |
Output is correct |
16 |
Correct |
10 ms |
5624 KB |
Output is correct |
17 |
Correct |
9 ms |
5624 KB |
Output is correct |
18 |
Correct |
9 ms |
5624 KB |
Output is correct |
19 |
Correct |
8 ms |
5624 KB |
Output is correct |
20 |
Correct |
9 ms |
5752 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
8 ms |
5496 KB |
Output is correct |
2 |
Correct |
8 ms |
5496 KB |
Output is correct |
3 |
Correct |
9 ms |
5496 KB |
Output is correct |
4 |
Correct |
8 ms |
5624 KB |
Output is correct |
5 |
Correct |
126 ms |
19704 KB |
Output is correct |
6 |
Correct |
73 ms |
27384 KB |
Output is correct |
7 |
Correct |
134 ms |
24708 KB |
Output is correct |
8 |
Correct |
90 ms |
19704 KB |
Output is correct |
9 |
Correct |
123 ms |
23032 KB |
Output is correct |
10 |
Correct |
90 ms |
19704 KB |
Output is correct |
11 |
Correct |
8 ms |
5496 KB |
Output is correct |
12 |
Correct |
9 ms |
5496 KB |
Output is correct |
13 |
Correct |
8 ms |
5624 KB |
Output is correct |
14 |
Correct |
116 ms |
28640 KB |
Output is correct |
15 |
Correct |
117 ms |
28664 KB |
Output is correct |
16 |
Correct |
122 ms |
28664 KB |
Output is correct |
17 |
Correct |
119 ms |
28664 KB |
Output is correct |
18 |
Correct |
113 ms |
28664 KB |
Output is correct |
19 |
Correct |
109 ms |
28792 KB |
Output is correct |
20 |
Correct |
122 ms |
28840 KB |
Output is correct |
21 |
Correct |
17 ms |
6392 KB |
Output is correct |
22 |
Correct |
118 ms |
28664 KB |
Output is correct |
23 |
Correct |
122 ms |
28820 KB |
Output is correct |
24 |
Correct |
118 ms |
28792 KB |
Output is correct |
25 |
Correct |
118 ms |
28764 KB |
Output is correct |
26 |
Correct |
118 ms |
28792 KB |
Output is correct |
27 |
Correct |
118 ms |
28664 KB |
Output is correct |
28 |
Correct |
118 ms |
28664 KB |
Output is correct |
29 |
Correct |
116 ms |
28720 KB |
Output is correct |
30 |
Correct |
118 ms |
28664 KB |
Output is correct |
31 |
Correct |
208 ms |
20472 KB |
Output is correct |
32 |
Correct |
112 ms |
30968 KB |
Output is correct |
33 |
Correct |
239 ms |
27896 KB |
Output is correct |
34 |
Correct |
152 ms |
22644 KB |
Output is correct |
35 |
Correct |
215 ms |
27512 KB |
Output is correct |
36 |
Correct |
152 ms |
22844 KB |
Output is correct |
37 |
Correct |
232 ms |
27128 KB |
Output is correct |
38 |
Correct |
194 ms |
23164 KB |
Output is correct |
39 |
Correct |
118 ms |
30968 KB |
Output is correct |
40 |
Correct |
240 ms |
26228 KB |
Output is correct |
41 |
Correct |
9 ms |
5624 KB |
Output is correct |
42 |
Correct |
8 ms |
5752 KB |
Output is correct |
43 |
Correct |
9 ms |
5624 KB |
Output is correct |
44 |
Correct |
9 ms |
5624 KB |
Output is correct |
45 |
Correct |
9 ms |
5624 KB |
Output is correct |
46 |
Correct |
10 ms |
5624 KB |
Output is correct |
47 |
Correct |
9 ms |
5624 KB |
Output is correct |
48 |
Correct |
9 ms |
5624 KB |
Output is correct |
49 |
Correct |
8 ms |
5624 KB |
Output is correct |
50 |
Correct |
9 ms |
5752 KB |
Output is correct |
51 |
Correct |
210 ms |
23416 KB |
Output is correct |
52 |
Correct |
121 ms |
31224 KB |
Output is correct |
53 |
Correct |
250 ms |
26740 KB |
Output is correct |
54 |
Correct |
147 ms |
23136 KB |
Output is correct |
55 |
Correct |
210 ms |
22900 KB |
Output is correct |
56 |
Correct |
118 ms |
31224 KB |
Output is correct |
57 |
Correct |
217 ms |
27436 KB |
Output is correct |
58 |
Correct |
148 ms |
22904 KB |
Output is correct |
59 |
Correct |
211 ms |
23392 KB |
Output is correct |
60 |
Correct |
118 ms |
31224 KB |
Output is correct |
61 |
Correct |
205 ms |
27384 KB |
Output is correct |
62 |
Correct |
154 ms |
22960 KB |
Output is correct |
63 |
Correct |
214 ms |
23040 KB |
Output is correct |
64 |
Correct |
116 ms |
31224 KB |
Output is correct |
65 |
Correct |
241 ms |
27124 KB |
Output is correct |
66 |
Correct |
156 ms |
23032 KB |
Output is correct |
67 |
Correct |
199 ms |
23032 KB |
Output is correct |
68 |
Correct |
115 ms |
31224 KB |
Output is correct |
69 |
Correct |
213 ms |
26360 KB |
Output is correct |
70 |
Correct |
153 ms |
23032 KB |
Output is correct |