Submission #83175

#TimeUsernameProblemLanguageResultExecution timeMemory
83175RezwanArefin01Beads and wires (APIO14_beads)C++17
28 / 100
1079 ms10112 KiB
///usr/bin/g++ -O2 $0 -o ${0%.cpp} && echo "----------" && ./${0%.cpp}; exit; #include <bits/stdc++.h> using namespace std; typedef long long ll; const int N = 2e5 + 10; vector<int> adj[N], cost[N]; ll dp[N][2]; ll f(int u, int par, bool c) { if(par && adj[u].size() == 1) { if(c) return -1e18; else return 0; } ll &ret = dp[u][c]; if(~ret) return ret; ret = 0; ll sum = 0; for(int i = 0; i < adj[u].size(); i++) { int v = adj[u][i], c = cost[u][i]; if(v == par) continue; sum += max(f(v, u, 0), f(v, u, 1) + c); } if(!c) return ret = sum; for(int i = 0; i < adj[u].size(); i++) { int v = adj[u][i], c = cost[u][i]; if(v == par) continue; ll cont = max(f(v, u, 0), f(v, u, 1) + c); ret = max(ret, sum - cont + f(v, u, 0) + c); } return ret; } int main() { int n; scanf("%d", &n); for(int i = 1; i < n; i++) { int u, v, c; scanf("%d %d %d", &u, &v, &c); adj[u].push_back(v); adj[v].push_back(u); cost[u].push_back(c); cost[v].push_back(c); } ll ans = 0; for(int i = 1; i <= n; i++) { for(int j = 1; j <= n; j++) dp[j][0] = dp[j][1] = -1; ans = max(ans, f(i, 0, 0)); } printf("%lld\n", ans); }

Compilation message (stderr)

beads.cpp: In function 'll f(int, int, bool)':
beads.cpp:21:22: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
     for(int i = 0; i < adj[u].size(); i++) {
                    ~~^~~~~~~~~~~~~~~
beads.cpp:29:22: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
     for(int i = 0; i < adj[u].size(); i++) {
                    ~~^~~~~~~~~~~~~~~
beads.cpp: In function 'int main()':
beads.cpp:39:17: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
     int n; scanf("%d", &n);
            ~~~~~^~~~~~~~~~
beads.cpp:42:14: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
         scanf("%d %d %d", &u, &v, &c); 
         ~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...