#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
#define MASK(i) (1LL << (i))
#define GETBIT(mask, i) (((mask) >> (i)) & 1)
#define ALL(v) (v).begin(), (v).end()
#define block_of_code if(true)
ll max(ll a, ll b){return (a > b) ? a : b;}
ll min(ll a, ll b){return (a < b) ? a : b;}
ll gcd(ll a, ll b){return __gcd(a, b);}
ll lcm(ll a, ll b){return a / gcd(a, b) * b;}
ll LASTBIT(ll mask){return (mask) & (-mask);}
int pop_cnt(ll mask){return __builtin_popcountll(mask);}
int ctz(ull mask){return __builtin_ctzll(mask);}
int logOf(ull mask){return 63 - __builtin_clzll(mask);}
mt19937_64 rng(chrono::high_resolution_clock::now().time_since_epoch().count());
ll rngesus(ll l, ll r){return l + (ull) rng() % (r - l + 1);}
double rngesus_d(double l, double r){
double cur = rngesus(0, MASK(60) - 1);
cur /= MASK(60) - 1;
return l + cur * (r - l);
}
template <class T1, class T2>
bool maximize(T1 &a, T2 b){
if (a < b) {a = b; return true;}
return false;
}
template <class T1, class T2>
bool minimize(T1 &a, T2 b){
if (a > b) {a = b; return true;}
return false;
}
template <class T>
void printArr(T container, string separator = " ", string finish = "\n", ostream &out = cout){
for(auto item: container) out << item << separator;
out << finish;
}
template <class T>
void remove_dup(vector<T> &a){
sort(ALL(a));
a.resize(unique(ALL(a)) - a.begin());
}
const int N = 1e5 + 69, LOG_N = 17;
int n, q;
vector<int> graph[N];
int parent[N][LOG_N], h[N];
int in[N], dfs_cnt = 0;
void dfs(int u, int p){
in[u] = ++dfs_cnt;
h[u] = h[p] + 1;
for(int j = 1; MASK(j) < h[u]; ++j)
parent[u][j] = parent[parent[u][j-1]][j-1];
for(int v: graph[u]) if (v != p){
parent[v][0] = u;
dfs(v, u);
}
}
int jump(int u, int d){
for(int j = 0; j < LOG_N; ++j) if (GETBIT(d, j)) u = parent[u][j];
return u;
}
int LCA(int u, int v){
if (h[u] < h[v]) swap(u, v);
u = jump(u, h[u] - h[v]);
if (u == v) return u;
for(int j = LOG_N - 1; j>=0; --j) if (parent[u][j] != parent[v][j]){
u = parent[u][j];
v = parent[v][j];
}
return parent[u][0];
}
vector<array<int, 3>> queries[N];
ll dp[N], sum[N];
void go(int u, int p){
for(int v: graph[u]) if (v != p){
go(v, u);
sum[u] += dp[v];
dp[u] += dp[v];
}
for(array<int, 3> i: queries[u]){
int x = i[0], y = i[1], w = i[2];
ll ans = sum[u] + w;
while(x != u){
ans += sum[x] - dp[x];
x = parent[x][0];
}
while(y != u){
ans += sum[y] - dp[y];
y = parent[y][0];
}
maximize(dp[u], ans);
}
}
int main(void){
ios::sync_with_stdio(0);cin.tie(0); cout.tie(0);
clock_t start = clock();
cin >> n;
for(int i = 1; i<n; ++i){
int u,v; cin >> u >> v;
graph[u].push_back(v);
graph[v].push_back(u);
}
dfs(1, 0);
cin >> q;
while(q--){
int u, v, w; cin >> u >> v >> w;
queries[LCA(u, v)].push_back({{u, v, w}});
}
go(1, 0);
cout << dp[1] << "\n";
cerr << "Time elapsed: " << clock() - start << " ms\n";
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
5976 KB |
Output is correct |
2 |
Correct |
2 ms |
5980 KB |
Output is correct |
3 |
Correct |
2 ms |
4956 KB |
Output is correct |
4 |
Correct |
2 ms |
5212 KB |
Output is correct |
5 |
Correct |
39 ms |
18384 KB |
Output is correct |
6 |
Correct |
32 ms |
26248 KB |
Output is correct |
7 |
Correct |
50 ms |
23556 KB |
Output is correct |
8 |
Correct |
26 ms |
17496 KB |
Output is correct |
9 |
Correct |
52 ms |
21752 KB |
Output is correct |
10 |
Correct |
25 ms |
17496 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
5980 KB |
Output is correct |
2 |
Correct |
2 ms |
4956 KB |
Output is correct |
3 |
Correct |
2 ms |
5212 KB |
Output is correct |
4 |
Execution timed out |
1098 ms |
26948 KB |
Time limit exceeded |
5 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
5980 KB |
Output is correct |
2 |
Correct |
2 ms |
4956 KB |
Output is correct |
3 |
Correct |
2 ms |
5212 KB |
Output is correct |
4 |
Execution timed out |
1098 ms |
26948 KB |
Time limit exceeded |
5 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
79 ms |
18696 KB |
Output is correct |
2 |
Execution timed out |
1103 ms |
29500 KB |
Time limit exceeded |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
5976 KB |
Output is correct |
2 |
Correct |
2 ms |
5980 KB |
Output is correct |
3 |
Correct |
2 ms |
4956 KB |
Output is correct |
4 |
Correct |
2 ms |
5212 KB |
Output is correct |
5 |
Correct |
39 ms |
18384 KB |
Output is correct |
6 |
Correct |
32 ms |
26248 KB |
Output is correct |
7 |
Correct |
50 ms |
23556 KB |
Output is correct |
8 |
Correct |
26 ms |
17496 KB |
Output is correct |
9 |
Correct |
52 ms |
21752 KB |
Output is correct |
10 |
Correct |
25 ms |
17496 KB |
Output is correct |
11 |
Correct |
2 ms |
6236 KB |
Output is correct |
12 |
Correct |
4 ms |
5416 KB |
Output is correct |
13 |
Correct |
3 ms |
5208 KB |
Output is correct |
14 |
Correct |
3 ms |
5212 KB |
Output is correct |
15 |
Correct |
3 ms |
6236 KB |
Output is correct |
16 |
Correct |
3 ms |
6236 KB |
Output is correct |
17 |
Correct |
3 ms |
5212 KB |
Output is correct |
18 |
Correct |
4 ms |
5212 KB |
Output is correct |
19 |
Correct |
3 ms |
5160 KB |
Output is correct |
20 |
Correct |
4 ms |
5368 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
5976 KB |
Output is correct |
2 |
Correct |
2 ms |
5980 KB |
Output is correct |
3 |
Correct |
2 ms |
4956 KB |
Output is correct |
4 |
Correct |
2 ms |
5212 KB |
Output is correct |
5 |
Correct |
39 ms |
18384 KB |
Output is correct |
6 |
Correct |
32 ms |
26248 KB |
Output is correct |
7 |
Correct |
50 ms |
23556 KB |
Output is correct |
8 |
Correct |
26 ms |
17496 KB |
Output is correct |
9 |
Correct |
52 ms |
21752 KB |
Output is correct |
10 |
Correct |
25 ms |
17496 KB |
Output is correct |
11 |
Correct |
2 ms |
5980 KB |
Output is correct |
12 |
Correct |
2 ms |
4956 KB |
Output is correct |
13 |
Correct |
2 ms |
5212 KB |
Output is correct |
14 |
Execution timed out |
1098 ms |
26948 KB |
Time limit exceeded |
15 |
Halted |
0 ms |
0 KB |
- |