# | Submission time | Handle | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
58260 | 2018-07-17T10:08:18 Z | PeppaPig | Islands (IOI08_islands) | C++14 | 404 ms | 115892 KB |
#include <bits/stdc++.h> #define pll pair<long long, long long> #define x first #define y second using namespace std; const int N = 1e6 + 5; int n; long long deg[N], dm[N]; pll par[N], dp[N]; bitset<N> vis; long long solve(int u) { long long ret = 0; int sz; vector<pll> V; deque<pll> Q; V.emplace_back(0, 0); while(deg[u]) { deg[u]--; dm[u] = max(dm[u], dp[u].x + dp[u].y); ret = max(ret, dm[u]); V.emplace_back(u, par[u].y); u = par[u].x; } sz = V.size() - 1; for(int i = 1; i <= sz; i++) V[i].y += V[i - 1].y; for(int i = 1; i <= sz; i++) { while(!Q.empty() && Q.back().x < V[i - 1].y + dp[V[i].x].x) Q.pop_back(); Q.emplace_back(V[i - 1].y + dp[V[i].x].x, i); } ret = max(ret, dp[V[1].x].x + Q.front().x); for(int i = 1; i < sz; i++) { if(Q.back().y == i) Q.pop_back(); while(!Q.empty() && Q.back().x < V[sz].y + V[i - 1].y + dp[V[i].x].x) Q.pop_back(); Q.emplace_back(V[sz].y + V[i - 1].y + dp[V[i].x].x, i); ret = max(ret, dp[V[i + 1].x].x + Q.front().x - V[i].y); } return ret; } int main() { scanf("%d", &n); for(int i = 1, v, w; i <= n; i++) { scanf("%d %d", &v, &w); par[i] = pll(v, w); deg[v]++; } queue<int> Q; for(int i = 1; i <= n; i++) if(!deg[i]) Q.emplace(i); while(!Q.empty()) { int now = Q.front(); Q.pop(); long long p = par[now].x, w = par[now].y; dm[now] = max(dm[now], dp[now].x + dp[now].y); dm[p] = max(dm[p], dm[now]); long long ret = dp[now].x + w; if(ret > dp[p].x) swap(ret, dp[p].x); if(ret > dp[p].y) swap(ret, dp[p].y); if(!--deg[p]) Q.emplace(p); } long long ans = 0; for(int i = 1; i <= n; i++) if(deg[i]) ans += solve(i); printf("%lld", ans); return 0; }
Compilation message
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
1 | Correct | 3 ms | 376 KB | Output is correct |
2 | Incorrect | 3 ms | 496 KB | Output isn't correct |
3 | Correct | 3 ms | 496 KB | Output is correct |
4 | Correct | 2 ms | 544 KB | Output is correct |
5 | Correct | 5 ms | 544 KB | Output is correct |
6 | Correct | 2 ms | 640 KB | Output is correct |
7 | Correct | 3 ms | 644 KB | Output is correct |
8 | Correct | 5 ms | 644 KB | Output is correct |
9 | Correct | 2 ms | 648 KB | Output is correct |
10 | Incorrect | 3 ms | 712 KB | Output isn't correct |
11 | Correct | 3 ms | 716 KB | Output is correct |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
1 | Correct | 2 ms | 720 KB | Output is correct |
2 | Correct | 4 ms | 768 KB | Output is correct |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
1 | Correct | 4 ms | 912 KB | Output is correct |
2 | Correct | 5 ms | 1068 KB | Output is correct |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
1 | Correct | 10 ms | 1952 KB | Output is correct |
2 | Correct | 22 ms | 3552 KB | Output is correct |
3 | Incorrect | 15 ms | 3552 KB | Output isn't correct |
4 | Halted | 0 ms | 0 KB | - |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
1 | Correct | 29 ms | 5528 KB | Output is correct |
2 | Correct | 44 ms | 8716 KB | Output is correct |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
1 | Correct | 87 ms | 16428 KB | Output is correct |
2 | Correct | 97 ms | 20308 KB | Output is correct |
3 | Correct | 144 ms | 28344 KB | Output is correct |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
1 | Correct | 179 ms | 35524 KB | Output is correct |
2 | Correct | 202 ms | 51736 KB | Output is correct |
3 | Correct | 183 ms | 56796 KB | Output is correct |
4 | Correct | 237 ms | 76992 KB | Output is correct |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
1 | Incorrect | 398 ms | 81228 KB | Output isn't correct |
2 | Halted | 0 ms | 0 KB | - |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
1 | Incorrect | 404 ms | 115892 KB | Output isn't correct |
2 | Halted | 0 ms | 0 KB | - |