제출 #918513

#제출 시각아이디문제언어결과실행 시간메모리
918513Boycl07구슬과 끈 (APIO14_beads)C++17
57 / 100
7 ms2652 KiB
#include <bits/stdc++.h> using namespace std; typedef long long 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 = 1e4 + 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]; int cnt[N], sum[N]; multiset<int> s[N]; vector<pii> g[N]; void dfs(int u, int p) { dp[u][0] = 0; dp[u][1] = -INF; for(pii e : g[u]) { int v = e.fi, w = e.se; if(v == p) continue; dfs(v, u); ++cnt[u]; dp[u][0] += max(dp[v][0], dp[v][1] + w); } for(pii e : g[u]) { int v = e.fi, w = e.se; if(v == p) continue; int diff = dp[v][0] + w - max(dp[v][0], dp[v][1] + w); s[u].insert(diff); } if(cnt[u] >= 1) dp[u][1] = dp[u][0] + *(--s[u].end()); else dp[u][1] = -INF; } void change(int u, int v, int w) { int diff; --cnt[u]; dp[u][0] -= max(dp[v][0], dp[v][1] + w); diff = dp[v][0] + w - max(dp[v][0], dp[v][1] + w); s[u].erase(s[u].find(diff)); if(cnt[u] >= 1) dp[u][1] = dp[u][0] + *(--s[u].end()); else dp[u][1] = -INF; ++cnt[v]; dp[v][0] += max(dp[u][0], dp[u][1] + w); diff = dp[u][0] + w - max(dp[u][0], dp[u][1] + w); s[v].insert(diff); if(cnt[v] >= 1) dp[v][1] = dp[v][0] + *(--s[v].end()); else dp[v][1] = -INF; } int res = 0; void dfs_reroot(int u, int p) { maximize(res, dp[u][0]); for(pii e : g[u]) { int v = e.fi, w = e.se; if(v == p) continue; change(u, v, w); dfs_reroot(v, u); change(v, u, w); } } 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}); } dfs(1, 1); dfs_reroot(1, 1); cout << res; } 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"_slow.out", "w", stdout); } while(TC--) { solve(); cout << endl; } return 0; }

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

beads.cpp: In function 'int main()':
beads.cpp:131:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
  131 |         freopen(task".inp", "r", stdin);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
beads.cpp:132:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
  132 |         freopen(task"_slow.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...