# |
Submission time |
Handle |
Problem |
Language |
Result |
Execution time |
Memory |
791804 |
2023-07-24T10:15:43 Z |
phoebe |
Islands (IOI08_islands) |
C++17 |
|
657 ms |
131072 KB |
#include <bits/stdc++.h>
using namespace std;
// #define int long long
#define ll long long
#define pii pair<ll, int>
#define F first
#define S second
#define PB push_back
#define ALL(x) x.begin(), x.end()
#define FOR(i, n) for (int i = 0; i < n; i++)
#define NYOOM ios::sync_with_stdio(0); cin.tie(0);
#define endl '\n'
const int INF = 1e9 + 7;
const ll LLINF = 1ll<<60;
const int maxn = 1e6 + 10;
int n, p[maxn], l[maxn]; // 0 = unseen, 1 = seeing, 2 = seen
ll val[maxn], cur = 0, re = 0;
// bool on_cycle[maxn] = {0};
bitset<maxn> on_cycle, seen, cur_seen;
vector<int> adj[maxn];
void dfs(int v, int p){
val[v] = 0;
ll best = -1, second_best = -1;
for (auto u : adj[v]){
if (on_cycle[u] || u == p) continue;
dfs(u, v);
val[v] = max(val[v], val[u] + l[u]);
if (best == -1) best = val[u] + l[u];
else second_best = max(second_best, val[u] + l[u]);
if (second_best > best) swap(best, second_best);
}
cur = max(cur, best + second_best);
}
void solve(int s, int sz, ll total_sum){
int v = s;
ll cw = -LLINF, ccw = -LLINF, cur_sum = 0;
FOR(i, sz){
cur = max(cur, cw + val[v] + cur_sum);
cur = max(cur, ccw + val[v] + (total_sum - cur_sum));
cw = max(cw, val[v] - cur_sum);
ccw = max(ccw, val[v] + cur_sum);
cur_sum += l[v]; v = p[v];
}
}
void find_cycle(int s){
int v = s;
while (!cur_seen[v]){ // while unseen
cur_seen[v] = true; v = p[v];
}
if (cur_seen[v] && !seen[v]){ // found a cycle
cur = 0; int sz = 0; ll total_sum = 0;
while (!on_cycle[v]){
total_sum += l[v];
sz++; on_cycle[v] = true; v = p[v];
}
FOR(i, sz){
dfs(v, v); v = p[v];
}
solve(v, sz, total_sum);
re += cur;
}
v = s;
while (!seen[v]){
seen[v] = true; v = p[v];
}
}
signed main(){
NYOOM;
// freopen("isl.in", "r", stdin);
// freopen("isl.out", "w", stdout);
cin >> n;
for (int i = 1; i <= n; i++){
cin >> p[i] >> l[i];
adj[p[i]].PB(i);
}
for (int i = 1; i <= n; i++){
if (seen[i] != 2) find_cycle(i);
}
cout << re;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
13 ms |
23764 KB |
Output is correct |
2 |
Correct |
16 ms |
23728 KB |
Output is correct |
3 |
Correct |
16 ms |
23848 KB |
Output is correct |
4 |
Correct |
12 ms |
23816 KB |
Output is correct |
5 |
Correct |
11 ms |
23824 KB |
Output is correct |
6 |
Correct |
12 ms |
23764 KB |
Output is correct |
7 |
Correct |
13 ms |
23764 KB |
Output is correct |
8 |
Correct |
13 ms |
23816 KB |
Output is correct |
9 |
Correct |
13 ms |
23764 KB |
Output is correct |
10 |
Correct |
12 ms |
23804 KB |
Output is correct |
11 |
Correct |
13 ms |
23764 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
15 ms |
23892 KB |
Output is correct |
2 |
Correct |
14 ms |
23836 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
13 ms |
23876 KB |
Output is correct |
2 |
Correct |
14 ms |
23952 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
16 ms |
24468 KB |
Output is correct |
2 |
Correct |
21 ms |
25240 KB |
Output is correct |
3 |
Correct |
18 ms |
24840 KB |
Output is correct |
4 |
Correct |
15 ms |
24236 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
25 ms |
26008 KB |
Output is correct |
2 |
Correct |
33 ms |
28292 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
69 ms |
31272 KB |
Output is correct |
2 |
Correct |
59 ms |
35140 KB |
Output is correct |
3 |
Correct |
70 ms |
33332 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
93 ms |
39684 KB |
Output is correct |
2 |
Correct |
113 ms |
40560 KB |
Output is correct |
3 |
Correct |
117 ms |
52636 KB |
Output is correct |
4 |
Correct |
143 ms |
46856 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
205 ms |
44544 KB |
Output is correct |
2 |
Correct |
395 ms |
56564 KB |
Output is correct |
3 |
Correct |
169 ms |
52188 KB |
Output is correct |
4 |
Correct |
226 ms |
58480 KB |
Output is correct |
5 |
Correct |
212 ms |
56128 KB |
Output is correct |
6 |
Correct |
657 ms |
59272 KB |
Output is correct |
7 |
Correct |
216 ms |
59028 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Runtime error |
213 ms |
131072 KB |
Execution killed with signal 9 |
2 |
Halted |
0 ms |
0 KB |
- |