#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];
array<int, 3> qq[N];
int in[N], out[N], rn[N], cnt;
int h[2 * N], rmq[2 * N][19], ct;
void predfs(int u, int par) {
in[u] = ++cnt;
rn[cnt] = u;
rmq[++ct][0] = in[u];
h[in[u]] = ct;
for(auto v : G[u]) if (v != par) {
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])];
}
namespace sub1{
int a[2][N];
void update(int u, int v) {
int p = lca(u, v);
a[0][u]++;
a[0][v]++;
a[0][p] -= 2;
a[1][p]++;
}
void dfs(int u, int par) {
for(auto v : G[u]) if (v != par) {
dfs(v, u);
a[0][u] += a[0][v];
}
}
long long sol() {
long long res = 0;
rep(mask, 0, (1 << m) - 1) {
long long s = 0;
rep(i, 1, m) if (bit(i - 1, mask)) {
s += qq[i][2];
update(qq[i][0], qq[i][1]);
}
dfs(1, -1);
bool flag = 1;
rep(i, 1, n) if (a[0][i] + a[1][i] > 1) flag = 0;
if (flag) res = max(res, s);
rep(i, 1, n) a[0][i] = a[1][i] = 0;
}
return res;
}
}
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);
}
cin >> m;
rep(i, 1, m) {
int u, v, c; cin >> u >> v >> c;
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();
}
Compilation message
election_campaign.cpp: In function 'long long int sub1::sol()':
election_campaign.cpp:60:30: warning: suggest parentheses around '-' inside '>>' [-Wparentheses]
60 | 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)
| ^
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
2652 KB |
Output is correct |
2 |
Correct |
2 ms |
2652 KB |
Output is correct |
3 |
Correct |
1 ms |
2652 KB |
Output is correct |
4 |
Correct |
138 ms |
2908 KB |
Output is correct |
5 |
Execution timed out |
1083 ms |
24404 KB |
Time limit exceeded |
6 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
2648 KB |
Output is correct |
2 |
Incorrect |
1 ms |
2648 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
2648 KB |
Output is correct |
2 |
Incorrect |
1 ms |
2648 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
54 ms |
26708 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
2652 KB |
Output is correct |
2 |
Correct |
2 ms |
2652 KB |
Output is correct |
3 |
Correct |
1 ms |
2652 KB |
Output is correct |
4 |
Correct |
138 ms |
2908 KB |
Output is correct |
5 |
Execution timed out |
1083 ms |
24404 KB |
Time limit exceeded |
6 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
2652 KB |
Output is correct |
2 |
Correct |
2 ms |
2652 KB |
Output is correct |
3 |
Correct |
1 ms |
2652 KB |
Output is correct |
4 |
Correct |
138 ms |
2908 KB |
Output is correct |
5 |
Execution timed out |
1083 ms |
24404 KB |
Time limit exceeded |
6 |
Halted |
0 ms |
0 KB |
- |