#include <bits/stdc++.h>
#define ll long long
#define fi first
#define se second
#define sz(x) (int)x.size()
#define ALL(v) v.begin(),v.end()
#define MASK(k) (1LL << (k))
#define BIT(x, i) (((x) >> (i)) & 1)
#define oo (ll)1e18
#define INF (ll)1e9
#define MOD (ll)(1e9 + 7)
using namespace std;
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 T1, class T2>
void add(T1 &a, T2 b){a += b; if(a >= MOD) a -= MOD;}
template<class T1, class T2>
void sub(T1 &a, T2 b){a -= b; if(a < 0) a += MOD;}
template<class T>
void cps(T &v){sort(ALL(v)); v.resize(unique(ALL(v)) - v.begin());}
struct fen{
vector<ll> tree;
int n;
fen(int _n = 0){
n = _n;
tree.resize(n + 3, 0);
}
void upd(int p, ll c){
while(p <= n){
tree[p] += c;
p += p & -p;
}
}
ll get(int p){
ll ans = 0;
while(p){
ans += tree[p];
p -= p & -p;
}
return ans;
}
} bit0, bit1;
const int MAX = 1e5 + 10, LOG = 17;
int N, M;
vector<int> adj[MAX];
int h[MAX], d[MAX][LOG], f[MAX][2], timer, in[MAX], out[MAX];
vector<array<int, 3>> plans[MAX];
void dfs(int u, int p = -1){
in[u] = ++timer;
for(int v: adj[u]){
if(v == p) continue;
h[v] = h[u] + 1;
d[v][0] = u;
for(int i = 1; i < LOG; i++) d[v][i] = d[d[v][i - 1]][i - 1];
dfs(v, u);
}
out[u] = timer;
}
int lca(int u, int v){
if(h[u] < h[v]) swap(u, v);
int delta = h[u] - h[v];
for(int i = 0; i < LOG; i++) if(BIT(delta, i)){
u = d[u][i];
}
if(u == v) return u;
for(int i = LOG - 1; i >= 0; i--) if(d[u][i] != d[v][i]){
u = d[u][i];
v = d[v][i];
}
return d[u][0];
}
void dfs2(int u, int p = -1){
for(int v: adj[u]){
if(v == p) continue;
dfs2(v, u);
f[u][0] += f[v][1];
}
bit0.upd(in[u], f[u][0]);
bit0.upd(out[u] + 1, -f[u][0]);
f[u][1] = f[u][0];
for(auto o: plans[u]){
int a = o[0], b = o[1], c = o[2];
ll sum = bit0.get(in[a]) + bit0.get(in[b]) - bit0.get(in[u]) - bit1.get(in[a]) - bit1.get(in[b]);
maximize(f[u][1], sum + c);
}
bit1.upd(in[u], f[u][1]);
bit1.upd(out[u] + 1, -f[u][1]);
}
void solve(){
cin >> N;
for(int i = 1; i < N; i++){
int u, v;
cin >> u >> v;
adj[u].push_back(v);
adj[v].push_back(u);
}
dfs(1);
cin >> M;
for(int i = 1; i <= M; i++){
int u, v, c;
cin >> u >> v >> c;
int p = lca(u, v);
plans[p].push_back({u, v, c});
}
bit0 = fen(N);
bit1 = fen(N);
dfs2(1);
ll ans = 0;
for(int i = 1; i <= N; i++) maximize(ans, f[i][1]);
cout << ans << '\n';
}
int main(){
ios_base::sync_with_stdio(0); cin.tie(0);
// freopen("order.inp","r",stdin);
// freopen("order.out","w",stdout);
solve();
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
4952 KB |
Output is correct |
2 |
Correct |
2 ms |
4956 KB |
Output is correct |
3 |
Correct |
2 ms |
4956 KB |
Output is correct |
4 |
Correct |
2 ms |
5212 KB |
Output is correct |
5 |
Correct |
46 ms |
19652 KB |
Output is correct |
6 |
Correct |
35 ms |
27216 KB |
Output is correct |
7 |
Correct |
57 ms |
24664 KB |
Output is correct |
8 |
Correct |
40 ms |
20048 KB |
Output is correct |
9 |
Correct |
53 ms |
23120 KB |
Output is correct |
10 |
Correct |
43 ms |
20052 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
4956 KB |
Output is correct |
2 |
Correct |
2 ms |
5176 KB |
Output is correct |
3 |
Correct |
2 ms |
5212 KB |
Output is correct |
4 |
Correct |
79 ms |
30536 KB |
Output is correct |
5 |
Correct |
77 ms |
30692 KB |
Output is correct |
6 |
Correct |
69 ms |
30548 KB |
Output is correct |
7 |
Correct |
74 ms |
30584 KB |
Output is correct |
8 |
Correct |
75 ms |
30652 KB |
Output is correct |
9 |
Correct |
66 ms |
30588 KB |
Output is correct |
10 |
Correct |
72 ms |
30548 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
4956 KB |
Output is correct |
2 |
Correct |
2 ms |
5176 KB |
Output is correct |
3 |
Correct |
2 ms |
5212 KB |
Output is correct |
4 |
Correct |
79 ms |
30536 KB |
Output is correct |
5 |
Correct |
77 ms |
30692 KB |
Output is correct |
6 |
Correct |
69 ms |
30548 KB |
Output is correct |
7 |
Correct |
74 ms |
30584 KB |
Output is correct |
8 |
Correct |
75 ms |
30652 KB |
Output is correct |
9 |
Correct |
66 ms |
30588 KB |
Output is correct |
10 |
Correct |
72 ms |
30548 KB |
Output is correct |
11 |
Correct |
8 ms |
5976 KB |
Output is correct |
12 |
Correct |
75 ms |
30836 KB |
Output is correct |
13 |
Correct |
87 ms |
30800 KB |
Output is correct |
14 |
Correct |
86 ms |
31056 KB |
Output is correct |
15 |
Correct |
75 ms |
31132 KB |
Output is correct |
16 |
Correct |
65 ms |
30960 KB |
Output is correct |
17 |
Correct |
77 ms |
30804 KB |
Output is correct |
18 |
Correct |
97 ms |
30800 KB |
Output is correct |
19 |
Correct |
71 ms |
30988 KB |
Output is correct |
20 |
Correct |
81 ms |
30908 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
79 ms |
22380 KB |
Output is correct |
2 |
Correct |
74 ms |
30544 KB |
Output is correct |
3 |
Correct |
113 ms |
27472 KB |
Output is correct |
4 |
Correct |
68 ms |
22796 KB |
Output is correct |
5 |
Correct |
117 ms |
26964 KB |
Output is correct |
6 |
Correct |
98 ms |
22856 KB |
Output is correct |
7 |
Correct |
108 ms |
26764 KB |
Output is correct |
8 |
Correct |
82 ms |
22676 KB |
Output is correct |
9 |
Correct |
66 ms |
30544 KB |
Output is correct |
10 |
Correct |
120 ms |
25864 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
4952 KB |
Output is correct |
2 |
Correct |
2 ms |
4956 KB |
Output is correct |
3 |
Correct |
2 ms |
4956 KB |
Output is correct |
4 |
Correct |
2 ms |
5212 KB |
Output is correct |
5 |
Correct |
46 ms |
19652 KB |
Output is correct |
6 |
Correct |
35 ms |
27216 KB |
Output is correct |
7 |
Correct |
57 ms |
24664 KB |
Output is correct |
8 |
Correct |
40 ms |
20048 KB |
Output is correct |
9 |
Correct |
53 ms |
23120 KB |
Output is correct |
10 |
Correct |
43 ms |
20052 KB |
Output is correct |
11 |
Correct |
3 ms |
5212 KB |
Output is correct |
12 |
Correct |
3 ms |
5212 KB |
Output is correct |
13 |
Correct |
2 ms |
5212 KB |
Output is correct |
14 |
Correct |
2 ms |
5212 KB |
Output is correct |
15 |
Correct |
3 ms |
5208 KB |
Output is correct |
16 |
Correct |
3 ms |
5212 KB |
Output is correct |
17 |
Correct |
3 ms |
5212 KB |
Output is correct |
18 |
Correct |
3 ms |
5172 KB |
Output is correct |
19 |
Correct |
3 ms |
5212 KB |
Output is correct |
20 |
Correct |
3 ms |
5212 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
4952 KB |
Output is correct |
2 |
Correct |
2 ms |
4956 KB |
Output is correct |
3 |
Correct |
2 ms |
4956 KB |
Output is correct |
4 |
Correct |
2 ms |
5212 KB |
Output is correct |
5 |
Correct |
46 ms |
19652 KB |
Output is correct |
6 |
Correct |
35 ms |
27216 KB |
Output is correct |
7 |
Correct |
57 ms |
24664 KB |
Output is correct |
8 |
Correct |
40 ms |
20048 KB |
Output is correct |
9 |
Correct |
53 ms |
23120 KB |
Output is correct |
10 |
Correct |
43 ms |
20052 KB |
Output is correct |
11 |
Correct |
2 ms |
4956 KB |
Output is correct |
12 |
Correct |
2 ms |
5176 KB |
Output is correct |
13 |
Correct |
2 ms |
5212 KB |
Output is correct |
14 |
Correct |
79 ms |
30536 KB |
Output is correct |
15 |
Correct |
77 ms |
30692 KB |
Output is correct |
16 |
Correct |
69 ms |
30548 KB |
Output is correct |
17 |
Correct |
74 ms |
30584 KB |
Output is correct |
18 |
Correct |
75 ms |
30652 KB |
Output is correct |
19 |
Correct |
66 ms |
30588 KB |
Output is correct |
20 |
Correct |
72 ms |
30548 KB |
Output is correct |
21 |
Correct |
8 ms |
5976 KB |
Output is correct |
22 |
Correct |
75 ms |
30836 KB |
Output is correct |
23 |
Correct |
87 ms |
30800 KB |
Output is correct |
24 |
Correct |
86 ms |
31056 KB |
Output is correct |
25 |
Correct |
75 ms |
31132 KB |
Output is correct |
26 |
Correct |
65 ms |
30960 KB |
Output is correct |
27 |
Correct |
77 ms |
30804 KB |
Output is correct |
28 |
Correct |
97 ms |
30800 KB |
Output is correct |
29 |
Correct |
71 ms |
30988 KB |
Output is correct |
30 |
Correct |
81 ms |
30908 KB |
Output is correct |
31 |
Correct |
79 ms |
22380 KB |
Output is correct |
32 |
Correct |
74 ms |
30544 KB |
Output is correct |
33 |
Correct |
113 ms |
27472 KB |
Output is correct |
34 |
Correct |
68 ms |
22796 KB |
Output is correct |
35 |
Correct |
117 ms |
26964 KB |
Output is correct |
36 |
Correct |
98 ms |
22856 KB |
Output is correct |
37 |
Correct |
108 ms |
26764 KB |
Output is correct |
38 |
Correct |
82 ms |
22676 KB |
Output is correct |
39 |
Correct |
66 ms |
30544 KB |
Output is correct |
40 |
Correct |
120 ms |
25864 KB |
Output is correct |
41 |
Correct |
3 ms |
5212 KB |
Output is correct |
42 |
Correct |
3 ms |
5212 KB |
Output is correct |
43 |
Correct |
2 ms |
5212 KB |
Output is correct |
44 |
Correct |
2 ms |
5212 KB |
Output is correct |
45 |
Correct |
3 ms |
5208 KB |
Output is correct |
46 |
Correct |
3 ms |
5212 KB |
Output is correct |
47 |
Correct |
3 ms |
5212 KB |
Output is correct |
48 |
Correct |
3 ms |
5172 KB |
Output is correct |
49 |
Correct |
3 ms |
5212 KB |
Output is correct |
50 |
Correct |
3 ms |
5212 KB |
Output is correct |
51 |
Correct |
81 ms |
22868 KB |
Output is correct |
52 |
Correct |
85 ms |
30892 KB |
Output is correct |
53 |
Correct |
107 ms |
26124 KB |
Output is correct |
54 |
Correct |
65 ms |
23180 KB |
Output is correct |
55 |
Correct |
84 ms |
22796 KB |
Output is correct |
56 |
Correct |
78 ms |
30800 KB |
Output is correct |
57 |
Correct |
93 ms |
26960 KB |
Output is correct |
58 |
Correct |
68 ms |
23048 KB |
Output is correct |
59 |
Correct |
83 ms |
23116 KB |
Output is correct |
60 |
Correct |
89 ms |
30984 KB |
Output is correct |
61 |
Correct |
94 ms |
26944 KB |
Output is correct |
62 |
Correct |
66 ms |
22824 KB |
Output is correct |
63 |
Correct |
79 ms |
22456 KB |
Output is correct |
64 |
Correct |
77 ms |
31056 KB |
Output is correct |
65 |
Correct |
129 ms |
26840 KB |
Output is correct |
66 |
Correct |
68 ms |
23180 KB |
Output is correct |
67 |
Correct |
92 ms |
22608 KB |
Output is correct |
68 |
Correct |
75 ms |
30928 KB |
Output is correct |
69 |
Correct |
94 ms |
25624 KB |
Output is correct |
70 |
Correct |
70 ms |
23352 KB |
Output is correct |