Submission #1125491

#TimeUsernameProblemLanguageResultExecution timeMemory
1125491adaawfElection Campaign (JOI15_election_campaign)C++20
100 / 100
308 ms84872 KiB
#include <iostream> #include <vector> #include <unordered_map> using namespace std; vector<int> g[100005], vv[100005], v[100005]; long long int l[100005][18], d[100005], f[100005], n, c[100005], dd[100005]; unordered_map<int, long long int> s[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 (int w : vv[x]) s[x][w] = 0; for (int w : g[x]) { if (w == p) continue; if (s[x].size() < s[w].size()) { s[x].swap(s[w]); swap(dd[x], dd[w]); } for (auto v : s[w]) { long long int h = v.second + dd[w]; if (s[x].count(v.first)) s[x][v.first] += h; else s[x][v.first] += h - dd[x]; } } dd[x] += f[x]; for (int w : v[x]) { f[x] = max(f[x], s[x][w] + dd[x] + c[w]); } dd[x] -= f[x]; } 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; cin >> x >> y >> c[i]; v[lca(x, y)].push_back(i); vv[x].push_back(i); vv[y].push_back(i); } 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...