이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
//
// main.cpp
// apioC
//
// Created by 방성원 on 2015. 8. 16..
// Copyright (c) 2015년 makesource. All rights reserved.
//
#include <stdio.h>
#include <vector>
#include <algorithm>
#include <memory.h>
#define max(a,b) ((a)>(b)?(a):(b))
#define __ 200020
using namespace std;
vector<int> g[__], length[__];
int red[__], blue[__], vis[__], second[__], pos[__];
int n, ans;
void dfs(int node,int parent,int len) {
vis[node] = 1;
for (int i=0;i<g[node].size();i++) {
if (vis[g[node][i]]) continue;
dfs(g[node][i],node,length[node][i]);
}
for (int i=0;i<g[node].size();i++) {
if(g[node][i] == parent) continue;
red[node] += max(red[g[node][i]],blue[g[node][i]]);
}
for (int i=0;i<g[node].size();i++) {
if(g[node][i] == parent) continue;
int value=max(red[g[node][i]],blue[g[node][i]]);
blue[node] = max(blue[node],red[node]-value+red[g[node][i]]+length[node][i]+len);
}
}
void changeRoot(int node,int parent) {
int rr = red[node];
int bb = blue[node];
vis[node] = 1;
red[node] = 0; blue[node] = 0;
for (int i=0;i<g[node].size();i++) {
red[node] += max(red[g[node][i]],blue[g[node][i]]);
}
for (int i=0;i<g[node].size();i++) {
int value=max(red[g[node][i]],blue[g[node][i]]);
if (blue[node] < red[node]-value+red[g[node][i]]+length[node][i]) {
second[node] = max(second[node],blue[node]);
blue[node] = red[node]-value+red[g[node][i]]+length[node][i];
pos[node] = i;
} else {
second[node] = max(second[node],red[node]-value+red[g[node][i]]+length[node][i]);
}
}
int nowRed = red[node];
int nowBlue = blue[node];
ans = max(ans,nowRed);
for (int i=0;i<g[node].size();i++) {
if (vis[g[node][i]]) continue;
red[node] = nowRed; blue[node] = nowBlue;
red[node] -= max(red[g[node][i]],blue[g[node][i]]);
if(pos[node] != i) {
blue[node] -= max(red[g[node][i]],blue[g[node][i]]);
} else {
blue[node] = second[node];
blue[node] -= max(red[g[node][i]],blue[g[node][i]]);
}
blue[node] += length[node][i];
changeRoot(g[node][i],node);
}
red[node] = rr;
blue[node] = bb;
}
int main() {
scanf("%d",&n);
for(int i=1;i<n;i++) {
int a,b,c;
scanf("%d%d%d",&a,&b,&c);
g[a].push_back(b); length[a].push_back(c);
g[b].push_back(a); length[b].push_back(c);
}
dfs(1,-1,0);
memset(vis,0,sizeof vis);
changeRoot(1,-1);
printf("%d",ans);
return 0;
}
컴파일 시 표준 에러 (stderr) 메시지
beads.cpp: In function 'void dfs(int, int, int)':
beads.cpp:22:19: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for (int i=0;i<g[node].size();i++) {
~^~~~~~~~~~~~~~~
beads.cpp:27:19: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for (int i=0;i<g[node].size();i++) {
~^~~~~~~~~~~~~~~
beads.cpp:31:19: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for (int i=0;i<g[node].size();i++) {
~^~~~~~~~~~~~~~~
beads.cpp: In function 'void changeRoot(int, int)':
beads.cpp:44:19: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for (int i=0;i<g[node].size();i++) {
~^~~~~~~~~~~~~~~
beads.cpp:47:19: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for (int i=0;i<g[node].size();i++) {
~^~~~~~~~~~~~~~~
beads.cpp:63:19: 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:85:10: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%d",&n);
~~~~~^~~~~~~~~
beads.cpp:88:14: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%d%d%d",&a,&b,&c);
~~~~~^~~~~~~~~~~~~~~~~~~
# | 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... |