이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include <bits/stdc++.h>
using namespace std;
#define mod 1000000007
#define oo 2000000000
const int N = 200010;
vector< pair<int,int> > g[N];
int n , dp[N][2];
pair<int,int> v[N];
int calc(int node,bool b,int prnt,int w){
if(dp[node][b] != -1) return dp[node][b];
dp[node][b] = 0 ;
if(b == 1){
for(int i=0;i<g[node].size();i++){
if(g[node][i].first == prnt) continue;
dp[node][b] += max(calc(g[node][i].first,0,node,g[node][i].second),calc(g[node][i].first,1,node,g[node][i].second));
}
}
else{
int sum = 0 ;
for(int i=0;i<g[node].size();i++){
if(g[node][i].first == prnt){
v[i] = (make_pair(-1,-1));
continue;
}
int cur = calc(g[node][i].first,0,node,g[node][i].second);
int cur2 = calc(g[node][i].first,1,node,g[node][i].second);
v[i] = (make_pair(cur,cur2));
sum += max(cur,cur2);
}
for(int i=0;i<g[node].size();i++){
if(v[i].first == -1) continue;
sum -= max(v[i].first,v[i].second);
sum += w + g[node][i].second + v[i].second;
dp[node][b] = max(dp[node][b],sum);
sum -= (w + g[node][i].second + v[i].second);
sum += max(v[i].first,v[i].second);
}
}
return dp[node][b];
}
int main() {
//freopen("in.txt","r",stdin);
scanf("%d",&n);
for(int i=0;i<n-1;i++){
int u ,v , w;
scanf("%d%d%d",&u,&v,&w);
g[u].push_back(make_pair(v,w));
g[v].push_back(make_pair(u,w));
}
int ans = 0 ;
for(int i=1;i<=n;i++){
if(g[i].size() == 1){
memset(dp,-1,sizeof(dp));
ans = max(ans,calc(i,1,-1,0));
memset(dp,-1,sizeof(dp));
ans = max(ans,calc(g[i][0].first,1,-1,0));
break;
}
}
cout << ans << endl;
return 0;
}
컴파일 시 표준 에러 (stderr) 메시지
beads.cpp: In function 'int calc(int, bool, int, int)':
beads.cpp:14:16: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for(int i=0;i<g[node].size();i++){
~^~~~~~~~~~~~~~~
beads.cpp:21:16: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for(int i=0;i<g[node].size();i++){
~^~~~~~~~~~~~~~~
beads.cpp:31:16: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for(int i=0;i<g[node].size();i++){
~^~~~~~~~~~~~~~~
beads.cpp: In function 'int main()':
beads.cpp:45:7: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%d",&n);
~~~~~^~~~~~~~~
beads.cpp:48:8: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%d%d%d",&u,&v,&w);
~~~~~^~~~~~~~~~~~~~~~~~~
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |