This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
//
// 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;
}
Compilation message (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... |