#include<bits/stdc++.h>
using namespace std;
const int inf = (int) 1e9 + 10;
const int inf1 = (int) 1e9 + 10;
// #define int long long
#define dbl long double
#define endl '\n'
#define sc second
#define fr first
#define mp make_pair
#define pb push_back
#define all(x) x.begin(), x.end()
#define maxn 200020
int n;
vector<int> dp[maxn][4][2], mark[maxn][4][2];
vector<pair<int,int>> g[maxn];
/*
u = vertice
p = como esta
0 = liga pro pai ou pra algum filho com azul e o outro azul que vai ligar para esse segundo
3 = liga pro pai ou pra algum filho com azul e esse cara que vai ligar para o primeiro
1 = liga pro pai com red ou nao pode mais ligar pra ninguem / ja fez sua ligacao
2 = pode ligar para alguem com red ou para 2 com azul / ainda nao ligou pra ninguem
f = pode nao usar nada
1 = tem que nao usar
0 = tem que usar
*/
int sol(int u, int p, int f, int id, int ant) {
if(mark[u][p][f][id]) return dp[u][p][f][id];
mark[u][p][f][id] = 1;
if(p == 0) {
//tem que ligar para alguem com azul e nao se preocupa
//se ja tiver passado por todo mundo é -inf
if(id == g[u].size()) return dp[u][p][f][id] = -inf;
int v = g[u][id].fr;
int w = g[u][id].sc;
if(v == ant) return dp[u][p][f][id] = sol(u,p,f,id+1,ant);
int ans = -inf;
//se o outro azul é v
ans = max(ans, w + sol(v,2,0,0,u) + sol(u,1,f,id+1,ant));
ans = max(ans, w + sol(v,2,f,0,u) + sol(u,1,0,id+1,ant));
//só passa para o próximo podendo ligar para ele com azul
//(e o outro azul que vai estar ocupado) ou red
ans = max(ans, max(sol(v,1,0,0,u),w+sol(v,3,0,0,u)) + sol(u,0,f,id+1,ant));
ans = max(ans, max(sol(v,1,f,0,u),w+sol(v,3,f,0,u)) + sol(u,0,0,id+1,ant));
// cout << u << " ja ligou azul p = 0 || f = " << f << " atual no " << v << " id " << id << " = " << " = " << ans << endl;
return dp[u][p][f][id] = ans;
}
else if(p == 3) {
//tem que ligar para alguem com azul e o filho azul nao pode ligar para mais ninguem
//se ja tiver passado por todo mundo é -inf
if(id == g[u].size()) return dp[u][p][f][id] = -inf;
int v = g[u][id].fr;
int w = g[u][id].sc;
if(v == ant) return dp[u][p][f][id] = sol(u,p,f,id+1,ant);
int ans = -inf;
//se o outro azul é v
ans = max(ans, w + sol(v,1,0,0,u) + sol(u,1,f,id+1,ant));
ans = max(ans, w + sol(v,1,f,0,u) + sol(u,1,0,id+1,ant));
//só passa para o próximo podendo ligar para ele com azul
//(e o outro azul que vai estar ocupado) ou red
ans = max(ans, max(sol(v,1,0,0,u),w+sol(v,3,0,0,u)) + sol(u,3,f,id+1,ant));
ans = max(ans, max(sol(v,1,f,0,u),w+sol(v,3,f,0,u)) + sol(u,3,0,id+1,ant));
// cout << u << " ja ligou azul p = 3 || f = " << f << " atual no " << v << " id " << id << " = " << " = " << ans << endl;
return dp[u][p][f][id] = ans;
}
else if(p == 1) {
//ele ja fez a ligacao entao só pode passar pro próximo
//o v pode ligar para ele com red ou azul
if(id == g[u].size()) {
if(f == 1) return dp[u][p][f][id] = -inf;
else return dp[u][p][f][id] = 0;
}
int v = g[u][id].fr;
int w = g[u][id].sc;
if(v == ant) return dp[u][p][f][id] = sol(u,p,f,id+1,ant);
int ans = -inf;
//só passa para o próximo podendo ligar para ele com azul
//(e o outro azul que vai estar ocupado) ou red
ans = max(ans, max(sol(v,1,0,0,u),w+sol(v,3,0,0,u)) + sol(u,1,f,id+1,ant));
ans = max(ans, max(sol(v,1,f,0,u),w+sol(v,3,f,0,u)) + sol(u,1,0,id+1,ant));
// cout << u << " ja ligou para todos || f = " << f << " atual no " << v << " = " << ans << endl;
return dp[u][p][f][id] = ans;
}
else if(p == 2) {
//ele nao ligou para ninguem ainda e se f = 1 pode continuear nao ligando
if(id == g[u].size()) {
if(f == 1) return dp[u][p][f][id] = 0; //ele fez a opcao de nao ligar para niguem
else return dp[u][p][f][id] = -inf; //ele nao pode nao ligar para ninguem e mesmo assim fez
}
int v = g[u][id].fr;
int w = g[u][id].sc;
if(v == ant) return dp[u][p][f][id] = sol(u,p,f,id+1,ant);
int ans = -inf;
//só passa para o próximo podendo ligar para ele com azul
//(e o outro azul que vai estar ocupado) ou red
ans = max(ans, max(sol(v,1,0,0,u),w+sol(v,3,0,0,u)) + sol(u,2,f,id+1,ant));
ans = max(ans, max(sol(v,1,f,0,u),w+sol(v,3,f,0,u)) + sol(u,2,0,id+1,ant));
//ele vai fazer o papel da aresta red que é quebrada pelo v
ans = max(ans, w+sol(v,0,0,0,u) + sol(u,1,f,id+1,ant));
ans = max(ans, w+sol(v,0,f,0,u) + sol(u,1,0,id+1,ant));
//liga para o proximo com um uma aresta red
ans = max(ans, sol(v,2,0,0,u) + sol(u,1,f,id+1,ant));
ans = max(ans, sol(v,2,f,0,u) + sol(u,1,0,id+1,ant));
//liga para o proximo com uma aresta azul e v que vai fazer a aresta red
ans = max(ans, w+sol(v,1,0,0,u) + sol(u,0,f,id+1,ant));
ans = max(ans, w+sol(v,1,f,0,u) + sol(u,0,0,id+1,ant));
//liga para o proximo com uma aresta azul e o outro que vai fazer a aresta red
ans = max(ans, w+sol(v,2,0,0,u) + sol(u,3,f,id+1,ant));
ans = max(ans, w+sol(v,2,f,0,u) + sol(u,3,0,id+1,ant));
// cout << u << " nao liga pra ninguem e || f = " << f << " atual no " << v << " = " << ans << endl;
return dp[u][p][f][id] = ans;
}
return 0;
}
void solve() {
cin >> n;
for(int i = 1; i < n; i++) {
int u,v,w;
cin >> u >> v >> w;
g[u].pb(mp(v,w));
g[v].pb(mp(u,w));
}
int rt;
for(int i = 1; i <= n; i++) {
if(g[i].size() == 1) {
rt = i;
// break;
}
}
for(int i = 1; i <= n; i++) {
for(int j = 0; j <= g[i].size(); j++) {
for(int k = 0; k <= 3; k++) {
dp[i][k][0].pb(0);
dp[i][k][1].pb(0);
mark[i][k][0].pb(0);
mark[i][k][1].pb(0);
}
}
}
cout << sol(rt,2,1,0,0) << endl;
}
int32_t main() {
ios::sync_with_stdio(false); cin.tie(0);
// freopen("in.in", "r", stdin);
// freopen("out.out", "w", stdout);
int tt = 1;
// cin >> tt;
while(tt--) solve();
}
Compilation message
beads.cpp: In function 'int sol(int, int, int, int, int)':
beads.cpp:43:15: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
43 | if(id == g[u].size()) return dp[u][p][f][id] = -inf;
| ~~~^~~~~~~~~~~~~~
beads.cpp:66:15: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
66 | if(id == g[u].size()) return dp[u][p][f][id] = -inf;
| ~~~^~~~~~~~~~~~~~
beads.cpp:89:15: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
89 | if(id == g[u].size()) {
| ~~~^~~~~~~~~~~~~~
beads.cpp:110:15: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
110 | if(id == g[u].size()) {
| ~~~^~~~~~~~~~~~~~
beads.cpp: In function 'void solve()':
beads.cpp:166:26: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
166 | for(int j = 0; j <= g[i].size(); j++) {
| ~~^~~~~~~~~~~~~~
beads.cpp:8:14: warning: 'rt' may be used uninitialized in this function [-Wmaybe-uninitialized]
8 | #define endl '\n'
| ^~~~
beads.cpp:157:9: note: 'rt' was declared here
157 | int rt;
| ^~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
37 ms |
80144 KB |
Output is correct |
2 |
Correct |
38 ms |
80080 KB |
Output is correct |
3 |
Correct |
37 ms |
80060 KB |
Output is correct |
4 |
Correct |
37 ms |
80072 KB |
Output is correct |
5 |
Correct |
40 ms |
80156 KB |
Output is correct |
6 |
Correct |
47 ms |
80120 KB |
Output is correct |
7 |
Correct |
41 ms |
80084 KB |
Output is correct |
8 |
Correct |
44 ms |
80124 KB |
Output is correct |
9 |
Correct |
44 ms |
80076 KB |
Output is correct |
10 |
Correct |
62 ms |
80100 KB |
Output is correct |
11 |
Correct |
48 ms |
80092 KB |
Output is correct |
12 |
Correct |
42 ms |
80136 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
37 ms |
80144 KB |
Output is correct |
2 |
Correct |
38 ms |
80080 KB |
Output is correct |
3 |
Correct |
37 ms |
80060 KB |
Output is correct |
4 |
Correct |
37 ms |
80072 KB |
Output is correct |
5 |
Correct |
40 ms |
80156 KB |
Output is correct |
6 |
Correct |
47 ms |
80120 KB |
Output is correct |
7 |
Correct |
41 ms |
80084 KB |
Output is correct |
8 |
Correct |
44 ms |
80124 KB |
Output is correct |
9 |
Correct |
44 ms |
80076 KB |
Output is correct |
10 |
Correct |
62 ms |
80100 KB |
Output is correct |
11 |
Correct |
48 ms |
80092 KB |
Output is correct |
12 |
Correct |
42 ms |
80136 KB |
Output is correct |
13 |
Correct |
45 ms |
80120 KB |
Output is correct |
14 |
Correct |
48 ms |
80208 KB |
Output is correct |
15 |
Correct |
48 ms |
80148 KB |
Output is correct |
16 |
Correct |
47 ms |
80188 KB |
Output is correct |
17 |
Correct |
45 ms |
80236 KB |
Output is correct |
18 |
Correct |
51 ms |
80264 KB |
Output is correct |
19 |
Correct |
55 ms |
80304 KB |
Output is correct |
20 |
Correct |
54 ms |
80252 KB |
Output is correct |
21 |
Correct |
54 ms |
80268 KB |
Output is correct |
22 |
Correct |
47 ms |
80284 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
37 ms |
80144 KB |
Output is correct |
2 |
Correct |
38 ms |
80080 KB |
Output is correct |
3 |
Correct |
37 ms |
80060 KB |
Output is correct |
4 |
Correct |
37 ms |
80072 KB |
Output is correct |
5 |
Correct |
40 ms |
80156 KB |
Output is correct |
6 |
Correct |
47 ms |
80120 KB |
Output is correct |
7 |
Correct |
41 ms |
80084 KB |
Output is correct |
8 |
Correct |
44 ms |
80124 KB |
Output is correct |
9 |
Correct |
44 ms |
80076 KB |
Output is correct |
10 |
Correct |
62 ms |
80100 KB |
Output is correct |
11 |
Correct |
48 ms |
80092 KB |
Output is correct |
12 |
Correct |
42 ms |
80136 KB |
Output is correct |
13 |
Correct |
45 ms |
80120 KB |
Output is correct |
14 |
Correct |
48 ms |
80208 KB |
Output is correct |
15 |
Correct |
48 ms |
80148 KB |
Output is correct |
16 |
Correct |
47 ms |
80188 KB |
Output is correct |
17 |
Correct |
45 ms |
80236 KB |
Output is correct |
18 |
Correct |
51 ms |
80264 KB |
Output is correct |
19 |
Correct |
55 ms |
80304 KB |
Output is correct |
20 |
Correct |
54 ms |
80252 KB |
Output is correct |
21 |
Correct |
54 ms |
80268 KB |
Output is correct |
22 |
Correct |
47 ms |
80284 KB |
Output is correct |
23 |
Correct |
66 ms |
83008 KB |
Output is correct |
24 |
Correct |
63 ms |
83028 KB |
Output is correct |
25 |
Correct |
70 ms |
83020 KB |
Output is correct |
26 |
Correct |
90 ms |
85836 KB |
Output is correct |
27 |
Correct |
107 ms |
85836 KB |
Output is correct |
28 |
Correct |
106 ms |
87620 KB |
Output is correct |
29 |
Correct |
104 ms |
87344 KB |
Output is correct |
30 |
Correct |
100 ms |
87232 KB |
Output is correct |
31 |
Correct |
109 ms |
86840 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
37 ms |
80144 KB |
Output is correct |
2 |
Correct |
38 ms |
80080 KB |
Output is correct |
3 |
Correct |
37 ms |
80060 KB |
Output is correct |
4 |
Correct |
37 ms |
80072 KB |
Output is correct |
5 |
Correct |
40 ms |
80156 KB |
Output is correct |
6 |
Correct |
47 ms |
80120 KB |
Output is correct |
7 |
Correct |
41 ms |
80084 KB |
Output is correct |
8 |
Correct |
44 ms |
80124 KB |
Output is correct |
9 |
Correct |
44 ms |
80076 KB |
Output is correct |
10 |
Correct |
62 ms |
80100 KB |
Output is correct |
11 |
Correct |
48 ms |
80092 KB |
Output is correct |
12 |
Correct |
42 ms |
80136 KB |
Output is correct |
13 |
Correct |
45 ms |
80120 KB |
Output is correct |
14 |
Correct |
48 ms |
80208 KB |
Output is correct |
15 |
Correct |
48 ms |
80148 KB |
Output is correct |
16 |
Correct |
47 ms |
80188 KB |
Output is correct |
17 |
Correct |
45 ms |
80236 KB |
Output is correct |
18 |
Correct |
51 ms |
80264 KB |
Output is correct |
19 |
Correct |
55 ms |
80304 KB |
Output is correct |
20 |
Correct |
54 ms |
80252 KB |
Output is correct |
21 |
Correct |
54 ms |
80268 KB |
Output is correct |
22 |
Correct |
47 ms |
80284 KB |
Output is correct |
23 |
Correct |
66 ms |
83008 KB |
Output is correct |
24 |
Correct |
63 ms |
83028 KB |
Output is correct |
25 |
Correct |
70 ms |
83020 KB |
Output is correct |
26 |
Correct |
90 ms |
85836 KB |
Output is correct |
27 |
Correct |
107 ms |
85836 KB |
Output is correct |
28 |
Correct |
106 ms |
87620 KB |
Output is correct |
29 |
Correct |
104 ms |
87344 KB |
Output is correct |
30 |
Correct |
100 ms |
87232 KB |
Output is correct |
31 |
Correct |
109 ms |
86840 KB |
Output is correct |
32 |
Correct |
352 ms |
108864 KB |
Output is correct |
33 |
Correct |
360 ms |
108800 KB |
Output is correct |
34 |
Correct |
278 ms |
108856 KB |
Output is correct |
35 |
Runtime error |
328 ms |
131072 KB |
Execution killed with signal 9 |
36 |
Halted |
0 ms |
0 KB |
- |