#include "bits/stdc++.h"
#define ll long long
#define all(x) x.begin(), x.end()
#define read(x) for(auto &i : x) cin >> i;
#define sz(x) (int) (x).size()
#define len(x) (int) (x).length()
#define arr array
constexpr int INF = 1e9 + 9;
constexpr long long INF_LL = 1e18 + 9;
constexpr int MOD = 1e9 + 7; // 998244353;
// up, right, down, left
constexpr int mX_4 [] = {0, 1, 0, -1};
constexpr int mY_4 [] = {-1, 0, 1, 0};
using namespace std;
short rand_num(short l, short r) {
static mt19937_64 gen(chrono::steady_clock::now().time_since_epoch().count());
return uniform_int_distribution<short>(l, r)(gen);
}
template <typename T>
std::ostream& operator<<(std::ostream& _os, const std::pair<T, T>& _p) {
_os << _p.first << ' ' << _p.second;
return _os;
}
template <typename T>
void dbg_vec(string str, vector <T> &x) {
cerr << "DBG " << str << '\n';
for (int i = 0; i < sz(x); ++i) {
cerr << "I " << i << " -> " << x[i] << '\n';
}
cerr << '\n';
}
const int MAX_BIT_DBG = 3;
void dbg_bits(int n) {
bitset <MAX_BIT_DBG> v (n);
cerr << v << '\n';
}
template <typename T>
bool vmin(T &a, T b) { return (a > b ? a = b, true : false); }
template <typename T>
bool vmax(T &a, T b) { return (a < b ? a = b, true : false); }
void solve() {
int n; cin >> n;
vector <vector <pair <int, int> > > g (n);
vector <bool> vis (n);
for (int i = 0; i < n; ++i) {
int v, w; cin >> v >> w; --v;
g[i].push_back({v, w});
g[v].push_back({i, w});
}
for (int i = 0; i < n; ++i) {
sort(all(g[i]), [&] (auto &a, auto &b) {
return a.second > b.second;
});
}
vector <vector <pair <int, ll> > > tree (n);
// creo il dfs tree
function <void(int, int)> dfs = [&] (int node, int parent) {
vis[node] = 1;
for (auto [to, w] : g[node]) {
if (to == parent or vis[to]) continue;
tree[node].push_back({to, w});
dfs(to, node);
}
};
vector <vector <ll> > dp (2, vector <ll> (n));
function <ll(int, int)> f = [&] (int node, int type) {
if (dp[type][node]) return dp[type][node];
// massimi sub problems
ll mx1 = 0; // massimo 1
ll mx2 = 0; // massimo 2
ll sub = 0;
for (auto [to, w] : tree[node]) {
// posso decidere di non percorrere l'arco solo se non mi sono collegato a nessuno
if (type == 0) {
sub = f(to, type);
}
// percorro l'arco
vmax(sub, f(to, 1) + w);
if (sub > mx1) {
mx2 = mx1;
mx1 = sub;
} else if (sub > mx2) {
mx2 = sub;
}
}
return dp[type][node] = (mx1 + (type ? 0 : mx2));
};
ll ans = 0;
for (int node = 0; node < n; ++node) {
if (!vis[node]) {
dfs(node, node);
// cerr << "Node " << node << '\n';
ans += f(node, 0);
}
}
cout << ans << '\n';
}
signed main() {
ios::sync_with_stdio(false); cin.tie(0);
int tt = 1; // cin >> tt;
while (tt--) solve();
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Incorrect |
1 ms |
340 KB |
Output isn't correct |
3 |
Incorrect |
1 ms |
340 KB |
Output isn't correct |
4 |
Correct |
1 ms |
320 KB |
Output is correct |
5 |
Correct |
1 ms |
212 KB |
Output is correct |
6 |
Correct |
1 ms |
212 KB |
Output is correct |
7 |
Incorrect |
0 ms |
212 KB |
Output isn't correct |
8 |
Incorrect |
1 ms |
212 KB |
Output isn't correct |
9 |
Incorrect |
1 ms |
212 KB |
Output isn't correct |
10 |
Correct |
1 ms |
340 KB |
Output is correct |
11 |
Correct |
1 ms |
340 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
1 ms |
468 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
2 ms |
460 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
8 ms |
2632 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
28 ms |
10052 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
100 ms |
30680 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
175 ms |
58092 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
695 ms |
131072 KB |
Output is correct |
2 |
Runtime error |
512 ms |
131072 KB |
Execution killed with signal 9 |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Runtime error |
332 ms |
131072 KB |
Execution killed with signal 9 |
2 |
Halted |
0 ms |
0 KB |
- |