이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
//khodaya khodet komak kon
#include <bits/stdc++.h>
#define F first
#define S second
#define pb push_back
#define all(x) x.begin(), x.end()
#pragma GCC optimise ("ofast")
#pragma GCC optimise("unroll-loops")
#define int long long
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
typedef vector<int> vi;
const int N = 200000 + 10;
const ll MOD = 1000000000 + 7;
const ll INF = 1000000000000000000;
const ll LOG = 25;
int dp[N][2], n;
vector<pii> G[N];
void DFS(int v = 1, int p = 1){
ll sm = 0;
ll sm2 = 0;
if (G[v].size() == 1 && v != 1) dp[v][1] = -INF;
for (auto u:G[v]){
if (u.F == p) continue;
DFS(u.F, v);
sm += max(dp[u.F][0], dp[u.F][1] + u.S);
}
for (auto u:G[v]){
if (u.F == p) continue;
dp[v][1] = max(dp[v][1], sm - max(dp[u.F][0], dp[u.F][1] + u.S) + u.S + dp[u.F][0]);
}
for (auto u:G[v]){
for (auto x:G[v]){
if (u.F == p || x.F == p) continue;
if (u.F == x.F) continue;
dp[v][0] = max(dp[v][0], sm - max(dp[u.F][0], dp[u.F][1] + u.S) - max(dp[x.F][0], dp[x.F][1] + x.S) + dp[u.F][0] + u.S + dp[x.F][0] + x.S);
}
}
//cout << v << ' ' << dp[v][0] << ' ' << dp[v][1] << '\n';
}
int32_t main(){
ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
cin >> n;
for (int i = 0; i < n - 1; i++){
int v, u, c;
cin >> v >> u >> c;
G[v].pb({u, c});
G[u].pb({v, u});
}
DFS();
cout << dp[1][0];
return 0;
}
컴파일 시 표준 에러 (stderr) 메시지
beads.cpp:8:0: warning: ignoring #pragma GCC optimise [-Wunknown-pragmas]
#pragma GCC optimise ("ofast")
beads.cpp:9:0: warning: ignoring #pragma GCC optimise [-Wunknown-pragmas]
#pragma GCC optimise("unroll-loops")
beads.cpp: In function 'void DFS(long long int, long long int)':
beads.cpp:28:5: warning: unused variable 'sm2' [-Wunused-variable]
ll sm2 = 0;
^~~
# | 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... |