이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
/*input
5
1 2 10
1 3 40
1 4 15
1 5 20
10
4 10 2
1 2 21
1 3 13
6 7 1
7 9 5
2 4 3
2 5 8
1 6 55
6 8 34
*/
#include <algorithm>
#include <bitset>
#include <cassert>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <fstream>
#include <functional>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <utility>
#include <vector>
using namespace std;
#define sp ' '
#define endl '\n'
#define fi first
#define se second
#define mp make_pair
#define int long long
#define N 200005
int n;
vector<vector<pair<int, int> > > a(N);
int wpar[N];
int dp[N][2];
void dfs(int u, int p) {
for (int i = 0; i < a[u].size(); i++) {
int v = a[u][i].fi; if (v == p) continue;
dfs(v, u); wpar[v] = a[u][i].se;
}
}
int cal(int u, int p, bool up) {
int &ret = dp[u][up];
if (ret != -1) return ret;
ret = 0;
int sum = 0;
for (int i = 0; i < a[u].size(); i++) {
int v = a[u][i].fi; if (v == p) continue;
sum += cal(v, u, 0);
}
ret = max(ret, sum);
if (!up) {
for (int i = 0; i < a[u].size(); i++) {
int v = a[u][i].fi; if (v == p) continue;
ret = max(ret, sum - cal(v, u, 0) + cal(v, u, 1) + a[u][i].se + wpar[u]);
}
}
return ret;
}
signed main() {
ios_base::sync_with_stdio(false); cin.tie(0);
cin >> n;
for (int i = 1; i <= n - 1; i++) {
int u, v, c; cin >> u >> v >> c;
a[u].push_back(mp(v, c));
a[v].push_back(mp(u, c));
}
int ans = 0;
for (int i = 1; i <= n; i++) {
memset(dp, -1, sizeof(dp));
dfs(i, i);
ans = max(ans, cal(i, i, 1));
}
cout << ans << endl;
}
컴파일 시 표준 에러 (stderr) 메시지
beads.cpp: In function 'void dfs(long long int, long long int)':
beads.cpp:54:20: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for (int i = 0; i < a[u].size(); i++) {
~~^~~~~~~~~~~~~
beads.cpp: In function 'long long int cal(long long int, long long int, bool)':
beads.cpp:65:20: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for (int i = 0; i < a[u].size(); i++) {
~~^~~~~~~~~~~~~
beads.cpp:71:21: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for (int i = 0; i < a[u].size(); i++) {
~~^~~~~~~~~~~~~
# | 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... |