제출 #918508

#제출 시각아이디문제언어결과실행 시간메모리
918508Boycl07구슬과 끈 (APIO14_beads)C++17
0 / 100
4 ms8284 KiB
#include <bits/stdc++.h> using namespace std; typedef long long ll; #define int ll #define rep(i, n) for(int i = 1; i <= n; ++i) #define forn(i, l, r) for(int i = l; i <= r; ++i) #define ford(i, r, l) for(int i = r; i >= l; --i) #define FOR(i, n) for(int i = 0; i < n; ++i) #define fi first #define se second #define pii pair<int, int> #define pll pair<ll, ll> #define pb push_back #define endl "\n" #define task "beads" #define sz(a) int(a.size()) #define C(x, y) make_pair(x, y) #define all(a) (a).begin(), (a).end() #define bit(i, mask) (mask >> i & 1) template<typename T> bool maximize(T &res, const T &val) { if (res < val){ res = val; return true; }; return false; } template<typename T> bool minimize(T &res, const T &val) { if (res > val){ res = val; return true; }; return false; } inline int readInt() {char c;while(c=getchar(),c!='-'&&(c<'0'||c>'9'));bool sign=(c=='-');if(sign)c=getchar();int n=c-'0';while(c=getchar(),c>='0'&&c<='9')n=10*n+c-'0';return(!sign)?n:-n;} inline ll readLong() {char c;while(c=getchar(),c!='-'&&(c<'0'||c>'9'));bool sign=(c=='-');if(sign)c=getchar();ll n=c-'0';while(c=getchar(),c>='0'&&c<='9')n=10*n+c-'0';return(!sign)?n:-n;} inline string readString() {char c;while(c=getchar(),c==' '||c=='\n'||c=='\t');string s({c});while(c=getchar(),c!=EOF&&c!=' '&&c!='\n'&&c!='\t')s+=c;return s;} const int N = 2e5 + 4; const int M = 3e5 + 3; const int K = 22; const int INF = 1e9 + 3; const ll LINF = 1e18; int n; int dp[N][2]; vector<pii> g[N]; int dfs(int u, int p, bool c) { int &res = dp[u][c]; if(res != -1) return res; res = -INF; int sum = 0; for(pii e : g[u]) { int v = e.fi, w = e.se; if(v == p) continue; sum += max(dfs(v, u, 0), dfs(v, u, 1) + w); } int Max1 = -INF, Max2 = -INF, cnt = 0; for(pii e : g[u]) { int v = e.fi, w = e.se; if(v == p) continue; ++cnt; int diff = dfs(v, u, 0) + w - max(dfs(v, u, 0), dfs(v, u, 1) + w); if(diff > Max1) Max2 = Max1, Max1 = diff; else maximize(Max2, diff); } if(c == 0) maximize(res, sum); if(cnt >= 2 && c == 0) maximize(res, sum + Max1 + Max2); if(cnt >= 1 && c == 1) maximize(res, sum + Max1); return res; } void solve() { cin >> n; rep(i, n - 1) { int u, v, w; cin >> u >> v >> w; g[u].pb({v, w}); g[v].pb({u, w}); } memset(dp, -1, sizeof dp); cout << dfs(1, 1, 0); } signed main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); int TC = 1; if(fopen(task".inp", "r")) { freopen(task".inp", "r", stdin); freopen(task".out", "w", stdout); } while(TC--) { solve(); cout << endl; } return 0; }

컴파일 시 표준 에러 (stderr) 메시지

beads.cpp: In function 'int main()':
beads.cpp:100:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
  100 |         freopen(task".inp", "r", stdin);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
beads.cpp:101:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
  101 |         freopen(task".out", "w", stdout);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...