이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const ll inf = 1e10;
const int Nmax = 2e5 + 5;
vector< pair<int,int> > v[Nmax];
ll dp[Nmax][2][2], all[5][5], act[5][5];
void init()
{
int i, j;
for(i=0; i<=2; ++i)
for(j=0; j<=1; ++j)
all[i][j] = -inf;
all[0][0] = all[0][1] = 0;
}
void dfs(int node, int dad = 0)
{
int x, i, j, z, w;
ll up = -inf, cost;
for(auto it : v[node])
{
tie(x, cost) = it;
if(x == dad)
{
up = cost;
continue;
}
dfs(x, node);
}
init();
for(i=0; i<=2; ++i)
for(j=0; j<=1; ++j)
act[i][j] = -inf;
act[0][0] = act[0][1] = 0;
for(auto it : v[node])
{
tie(x, cost) = it;
if(x == dad) continue;
for(i=0; i<=2; ++i) /// gradul nodului
for(j=0; j<=1; ++j) /// am noduri proaste?
for(z=0; z<=1; ++z)
for(w=0; w<=1; ++w)
all[i + z][j + w] = max(all[i+z][j+w], act[i][j] + dp[x][z][w]);
for(i=0; i<=2; ++i)
for(j=0; j<=1; ++j)
act[i][j] = all[i][j];
init();
}
dp[node][0][0] = max(act[0][0], act[1][0] + up);
dp[node][0][1] = max(act[0][1], act[1][1] + up);
dp[node][0][1] = max(dp[node][0][1], act[2][(node==1)]);
dp[node][1][0] = act[0][0] + up;
dp[node][1][1] = max(act[0][1], act[2][0]) + up;
}
int main()
{
// freopen("input", "r", stdin);
cin.sync_with_stdio(false);
int n, i, x, y, cost;
cin >> n;
for(i=1; i<n; ++i)
{
cin >> x >> y >> cost;
v[x].push_back({y, cost});
v[y].push_back({x, cost});
}
dfs(1);
cout << max(dp[1][0][0], dp[1][0][1]) << '\n';
return 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... |