제출 #96168

#제출 시각아이디문제언어결과실행 시간메모리
96168Retro3014구슬과 끈 (APIO14_beads)C++17
0 / 100
6 ms4992 KiB
#include <iostream> #include <vector> #include <algorithm> #include <stdio.h> #define MAX_N 200000 #define INF 2000000000LL using namespace std; typedef long long ll; int N; int ans; struct S{ S (int idx, ll data) : idx(idx), data(data) {} int idx;ll data; }; vector<S> gp[MAX_N+1]; vector<S> p; ll dp[MAX_N+1][2]; void dfs(int x){ for(int i=0; i<gp[x].size(); i++){ if(gp[x][i].idx!=p[x].idx){ p[gp[x][i].idx].idx = x; p[gp[x][i].idx].data = gp[x][i].data; dfs(gp[x][i].idx); } } ll mx = 0; for(int i=0; i<gp[x].size(); i++){ if(gp[x][i].idx==p[x].idx) continue; mx += max(dp[gp[x][i].idx][0], dp[gp[x][i].idx][1]); } dp[x][0] = mx; dp[x][1] = -INF; ll sum = mx; bool tf = false; mx = 0; for(int i=0; i<gp[x].size(); i++){ if(gp[x][i].idx==p[x].idx) continue; if(!tf){ tf = true; mx = dp[gp[x][i].idx][0]-max(dp[gp[x][i].idx][0], dp[gp[x][i].idx][1])+gp[x][i].data; }else{ dp[x][0] = max(dp[x][0], sum+mx+(dp[gp[x][i].idx][0]-max(dp[gp[x][i].idx][0], dp[gp[x][i].idx][1])+gp[x][i].data)); mx = max(mx, dp[gp[x][i].idx][0]-max(dp[gp[x][i].idx][0], dp[gp[x][i].idx][1])+gp[x][i].data); } dp[x][1] = max(dp[x][1], sum + p[x].data + (dp[gp[x][i].idx][0]-max(dp[gp[x][i].idx][0], dp[gp[x][i].idx][1])+gp[x][i].data)); } return; } int main(){ scanf("%d", &N); for(int i=0; i<=N; i++) p.push_back({0, 0}); for(int i=0; i<N-1; i++){ int a, b;ll c; scanf("%d%d%lld", &a, &b, &c); gp[a].push_back({b, c}); gp[b].push_back({a, c}); } dfs(1); printf("%lld", dp[1][0]); return 0; }

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

beads.cpp: In function 'void dfs(int)':
beads.cpp:22:16: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
  for(int i=0; i<gp[x].size(); i++){
               ~^~~~~~~~~~~~~
beads.cpp:30:16: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
  for(int i=0; i<gp[x].size(); i++){
               ~^~~~~~~~~~~~~
beads.cpp:39:16: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
  for(int i=0; i<gp[x].size(); i++){
               ~^~~~~~~~~~~~~
beads.cpp: In function 'int main()':
beads.cpp:55:7: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
  scanf("%d", &N);
  ~~~~~^~~~~~~~~~
beads.cpp:59:8: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
   scanf("%d%d%lld", &a, &b, &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...