Submission #565310

#TimeUsernameProblemLanguageResultExecution timeMemory
565310SSRSElection Campaign (JOI15_election_campaign)C++14
20 / 100
524 ms262144 KiB
#include <bits/stdc++.h> using namespace std; int main(){ int N; cin >> N; vector<vector<int>> E(N); for (int i = 0; i < N - 1; i++){ int X, Y; cin >> X >> Y; X--; Y--; E[X].push_back(Y); E[Y].push_back(X); } int M; cin >> M; vector<int> A(M), B(M), C(M); for (int i = 0; i < M; i++){ cin >> A[i] >> B[i] >> C[i]; A[i]--; B[i]--; } vector<int> p(N, -1); vector<vector<int>> c(N); vector<int> d(N, 0); queue<int> Q; Q.push(0); vector<int> bfs; while (!Q.empty()){ int v = Q.front(); Q.pop(); bfs.push_back(v); for (int w : E[v]){ if (w != p[v]){ p[w] = v; c[v].push_back(w); d[w] = d[v] + 1; Q.push(w); } } } vector<vector<int>> path(M); vector<vector<int>> id(N); for (int i = 0; i < M; i++){ int u = A[i], v = B[i]; while (d[u] > d[v]){ path[i].push_back(u); u = p[u]; } while (d[v] > d[u]){ path[i].push_back(v); v = p[v]; } while (u != v){ path[i].push_back(u); path[i].push_back(v); u = p[u]; v = p[v]; } path[i].push_back(u); id[u].push_back(i); } reverse(bfs.begin(), bfs.end()); vector<int> dp(N, 0); vector<int> dpsum(N, 0); for (int v : bfs){ for (int w : c[v]){ dpsum[v] += dp[w]; } int tmp = dpsum[v]; for (int w : id[v]){ vector<bool> in_path(N, false); int sum = 0; for (int x : path[w]){ sum += dpsum[x] - dp[x]; } tmp = max(tmp, sum + C[w]); } dp[v] = tmp; } cout << dp[0] << endl; }
#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...