Submission #39763

#TimeUsernameProblemLanguageResultExecution timeMemory
3976314kgBeads and wires (APIO14_beads)C++11
0 / 100
6 ms4992 KiB
#include <stdio.h> #include <vector> #include <algorithm> #define N 200001 #define INF 999999999999999999 using namespace std; int n; long long dp[N][4]; bool check[N][4]; vector<pair<int, long long> > r[N]; long long max2(long long x, long long y) { return x > y ? x : y; } long long f(int x, int y, int up) { long long MAX1 = -INF, MAX2 = -INF; long long _MAX1 = -INF, _MAX2 = -INF; int k1 = 0, k2 = 0; if (!check[x][y]) { check[x][y] = true; if (y == 1) { for (auto i : r[x]) if (i.first != up) { long long Push = max2(f(i.first, 1, x) + i.second, f(i.first, 0, x)); long long temp = i.second + f(i.first, 0, x) - Push; if (MAX1 < temp) MAX1 = temp; dp[x][y] += Push; } dp[x][y] += MAX1; } else if (y == 3) { for (auto i : r[x]) if (i.first != up) { long long Push = max2(f(i.first, 3, x) + i.second, f(i.first, 2, x)); long long temp = i.second + f(i.first, 3, x) - Push; if (MAX1 < temp) MAX2 = MAX1, MAX1 = temp; else if (MAX2 < temp) MAX2 = temp; dp[x][y] += Push; } dp[x][y] += MAX1; } else if (y == 0) { for (auto i : r[x]) if (i.first != up) { long long Push = max2(f(i.first, 1, x) + i.second, f(i.first, 0, x)); long long temp = i.second + f(i.first, 0, x) - Push; long long _temp = i.second + f(i.first, 2, x) - Push; if (MAX1 < temp) MAX2 = MAX1, MAX1 = temp, k1=i.first; else if (MAX2 < temp) MAX2 = temp; if (_MAX1 < _temp) _MAX2 = _MAX1, _MAX1 = _temp, k2=i.first; else if (_MAX2 < _temp) _MAX2 = _temp; dp[x][y] += Push; } if (k1 != k2) dp[x][y] = max2(dp[x][y], dp[x][y] + MAX1 + _MAX1); else dp[x][y] = max2(dp[x][y], max2(dp[x][y] + MAX1 + _MAX2, dp[x][y] + MAX2 + _MAX1)); } else if (y == 2) { for (auto i : r[x]) if (i.first != up) { long long Push = max2(f(i.first, 3, x) + i.second, f(i.first, 2, x)); long long temp = i.second + f(i.first, 0, x) - Push; if (MAX1 < temp) MAX2 = MAX1, MAX1 = temp; else if (MAX2 < temp) MAX2 = temp; dp[x][y] += Push; } } } return dp[x][y]; } int main() { freopen("input.txt", "r", stdin); int x, y, z; scanf("%d", &n); for (int i = 1; i < n; i++) { scanf("%d %d %d", &x, &y, &z); r[x].push_back({ y,(long long)z }), r[y].push_back({ x,(long long)z }); } printf("%lld", f(4, 0, 0)); }

Compilation message (stderr)

beads.cpp: In function 'int main()':
beads.cpp:71:10: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)', declared with attribute warn_unused_result [-Wunused-result]
   freopen("input.txt", "r", stdin);
   ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~
beads.cpp:74:7: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
  scanf("%d", &n);
  ~~~~~^~~~~~~~~~
beads.cpp:76:8: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
   scanf("%d %d %d", &x, &y, &z);
   ~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...