제출 #717214

#제출 시각아이디문제언어결과실행 시간메모리
717214Obada_Saleh구슬과 끈 (APIO14_beads)C++14
0 / 100
3 ms4948 KiB
#include <bits/stdc++.h> using namespace std; int n; vector<pair<long long,int>>adj[200010]; long long dp1[200010],dp2[200010],dp3[200010],dp4[200010]; bool comp(pair<long long,int>x,pair<long long,int>y) { return x.first+dp3[x.second]-dp4[x.second]>y.first+dp3[y.second]-dp4[y.second]; } void dfs(int node=1,int p=0) { for(int i=0;i<adj[node].size();i++) { int neighbour=adj[node][i].second; if(neighbour==p) { adj[node].erase(adj[node].begin()+i); break; } } for(int i=0;i<adj[node].size();i++) { int neighbour=adj[node][i].second; dfs(neighbour,node); } for(int i=0;i<adj[node].size();i++) { long long weight=adj[node][i].first; int neighbour=adj[node][i].second; dp4[neighbour]=dp3[neighbour]; if(dp1[neighbour]!=0) dp4[neighbour]=max(dp4[neighbour],weight+dp1[neighbour]); } sort(adj[node].begin(),adj[node].end(),comp); if(adj[node].size()!=0) { long long weight=adj[node][0].first; int neighbour=adj[node][0].second; dp1[node]=weight+dp3[neighbour]; for(int i=1;i<adj[node].size();i++) { weight=adj[node][i].first; neighbour=adj[node][i].second; dp1[node]+=dp4[neighbour]; } } if(adj[node].size()>=2) { long long weight; int neighbour; for(int i=0;i<2;i++) { weight=adj[node][i].first; neighbour=adj[node][i].second; dp2[node]+=weight+dp3[neighbour]; } for(int i=2;i<adj[node].size();i++) { weight=adj[node][i].first; neighbour=adj[node][i].second; dp2[node]+=dp4[neighbour]; } } for(int i=0;i<adj[node].size();i++) { long long weight=adj[node][i].first; int neighbour=adj[node][i].second; dp3[node]+=dp4[neighbour]; } dp3[node]=max(dp3[node],dp2[node]); } int main() { ios::sync_with_stdio(0); cin.tie(0); cout.tie(0); cin>>n; for(int i=1;i<=n-1;i++) { int u,v; cin>>u>>v; long long w; cin>>w; adj[u].push_back({w,v}); adj[v].push_back({w,u}); } dfs(); cout<<dp3[1]; return 0; }

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

beads.cpp: In function 'void dfs(int, int)':
beads.cpp:12:18: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<long long int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   12 |     for(int i=0;i<adj[node].size();i++)
      |                 ~^~~~~~~~~~~~~~~~~
beads.cpp:21:18: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<long long int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   21 |     for(int i=0;i<adj[node].size();i++)
      |                 ~^~~~~~~~~~~~~~~~~
beads.cpp:26:18: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<long long int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   26 |     for(int i=0;i<adj[node].size();i++)
      |                 ~^~~~~~~~~~~~~~~~~
beads.cpp:40:22: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<long long int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   40 |         for(int i=1;i<adj[node].size();i++)
      |                     ~^~~~~~~~~~~~~~~~~
beads.cpp:57:22: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<long long int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   57 |         for(int i=2;i<adj[node].size();i++)
      |                     ~^~~~~~~~~~~~~~~~~
beads.cpp:64:18: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<long long int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   64 |     for(int i=0;i<adj[node].size();i++)
      |                 ~^~~~~~~~~~~~~~~~~
beads.cpp:66:19: warning: unused variable 'weight' [-Wunused-variable]
   66 |         long long weight=adj[node][i].first;
      |                   ^~~~~~
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...