이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include <cstdio>
#include <vector>
#include <algorithm>
using namespace std;
vector <pair <int,int> > v[200010];
int dp[4][200010];
void dfs (int nod,int tt){
/// 0 -> nod nu a fost luat
/// 1 -> nod e mijloc in lant
/// 2 -> nod e mijloc in triunghi (e si mijloc si cel mai de sus)
/// 3 -> nod e cel mai de sus dintr un lant luat
int i,s3=0,other=0,chosen,vecin,cost,sum,val;
/// continui dfs + dp[0][nod]
s3 = 0;
for (i=0;i<v[nod].size();i++){
vecin = v[nod][i].first;
cost = v[nod][i].second;
if (vecin != tt){
dfs (vecin , nod);
dp[0][nod] += max (dp[0][vecin] , max (dp[2][vecin] , dp[3][vecin]));
if (dp[1][vecin])
s3 = s3 + max ( max ( dp[0][vecin] , dp[1][vecin] + cost) , max (dp[2][vecin] , dp[3][vecin]));
else s3 = s3 + max ( dp[0][vecin] , max (dp[2][vecin] , dp[3][vecin]));
}
}
/// dp[1][nod] -> alegi un vecin pe care il iei obligatoriu cu 0
sum = s3;
int ok = 0;
for (i=0;i<v[nod].size();i++){
vecin = v[nod][i].first;
cost = v[nod][i].second;
if (vecin!=tt){
if (dp[1][vecin])
other = sum - max ( max ( dp[0][vecin] , dp[1][vecin] + cost) , max (dp[2][vecin] , dp[3][vecin]));
else other = sum - max ( dp[0][vecin] , max (dp[2][vecin] , dp[3][vecin]));
val = max (dp[0][vecin] , max(dp[2][vecin] , dp[3][vecin]));
if (dp[1][nod] < other + val + cost){
chosen = vecin;
dp[1][nod] = other + val + cost;
if (val == dp[2][vecin])
ok = 1;
else ok = 0;
}
}
}
/// dp[2][nod] -> alegi doi vecini pe care sa ii iei obligatoriu cu 0
/// ok == 1 => nu mai poti sa iei alt 2
for (i=0;i<v[nod].size();i++){
vecin = v[nod][i].first;
cost = v[nod][i].second;
if (vecin!=tt && vecin!=chosen){
if (dp[1][vecin])
other = dp[1][nod] - max ( max ( dp[0][vecin] , dp[1][vecin] + cost) , max (dp[2][vecin] , dp[3][vecin]));
else other = dp[1][nod] - max ( dp[0][vecin] , max (dp[2][vecin] , dp[3][vecin]));
val = max (dp[0][vecin] , dp[3][vecin]);
if (!ok)
val = max (val , dp[2][vecin]);
if (dp[2][nod] < other + val + cost){
dp[2][nod] = other + val + cost;
}
}
}
/// dp[3][nod] -> alegi cati vecini vrei cu 1 , CEL PUTIN UNUL
for (i=0;i<v[nod].size();i++){
vecin = v[nod][i].first;
cost = v[nod][i].second;
if (vecin!=tt && dp[1][vecin]){
other = s3 - max ( max ( dp[0][vecin] , dp[1][vecin] + cost) , max (dp[2][vecin] , dp[3][vecin]));
dp[3][nod] = max ( dp[3][nod] , other + dp[1][vecin] + cost);
}
}
}
int main()
{
// freopen ("a.in" , "r" , stdin);
// freopen ("a.out" , "w" , stdout);
int n,i,x,y,c;
scanf ("%d",&n);
for (i=1;i<n;i++){
scanf ("%d%d%d",&x,&y,&c);
v[x].push_back(make_pair(y,c));
v[y].push_back(make_pair(x,c));
}
dfs (1,0);
printf ("%d",max (dp[0][1] , max (dp[2][1] , dp[3][1])));
/// nu poate fi mijloc in lant
return 0;
}
컴파일 시 표준 에러 (stderr) 메시지
beads.cpp: In function 'void dfs(int, int)':
beads.cpp:18:15: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for (i=0;i<v[nod].size();i++){
~^~~~~~~~~~~~~~
beads.cpp:34:15: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for (i=0;i<v[nod].size();i++){
~^~~~~~~~~~~~~~
beads.cpp:56:15: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for (i=0;i<v[nod].size();i++){
~^~~~~~~~~~~~~~
beads.cpp:77:15: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for (i=0;i<v[nod].size();i++){
~^~~~~~~~~~~~~~
beads.cpp: In function 'int main()':
beads.cpp:91:11: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf ("%d",&n);
~~~~~~^~~~~~~~~
beads.cpp:93:15: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf ("%d%d%d",&x,&y,&c);
~~~~~~^~~~~~~~~~~~~~~~~~~
beads.cpp: In function 'void dfs(int, int)':
beads.cpp:59:31: warning: 'chosen' may be used uninitialized in this function [-Wmaybe-uninitialized]
if (vecin!=tt && vecin!=chosen){
~~~~~^~~~~~~~
# | 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... |