Submission #1125465

#TimeUsernameProblemLanguageResultExecution timeMemory
1125465adaawfElection Campaign (JOI15_election_campaign)C++20
20 / 100
1097 ms35896 KiB
#include <iostream> #include <vector> using namespace std; vector<int> g[100005]; vector<pair<pair<int, int>, int>> v[100005]; long long int l[100005][18], d[100005], f[100005], n, dd[100005]; void dfs(int x, int p) { l[x][0] = p; for (int w : g[x]) { if (w == p) continue; d[w] = d[x] + 1; dfs(w, x); } } int lca(int x, int y) { if (d[x] < d[y]) swap(x, y); int k = d[x] - d[y]; for (int i = 17; i >= 0; i--) { if (k & (1 << i)) { x = l[x][i]; } } if (x == y) return x; for (int i = 17; i >= 0; i--) { if (l[x][i] != l[y][i]) { x = l[x][i]; y = l[y][i]; } } return l[x][0]; } void dfs2(int x, int p) { long long int res = 0; for (int w : g[x]) { if (w == p) continue; dfs2(w, x); res += f[w]; } f[x] = max(f[x], res); for (auto w : v[x]) { long long int h = w.first.first, k = w.first.second, res = 0; while (h != x) { dd[h] = 1; h = l[h][0]; } while (k != x) { dd[k] = 1; k = l[k][0]; } dd[x] = 1; for (int i = 1; i <= n; i++) { if (dd[i] == 0 && dd[l[i][0]] == 1) { res += f[i]; } } f[x] = max(f[x], res + w.second); for (int i = 1; i <= n; i++) { dd[i] = 0; } } } int main() { ios::sync_with_stdio(0); cin.tie(0); cout.tie(0); cin >> n; for (int i = 1; i < n; i++) { int u, v; cin >> u >> v; g[u].push_back(v); g[v].push_back(u); } dfs(1, 0); for (int i = 1; i <= 17; i++) { for (int j = 1; j <= n; j++) { l[j][i] = l[l[j][i - 1]][i - 1]; } } int m; cin >> m; for (int i = 1; i <= m; i++) { int x, y, c; cin >> x >> y >> c; v[lca(x, y)].push_back({{x, y}, c}); } dfs2(1, -1); cout << f[1]; }
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...