이 제출은 이전 버전의 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] , q[N], in[N];
pair<int,int> v[N];
int calc(int node){
int st = 0 , en = 0 , curnode , w , sum = 0;
for(int i=1;i<=n;i++){
in[i] = g[i].size() - (i != node);
if(in[i] == 0)
q[en++] = i;
}
while(st < en){
curnode = q[st++];
dp[curnode][1] = dp[curnode][0] = 0;
sum = 0 ;
for(int i=0;i<g[curnode].size();i++){
if(in[g[curnode][i].first] != 0){
w = g[curnode][i].second;
in[g[curnode][i].first]--;
if(in[g[curnode][i].first] == 0){
q[en++] = g[curnode][i].first;
}
v[i] = make_pair(-1,-1);
}
else{
v[i] = make_pair(dp[g[curnode][i].first][0],dp[g[curnode][i].first][1]);
sum += max(v[i].first,v[i].second);
}
}
dp[curnode][1] = sum;
for(int i=0;i<g[curnode].size();i++){
if(v[i].first == -1) continue;
sum -= max(v[i].first,v[i].second);
sum += w + g[curnode][i].second + v[i].second;
dp[curnode][0] = max(dp[curnode][0],sum);
sum += max(v[i].first,v[i].second);
sum -= (w + g[curnode][i].second + v[i].second);
}
}
return dp[node][1];
}
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){
ans = max(ans,calc(i));
}
}
printf("%d\n",ans);
return 0;
}
컴파일 시 표준 에러 (stderr) 메시지
beads.cpp: In function 'int calc(int)':
beads.cpp:21:16: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for(int i=0;i<g[curnode].size();i++){
~^~~~~~~~~~~~~~~~~~
beads.cpp:36:16: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for(int i=0;i<g[curnode].size();i++){
~^~~~~~~~~~~~~~~~~~
beads.cpp: In function 'int main()':
beads.cpp:51:7: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%d",&n);
~~~~~^~~~~~~~~
beads.cpp:54: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);
~~~~~^~~~~~~~~~~~~~~~~~~
beads.cpp: In function 'int calc(int)':
beads.cpp:42:14: warning: 'w' may be used uninitialized in this function [-Wmaybe-uninitialized]
sum -= (w + g[curnode][i].second + v[i].second);
~~^~~~~~~~~~~~~~~~~~~~~~
# | 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... |