이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
//even grosser
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef vector<ll> vl;
typedef pair<ll,ll> pl;
typedef vector<pl> vpl;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<vpl> al;
struct EG {
ll st,ed,wo;
};
const ll INFL = 1e18;
const int MN = 200200;
ll dp[MN][2][2];
bool bp[MN][2][2];
vl lev;
vvi g;
void dfa(int u, int p) {
if(u != p) {
lev[u] = lev[p]+1;
}
for(int i=0;i<g[u].size();i++) {
int v = g[u][i];
if(v == p) {continue;}
dfa(v,u);
}
}
vl ko;
ll ds(int u, int p, int m, int sp) {
if(bp[u][m][sp]) {
return dp[u][m][sp];
}
ll res;
if(m) {
if(g[u].size() >= 1) {
res = 0;
ll ma = -INFL;
for(int i=0;i<g[u].size();i++) {
int v = g[u][i];
if(v == p) {continue;}
ll mv = max(ko[v]+ds(v,u,1,0),ds(v,u,0,0));
res += mv;
ma = max(ma,ko[v]+ds(v,u,0,sp)-mv);
}
res += ma;
} else {
res = -INFL;
}
} else {
res = 0;
vector<pl> nu,du;
ll ma = -INFL;
for(int i=0;i<g[u].size();i++) {
int v = g[u][i];
if(v == p) {continue;}
ll mv = max(ds(v,u,0,0),ko[v]+ds(v,u,1,0));
ma = max(ma,max(ds(v,u,0,1),ko[v]+ds(v,u,1,1))-mv);
res += mv;
du.emplace_back(ko[v]+ds(v,u,0,0)-mv,v);
nu.emplace_back(ko[v]+ds(v,u,0,1)-mv,v);
}
if(sp) {
ll ad = max(0LL,ma);
if(du.size() >= 2) {
sort(du.rbegin(),du.rend());
sort(nu.rbegin(),nu.rend());
for(int i=0;i<2;i++) {
for(int j=0;j<2;j++) {
if(nu[i].second != du[j].second) {
ad = max(ad,nu[i].first+du[j].first);
}
}
}
}
res += ad;
}
}
bp[u][m][sp] = true;
return dp[u][m][sp] = res;
}
int main() {
ios::sync_with_stdio(0);cin.tie(0);
memset(bp,0,sizeof(bp));
ll n;
cin >> n;
lev.assign(n,0);
g.assign(n,vi());
ko.assign(n,0);
vector<EG> el;
for(int i=1;i<n;i++) {
ll a,b,c;
cin >> a >> b >> c;
a--;b--;
g[a].push_back(b);
g[b].push_back(a);
el.push_back({a,b,c});
}
dfa(0,0);
for(int i=0;i<el.size();i++) {
ll a = el[i].st;
ll b = el[i].ed;
if(lev[a] > lev[b]) {swap(a,b);}
ko[b] = el[i].wo;
}
cout << ds(0,0,0,1) << '\n';
}
컴파일 시 표준 에러 (stderr) 메시지
beads.cpp: In function 'void dfa(int, int)':
beads.cpp:24:15: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
24 | for(int i=0;i<g[u].size();i++) {
| ~^~~~~~~~~~~~
beads.cpp: In function 'll ds(int, int, int, int)':
beads.cpp:40:17: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
40 | for(int i=0;i<g[u].size();i++) {
| ~^~~~~~~~~~~~
beads.cpp:55:16: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
55 | for(int i=0;i<g[u].size();i++) {
| ~^~~~~~~~~~~~
beads.cpp: In function 'int main()':
beads.cpp:101:15: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<EG>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
101 | for(int i=0;i<el.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... |