# | Submission time | Handle | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
58757 | 2018-07-19T08:59:30 Z | Sherazin | Islands (IOI08_islands) | C++14 | 908 ms | 90756 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 = 2; 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.front().y == i + 1) Q.pop_front(); 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 | 2 ms | 248 KB | Output is correct |
2 | Correct | 3 ms | 360 KB | Output is correct |
3 | Correct | 3 ms | 400 KB | Output is correct |
4 | Correct | 2 ms | 484 KB | Output is correct |
5 | Correct | 2 ms | 504 KB | Output is correct |
6 | Correct | 3 ms | 636 KB | Output is correct |
7 | Correct | 2 ms | 636 KB | Output is correct |
8 | Correct | 3 ms | 652 KB | Output is correct |
9 | Correct | 2 ms | 652 KB | Output is correct |
10 | Correct | 2 ms | 660 KB | Output is correct |
11 | Correct | 2 ms | 660 KB | Output is correct |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
1 | Correct | 4 ms | 660 KB | Output is correct |
2 | Correct | 3 ms | 672 KB | Output is correct |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
1 | Correct | 4 ms | 948 KB | Output is correct |
2 | Correct | 4 ms | 976 KB | Output is correct |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
1 | Correct | 9 ms | 1800 KB | Output is correct |
2 | Correct | 19 ms | 3404 KB | Output is correct |
3 | Correct | 11 ms | 3404 KB | Output is correct |
4 | Correct | 7 ms | 3404 KB | Output is correct |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
1 | Correct | 21 ms | 4308 KB | Output is correct |
2 | Correct | 48 ms | 6408 KB | Output is correct |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
1 | Correct | 75 ms | 11600 KB | Output is correct |
2 | Correct | 79 ms | 13164 KB | Output is correct |
3 | Correct | 100 ms | 17608 KB | Output is correct |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
1 | Correct | 139 ms | 19540 KB | Output is correct |
2 | Correct | 238 ms | 30052 KB | Output is correct |
3 | Correct | 208 ms | 30052 KB | Output is correct |
4 | Correct | 270 ms | 40476 KB | Output is correct |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
1 | Correct | 393 ms | 40476 KB | Output is correct |
2 | Correct | 657 ms | 46988 KB | Output is correct |
3 | Correct | 316 ms | 46988 KB | Output is correct |
4 | Correct | 380 ms | 57572 KB | Output is correct |
5 | Correct | 394 ms | 58492 KB | Output is correct |
6 | Correct | 908 ms | 62328 KB | Output is correct |
7 | Correct | 424 ms | 80824 KB | Output is correct |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
1 | Correct | 418 ms | 80824 KB | Output is correct |
2 | Correct | 375 ms | 80824 KB | Output is correct |
3 | Correct | 399 ms | 80824 KB | Output is correct |
4 | Correct | 496 ms | 80824 KB | Output is correct |
5 | Correct | 366 ms | 80824 KB | Output is correct |
6 | Correct | 376 ms | 80824 KB | Output is correct |
7 | Correct | 791 ms | 90756 KB | Output is correct |