#include <bits/stdc++.h>
#define rep(i, l, r) for(int i = l; i <= r; i++)
#define rep2(i, l, r) for(int i = l; i >= r; i--)
#define fi first
#define se second
#define bit(i, x) (x >> i & 1)
using namespace std;
const int N = 1e5 + 3;
const int mod = 1e9 + 7;
mt19937_64 rng(chrono::steady_clock::now().time_since_epoch().count());
long long rnd(long long l, long long r) {
return uniform_int_distribution<long long>(l, r)(rng);
}
int n, m;
vector<int> G[N];
pair<int, int> e[N];
array<int, 3> qq[N];
int in[N], out[N], rn[N], cnt;
int h[2 * N], rmq[2 * N][19], ct;
int f[N][18], dep[N];
void predfs(int u, int par) {
in[u] = ++cnt;
rn[cnt] = u;
rmq[++ct][0] = in[u];
h[in[u]] = ct;
rep(i, 1, 17) f[u][i] = f[f[u][i - 1]][i - 1];
for(auto v : G[u]) if (v != par) {
f[v][0] = u;
dep[v] = dep[u] + 1;
predfs(v, u);
rmq[++ct][0] = in[u];
}
out[u] = cnt;
}
int lca(int x, int y) {
x = h[in[x]], y = h[in[y]];
if (x > y) swap(x, y);
int kk = 31 - __builtin_clz(y - x + 1);
return rn[min(rmq[x][kk], rmq[y - (1 << kk) + 1][kk])];
}
bool path(int x, int y, int u) {
bool flag = 0;
flag |= (lca(y, u) == u && lca(x, u) == x);
swap(x, y);
flag |= (lca(y, u) == u && lca(x, u) == x);
return flag;
}
bool check(int x, int y, int u, int v) {
int p = lca(x, y);
int g = lca(u, v);
bool flag = 0;
flag |= (path(p, x, u) || path(p, x, v) || path(p, x, g));
flag |= (path(p, y, u) || path(p, y, v) || path(p, y, g));
swap(x, u);
swap(y, v);
swap(p, g);
flag |= (path(p, x, u) || path(p, x, v) || path(p, x, g));
flag |= (path(p, y, u) || path(p, y, v) || path(p, y, g));
return flag;
}
int jump(int x, int y) {
if (x == y || !x || !y) return 0;
if (dep[x] < dep[y]) swap(x, y);
int delta = dep[x] - dep[y] - 1;
rep(i, 0, 17) if (bit(i, delta)) x = f[x][i];
return x;
}
namespace sub1{
long long sol() {
long long res = 0;
rep(mask, 0, (1 << m) - 1) {
long long s = 0;
bool flag = 1;
rep(i, 1, m) if (bit(i - 1, mask)) {
s += qq[i][2];
rep(j, i + 1, m) if (bit(j - 1, mask)) {
if (check(qq[i][0], qq[i][1], qq[j][0], qq[j][1])) flag = 0;
}
}
if (flag) res = max(res, s);
}
return res;
}
}
namespace sub23{
long long dp[N];
long long sol() {
rep(i, 1, n - 1) {
assert(e[i].fi == i && e[i].se == i + 1);
}
sort(qq + 1, qq + m + 1, [&](array<int, 3> x, array<int, 3> y) {
return x[1] < y[1];
});
long long res = 0;
for(int i = 1, j = 1, k = 1; i <= m; ) {
while(k <= qq[i][1]) {
dp[k] = max(dp[k], dp[k - 1]);
k++;
}
while(j <= m && qq[i][1] == qq[j][1]) {
dp[qq[j][1]] = max(dp[qq[j][1]], dp[qq[j][0] - 1] + qq[j][2]);
j++;
}
res = max(res, dp[qq[i][1]]);
i = j;
}
return res;
}
}
namespace sub6{
long long dp[N][2];
vector<array<int, 3>> g[N];
long long bit[N];
void update(int u, long long x) {
for(int i = u; i <= n; i += i & -i) bit[i] += x;
}
void rangeupdate(int u, int v, long long x) {
update(u, x);
update(v + 1, -x);
}
long long query(int u) {
long long s = 0;
for(int i = u; i; i -= i & -i) s += bit[i];
return s;
}
long long query(int u, int v) {
if (u > v) return 0;
return query(v) - query(u - 1);
}
void dfs(int u, int par) {
for(auto v : G[u]) if (v != par) {
// if (u == 12) cout << v << "\n";
dfs(v, u);
dp[u][0] += dp[v][1];
}
dp[u][1] = dp[u][0];
for(auto i : g[u]) {
int x = i[0];
int y = i[1];
int c = i[2];
// if (x == u) swap(x, y);
int xx = jump(x, u);
int yy = jump(y, u);
// cout << x << " " << y << " " << c << "\n";
dp[u][1] = max(dp[u][1], dp[u][0] + query(in[x]) + query(in[y]) - 2 * query(in[u]) + c);
}
rangeupdate(in[u], out[u], (dp[u][0] - dp[u][1]));
}
long long sol() {
rep(i, 1, m) g[lca(qq[i][0], qq[i][1])].push_back(qq[i]);
dfs(1, -1);
long long res = 0;
rep(i, 1, n) res = max(res, dp[i][1]);
// rep(i, 1, n) cout << dp[i][1] << " ";
return res;
}
}
void Gen() {
n = 1000;
rep(i, 1, n - 1) {
int u = i;
int v = rnd(i + 1, n);
G[u].push_back(v);
G[v].push_back(u);
e[i] = {u, v};
}
m = 14;
rep(i, 1, m) {
int u = rnd(1, n);
int v = rnd(1, n);
int c = rnd(1, 1e4);
if (u > v) swap(u, v);
qq[i] = {u, v, c};
}
}
int main(int argc, char* argv[]) {
ios_base::sync_with_stdio(0);
cin.tie(0); cout.tie(0);
// freopen("testing.txt", "r", stdin);
// freopen("outputing.txt", "w", stdout);
cin >> n;
rep(i, 1, n - 1) {
int u, v; cin >> u >> v;
G[u].push_back(v);
G[v].push_back(u);
e[i] = {u, v};
}
cin >> m;
rep(i, 1, m) {
int u, v, c; cin >> u >> v >> c;
if (u > v) swap(u, v);
qq[i] = {u, v, c};
}
predfs(1, -1);
rep(j, 1, 18) rep(i, 1, ct - (1 << (j - 1))) rmq[i][j] = min(rmq[i][j - 1], rmq[i + (1 << (j - 1))][j - 1]);
//// cout << sub1::sol();
//// cout << sub23::sol();
cout << sub6::sol();
// cout << jump(3, 5);
// Gen();
// predfs(1, -1);
// rep(j, 1, 18) rep(i, 1, ct - (1 << (j - 1))) rmq[i][j] = min(rmq[i][j - 1], rmq[i + (1 << (j - 1))][j - 1]);
// cout << n << " ";
// cout << "\n";
// rep(i, 1, n - 1) cout << e[i].fi << " " << e[i].se << "\n";
// cout << m << "\n";
// rep(i, 1, m) {
// rep(j, 0, 2) cout << qq[i][j] << " ";
// cout << "\n";
// }
// cout << "\n";
// pair<int, int> res = {sub1::sol(), sub6::sol()};
// cout << "\n";
// cout << res.fi << " " << res.se;
// cout << jump(5, 12);
}
Compilation message
election_campaign.cpp: In function 'long long int sub1::sol()':
election_campaign.cpp:79:30: warning: suggest parentheses around '-' inside '>>' [-Wparentheses]
79 | rep(i, 1, m) if (bit(i - 1, mask)) {
| ~~^~~
election_campaign.cpp:6:25: note: in definition of macro 'bit'
6 | #define bit(i, x) (x >> i & 1)
| ^
election_campaign.cpp:81:36: warning: suggest parentheses around '-' inside '>>' [-Wparentheses]
81 | rep(j, i + 1, m) if (bit(j - 1, mask)) {
| ~~^~~
election_campaign.cpp:6:25: note: in definition of macro 'bit'
6 | #define bit(i, x) (x >> i & 1)
| ^
election_campaign.cpp: In function 'void sub6::dfs(int, int)':
election_campaign.cpp:149:11: warning: unused variable 'xx' [-Wunused-variable]
149 | int xx = jump(x, u);
| ^~
election_campaign.cpp:150:11: warning: unused variable 'yy' [-Wunused-variable]
150 | int yy = jump(y, u);
| ^~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
7516 KB |
Output is correct |
2 |
Correct |
2 ms |
6504 KB |
Output is correct |
3 |
Correct |
3 ms |
5916 KB |
Output is correct |
4 |
Correct |
2 ms |
6748 KB |
Output is correct |
5 |
Correct |
67 ms |
36440 KB |
Output is correct |
6 |
Correct |
51 ms |
45648 KB |
Output is correct |
7 |
Correct |
71 ms |
42536 KB |
Output is correct |
8 |
Correct |
56 ms |
36760 KB |
Output is correct |
9 |
Correct |
70 ms |
40528 KB |
Output is correct |
10 |
Correct |
55 ms |
36720 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
6492 KB |
Output is correct |
2 |
Correct |
2 ms |
5724 KB |
Output is correct |
3 |
Correct |
2 ms |
6236 KB |
Output is correct |
4 |
Correct |
77 ms |
50168 KB |
Output is correct |
5 |
Correct |
76 ms |
50288 KB |
Output is correct |
6 |
Correct |
81 ms |
50260 KB |
Output is correct |
7 |
Correct |
80 ms |
50164 KB |
Output is correct |
8 |
Correct |
76 ms |
50192 KB |
Output is correct |
9 |
Correct |
75 ms |
50224 KB |
Output is correct |
10 |
Correct |
74 ms |
50260 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
6492 KB |
Output is correct |
2 |
Correct |
2 ms |
5724 KB |
Output is correct |
3 |
Correct |
2 ms |
6236 KB |
Output is correct |
4 |
Correct |
77 ms |
50168 KB |
Output is correct |
5 |
Correct |
76 ms |
50288 KB |
Output is correct |
6 |
Correct |
81 ms |
50260 KB |
Output is correct |
7 |
Correct |
80 ms |
50164 KB |
Output is correct |
8 |
Correct |
76 ms |
50192 KB |
Output is correct |
9 |
Correct |
75 ms |
50224 KB |
Output is correct |
10 |
Correct |
74 ms |
50260 KB |
Output is correct |
11 |
Correct |
11 ms |
8280 KB |
Output is correct |
12 |
Correct |
78 ms |
50516 KB |
Output is correct |
13 |
Correct |
76 ms |
50512 KB |
Output is correct |
14 |
Correct |
81 ms |
50516 KB |
Output is correct |
15 |
Correct |
74 ms |
50516 KB |
Output is correct |
16 |
Correct |
87 ms |
50512 KB |
Output is correct |
17 |
Correct |
80 ms |
50512 KB |
Output is correct |
18 |
Correct |
74 ms |
50516 KB |
Output is correct |
19 |
Correct |
85 ms |
50516 KB |
Output is correct |
20 |
Correct |
85 ms |
50516 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
98 ms |
40492 KB |
Output is correct |
2 |
Correct |
79 ms |
50256 KB |
Output is correct |
3 |
Correct |
101 ms |
46420 KB |
Output is correct |
4 |
Correct |
82 ms |
40912 KB |
Output is correct |
5 |
Correct |
99 ms |
45912 KB |
Output is correct |
6 |
Correct |
72 ms |
40776 KB |
Output is correct |
7 |
Correct |
92 ms |
45712 KB |
Output is correct |
8 |
Correct |
91 ms |
40784 KB |
Output is correct |
9 |
Correct |
77 ms |
50332 KB |
Output is correct |
10 |
Correct |
103 ms |
44572 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
7516 KB |
Output is correct |
2 |
Correct |
2 ms |
6504 KB |
Output is correct |
3 |
Correct |
3 ms |
5916 KB |
Output is correct |
4 |
Correct |
2 ms |
6748 KB |
Output is correct |
5 |
Correct |
67 ms |
36440 KB |
Output is correct |
6 |
Correct |
51 ms |
45648 KB |
Output is correct |
7 |
Correct |
71 ms |
42536 KB |
Output is correct |
8 |
Correct |
56 ms |
36760 KB |
Output is correct |
9 |
Correct |
70 ms |
40528 KB |
Output is correct |
10 |
Correct |
55 ms |
36720 KB |
Output is correct |
11 |
Correct |
2 ms |
8028 KB |
Output is correct |
12 |
Correct |
2 ms |
6236 KB |
Output is correct |
13 |
Correct |
3 ms |
6232 KB |
Output is correct |
14 |
Correct |
3 ms |
6236 KB |
Output is correct |
15 |
Correct |
3 ms |
6236 KB |
Output is correct |
16 |
Correct |
2 ms |
6748 KB |
Output is correct |
17 |
Correct |
2 ms |
6748 KB |
Output is correct |
18 |
Correct |
2 ms |
6236 KB |
Output is correct |
19 |
Correct |
2 ms |
8028 KB |
Output is correct |
20 |
Correct |
3 ms |
6236 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
7516 KB |
Output is correct |
2 |
Correct |
2 ms |
6504 KB |
Output is correct |
3 |
Correct |
3 ms |
5916 KB |
Output is correct |
4 |
Correct |
2 ms |
6748 KB |
Output is correct |
5 |
Correct |
67 ms |
36440 KB |
Output is correct |
6 |
Correct |
51 ms |
45648 KB |
Output is correct |
7 |
Correct |
71 ms |
42536 KB |
Output is correct |
8 |
Correct |
56 ms |
36760 KB |
Output is correct |
9 |
Correct |
70 ms |
40528 KB |
Output is correct |
10 |
Correct |
55 ms |
36720 KB |
Output is correct |
11 |
Correct |
2 ms |
6492 KB |
Output is correct |
12 |
Correct |
2 ms |
5724 KB |
Output is correct |
13 |
Correct |
2 ms |
6236 KB |
Output is correct |
14 |
Correct |
77 ms |
50168 KB |
Output is correct |
15 |
Correct |
76 ms |
50288 KB |
Output is correct |
16 |
Correct |
81 ms |
50260 KB |
Output is correct |
17 |
Correct |
80 ms |
50164 KB |
Output is correct |
18 |
Correct |
76 ms |
50192 KB |
Output is correct |
19 |
Correct |
75 ms |
50224 KB |
Output is correct |
20 |
Correct |
74 ms |
50260 KB |
Output is correct |
21 |
Correct |
11 ms |
8280 KB |
Output is correct |
22 |
Correct |
78 ms |
50516 KB |
Output is correct |
23 |
Correct |
76 ms |
50512 KB |
Output is correct |
24 |
Correct |
81 ms |
50516 KB |
Output is correct |
25 |
Correct |
74 ms |
50516 KB |
Output is correct |
26 |
Correct |
87 ms |
50512 KB |
Output is correct |
27 |
Correct |
80 ms |
50512 KB |
Output is correct |
28 |
Correct |
74 ms |
50516 KB |
Output is correct |
29 |
Correct |
85 ms |
50516 KB |
Output is correct |
30 |
Correct |
85 ms |
50516 KB |
Output is correct |
31 |
Correct |
98 ms |
40492 KB |
Output is correct |
32 |
Correct |
79 ms |
50256 KB |
Output is correct |
33 |
Correct |
101 ms |
46420 KB |
Output is correct |
34 |
Correct |
82 ms |
40912 KB |
Output is correct |
35 |
Correct |
99 ms |
45912 KB |
Output is correct |
36 |
Correct |
72 ms |
40776 KB |
Output is correct |
37 |
Correct |
92 ms |
45712 KB |
Output is correct |
38 |
Correct |
91 ms |
40784 KB |
Output is correct |
39 |
Correct |
77 ms |
50332 KB |
Output is correct |
40 |
Correct |
103 ms |
44572 KB |
Output is correct |
41 |
Correct |
2 ms |
8028 KB |
Output is correct |
42 |
Correct |
2 ms |
6236 KB |
Output is correct |
43 |
Correct |
3 ms |
6232 KB |
Output is correct |
44 |
Correct |
3 ms |
6236 KB |
Output is correct |
45 |
Correct |
3 ms |
6236 KB |
Output is correct |
46 |
Correct |
2 ms |
6748 KB |
Output is correct |
47 |
Correct |
2 ms |
6748 KB |
Output is correct |
48 |
Correct |
2 ms |
6236 KB |
Output is correct |
49 |
Correct |
2 ms |
8028 KB |
Output is correct |
50 |
Correct |
3 ms |
6236 KB |
Output is correct |
51 |
Correct |
101 ms |
41040 KB |
Output is correct |
52 |
Correct |
78 ms |
50464 KB |
Output is correct |
53 |
Correct |
105 ms |
44808 KB |
Output is correct |
54 |
Correct |
82 ms |
41128 KB |
Output is correct |
55 |
Correct |
84 ms |
40712 KB |
Output is correct |
56 |
Correct |
74 ms |
50512 KB |
Output is correct |
57 |
Correct |
92 ms |
45652 KB |
Output is correct |
58 |
Correct |
78 ms |
40972 KB |
Output is correct |
59 |
Correct |
100 ms |
41040 KB |
Output is correct |
60 |
Correct |
76 ms |
50512 KB |
Output is correct |
61 |
Correct |
100 ms |
45908 KB |
Output is correct |
62 |
Correct |
73 ms |
40820 KB |
Output is correct |
63 |
Correct |
82 ms |
40636 KB |
Output is correct |
64 |
Correct |
77 ms |
50512 KB |
Output is correct |
65 |
Correct |
92 ms |
45580 KB |
Output is correct |
66 |
Correct |
74 ms |
41116 KB |
Output is correct |
67 |
Correct |
91 ms |
40776 KB |
Output is correct |
68 |
Correct |
80 ms |
50500 KB |
Output is correct |
69 |
Correct |
122 ms |
44372 KB |
Output is correct |
70 |
Correct |
75 ms |
41360 KB |
Output is correct |