This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <bits/stdc++.h>
using namespace std;
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;
template<class T> using oset =
tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;
#define ll long long
#define ld long double
#define ar array
#define vi vector<int>
#define vii vector<vector<int>>
#define pii pair<int, int>
#define pb push_back
#define all(x) x.begin(), x.end()
#define f first
#define s second
#define endl "\n"
const int MOD = 1e9+7;
const int inf = 1e9;
const ll linf = 1e18;
const int d4i[4]={-1, 0, 1, 0}, d4j[4]={0, 1, 0, -1};
const int d8i[8]={-1, -1, 0, 1, 1, 1, 0, -1}, d8j[8]={0, 1, 1, 1, 0, -1, -1, -1};
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
// -------------------------------------------------- Main Code --------------------------------------------------
struct fenTree {
int n;
vi tree;
fenTree(int N) {n = N+5; tree = vi(n+5);}
void update(int idx, int val) {for (idx; idx <= n; idx += (idx&-idx)) tree[idx] += val;}
int sum(int idx) {int ans = 0; for (idx; idx; idx -= (idx&-idx)) ans += tree[idx]; return ans;}
};
const int N = 1e5+5;
vi g[N];
int parent[N][25], tin[N], tout[N], Time = 0, depth[N], dp[N][2];
vector<array<int, 3>> arr[N];
fenTree Tree(N);
void dfs2(int src, int par) {
tin[src] = ++Time;
parent[src][0] = par;
for (int j = 1; j < 25; j++) parent[src][j] = parent[parent[src][j-1]][j-1];
for (auto ch : g[src]) {
if (ch == par) continue;
depth[ch] = depth[src]+1;
dfs2(ch, src);
}
tout[src] = ++Time;
}
int lca(int a, int b) {
if (depth[a] > depth[b]) swap(a, b);
int diff = depth[b]-depth[a];
for (int j = 0; j < 25; j++) {
if (diff&(1<<j)) b = parent[b][j];
}
for (int j = 24; j >= 0; j--) if (parent[a][j] != parent[b][j]) a = parent[a][j], b = parent[b][j];
if (a == b) return a;
return parent[a][0];
}
void dfs(int src, int par) {
for (auto ch : g[src]) {
if (ch == par) continue;
dfs(ch, src);
dp[src][0] += dp[ch][1];
}
dp[src][1] = dp[src][0];
for (auto x : arr[src]) {
int a = x[0], b = x[1], votes = x[2];
dp[src][1] = max(dp[src][1], dp[src][0] + Tree.sum(tin[a]) + Tree.sum(tin[b]) + votes);
}
Tree.update(tin[src], dp[src][0]-dp[src][1]);
Tree.update(tout[src], dp[src][1]-dp[src][0]);
}
void sol() {
int n; cin >> n;
for (int i = 1; i <= n-1; i++) {
int a, b; cin >> a >> b;
g[a].pb(b);
g[b].pb(a);
}
dfs2(1, -1);
int m; cin >> m;
for (int i = 1; i <= m; i++) {
int a, b, c; cin >> a >> b >> c;
int l = lca(a, b);
arr[l].pb({a, b, c});
}
dfs(1, -1);
cout << max(dp[1][0], dp[1][1]) << endl;
}
int main () {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
int t = 1;
// cin >> t;
while (t--) {
sol();
}
return 0;
}
Compilation message (stderr)
election_campaign.cpp: In member function 'void fenTree::update(int, int)':
election_campaign.cpp:41:41: warning: statement has no effect [-Wunused-value]
41 | void update(int idx, int val) {for (idx; idx <= n; idx += (idx&-idx)) tree[idx] += val;}
| ^~~
election_campaign.cpp: In member function 'int fenTree::sum(int)':
election_campaign.cpp:42:41: warning: statement has no effect [-Wunused-value]
42 | int sum(int idx) {int ans = 0; for (idx; idx; idx -= (idx&-idx)) ans += tree[idx]; return ans;}
| ^~~
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |