Submission #347743

#TimeUsernameProblemLanguageResultExecution timeMemory
347743parsabahramiBeads and wires (APIO14_beads)C++17
0 / 100
4 ms4972 KiB
// Call my Name and Save me from The Dark #include <bits/stdc++.h> using namespace std; typedef long long int ll; typedef pair<int, int> pii; #define SZ(x) (int) x.size() #define F first #define S second const int N = 2e5 + 10; ll dp[2][N]; int n; vector<pair<int, ll>> adj[N]; void DFS(int v, int p = -1) { dp[0][v] = 0, dp[1][v] = -1e18; for (pair<int, ll> u : adj[v]) { if (u.F != p) DFS(u.F, v); } ll sum = 0; for (pair<int, ll> u : adj[v]) if (u.F != p) { dp[0][v] = max(dp[0][v] + max(dp[0][u.F], dp[1][u.F] + u.S), dp[1][v] + dp[0][u.F] + u.S); dp[1][v] = max(dp[1][v] + max(dp[0][u.F], dp[1][u.F] + u.S), sum + dp[0][u.F] + u.S); sum += max(dp[0][u.F], dp[1][u.F] + u.S); } dp[0][v] = max(dp[0][v], sum); //printf("%d %d %d\n", v, dp[0][v], dp[1][v]); } int main() { scanf("%d", &n); for (int i = 1; i < n; i++) { int u, v; ll w; scanf("%d%d%lld", &u, &v, &w); adj[u].push_back({v, w}); adj[v].push_back({u, w}); } DFS(1); printf("%lld\n", dp[0][1]); return 0; }

Compilation message (stderr)

beads.cpp: In function 'int main()':
beads.cpp:34:10: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
   34 |     scanf("%d", &n);
      |     ~~~~~^~~~~~~~~~
beads.cpp:36:30: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
   36 |         int u, v; ll w; scanf("%d%d%lld", &u, &v, &w);
      |                         ~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...