이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include <bits/stdc++.h>
#define ll long long
#define pb push_back
#define mp make_pair
#define f first
#define s second
#define FIO ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0);
#define pll pair < ll, ll >
using namespace std;
const int N = 2*1e5+10;
int n;
vector < pll > edges[N];
vector < ll > takeb[N];
pll nastavi[N];
void dfs( int pos, int par ) {
ll paran = 0, neparan = -1e9, total = 0, parval = 0, cur = 0;
for(int i = 0; i < (int) edges[pos].size(); i++) {
if(edges[pos][i].f != par) {
dfs(edges[pos][i].f, pos);
total += nastavi[edges[pos][i].f].f;
takeb[pos].pb(nastavi[edges[pos][i].f].s - nastavi[edges[pos][i].f].f + edges[pos][i].s);
}
else parval = edges[pos][i].s;
}
sort(takeb[pos].begin(), takeb[pos].end());
reverse(takeb[pos].begin(), takeb[pos].end());
if((int) takeb[pos].size() > 0) neparan = takeb[pos][0];
if((int) takeb[pos].size() > 1) paran = max((ll) 0, takeb[pos][0] + takeb[pos][1]);
if((int) takeb[pos].size() > 2) neparan = max(neparan, takeb[pos][0] + takeb[pos][1] + takeb[pos][2]);
nastavi[pos].f = total + max(paran, neparan + parval);
nastavi[pos].s = total + paran;
return;
}
int main( void ) {
FIO
ll tempa, tempb, tempc;
cin >> n;
for(int i = 0; i < n - 1; i++) {
cin >> tempa >> tempb >> tempc;
edges[--tempa].pb(mp(--tempb, tempc));
edges[tempb].pb(mp(tempa, tempc));
}
dfs(0, -1);
cout << nastavi[0].s;
return 0;
}
컴파일 시 표준 에러 (stderr) 메시지
beads.cpp: In function 'void dfs(int, int)':
beads.cpp:20:55: warning: unused variable 'cur' [-Wunused-variable]
20 | ll paran = 0, neparan = -1e9, total = 0, parval = 0, cur = 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... |