Submission #665750

#TimeUsernameProblemLanguageResultExecution timeMemory
665750AmirAli_H1Beads and wires (APIO14_beads)C++17
0 / 100
4 ms5088 KiB
// In the name of Allah #include <bits/stdc++.h> using namespace std; typedef long long int ll; typedef long double ld; typedef pair<int, int> pii; typedef pair<ll, ll> pll; #define all(x) (x).begin(),(x).end() #define len(x) ((ll) (x).size()) #define F first #define S second #define pb push_back #define sep ' ' #define endl '\n' #define Mp make_pair #define debug(x) cerr << #x << ": " << x << endl; #define kill(x) cout << x << '\n', exit(0); #define set_dec(x) cout << fixed << setprecision(x); #define file_io(x,y) freopen(x, "r", stdin); freopen(y, "w", stdout); int n; const int maxn = 2e5 + 5; vector<pll> adj[maxn]; ll dp[maxn][2]; bool mark[maxn]; void dfs(int v, ll x = -1) { mark[v] = 1; vector<ll> ls; for (auto f : adj[v]) { int u = f.F; ll w = f.S; if (!mark[u]) { dfs(u, w); dp[v][0] += dp[u][1]; dp[v][1] += dp[u][1]; ls.pb((dp[u][0] + w) - dp[u][1]); } } sort(all(ls), greater<ll>()); if (len(ls) >= 2 && ls[0] + ls[1] > 0) dp[v][0] += (ls[0] + ls[1]); if (len(ls) >= 1 && x != -1 && x + ls[0] > 0) dp[v][1] += (x + ls[0]); dp[v][1] = max(dp[v][1], dp[v][0]); ls.clear(); } int main() { ios::sync_with_stdio(false); cin.tie(0); cout.tie(0); file_io("beads.in", "beads.out"); cin >> n; for (int i = 0; i < n - 1; i++) { int u, v; ll w; cin >> u >> v >> w; u--; v--; adj[u].pb(Mp(v, w)); adj[v].pb(Mp(u, w)); } dfs(0); cout << dp[0][1] << endl; return 0; }

Compilation message (stderr)

beads.cpp: In function 'int main()':
beads.cpp:22:29: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   22 | #define file_io(x,y) freopen(x, "r", stdin); freopen(y, "w", stdout);
      |                      ~~~~~~~^~~~~~~~~~~~~~~
beads.cpp:52:2: note: in expansion of macro 'file_io'
   52 |  file_io("beads.in", "beads.out");
      |  ^~~~~~~
beads.cpp:22:53: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   22 | #define file_io(x,y) freopen(x, "r", stdin); freopen(y, "w", stdout);
      |                                              ~~~~~~~^~~~~~~~~~~~~~~~
beads.cpp:52:2: note: in expansion of macro 'file_io'
   52 |  file_io("beads.in", "beads.out");
      |  ^~~~~~~
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...