# | 제출 시각 | 아이디 | 문제 | 언어 | 결과 | 실행 시간 | 메모리 |
---|---|---|---|---|---|---|---|
216625 | 2020-03-27T16:46:05 Z | songc | Islands (IOI08_islands) | C++14 | 824 ms | 131072 KB |
#include <bits/stdc++.h> #define pb push_back #define all(v) v.begin(),v.end() #define fi first #define se second #define INF (1ll<<60) using namespace std; typedef long long LL; typedef pair<int,int> pii; int N, M; LL ans, ret; int P[1000010], W[1000010]; bool chk[1000010]; vector<int> adj[1000010]; vector<LL> V, S; inline int cyc(int u){ if (chk[u]) return u; chk[u] = true; int r = cyc(P[u]); if (r){ V.pb(u), S.pb(W[u]); return (r==u)?0:r; } chk[u] = false; return 0; } inline LL dfs(int u){ LL mx=0; chk[u] = true; for (int v : adj[u]){ if (chk[v]) continue; LL r = dfs(v) + W[v]; ret = max(ret, mx+r); mx = max(mx, r); } return mx; } void dia(int u){ V.clear(), S.clear(); cyc(u); reverse(all(V)); reverse(all(S)); for (LL &v : V) v = dfs(v); M = V.size(); for (int i=M-1; i>0; i--) swap(S[i], S[i-1]); for (int i=1; i<M; i++) S[i] += S[i-1]; LL m1=-INF, m2=-INF; for (int i=0; i<M; i++){ ret = max(ret, m1+S[i]+V[i]); ret = max(ret, m2-S[i]+V[i]+S[M-1]); m1 = max(m1, V[i]-S[i]); m2 = max(m2, V[i]+S[i]); } } int main(){ scanf("%d", &N); for (int i=1; i<=N; i++){ scanf("%d %lld", &P[i], &W[i]); adj[P[i]].pb(i); } for (int i=1; i<=N; i++){ if (chk[i]) continue; dia(i); ans+=ret, ret=0; } printf("%lld\n", ans); return 0; }
Compilation message
# | 결과 | 실행 시간 | 메모리 | Grader output |
---|---|---|---|---|
1 | Correct | 20 ms | 23856 KB | Output is correct |
2 | Correct | 20 ms | 23808 KB | Output is correct |
3 | Correct | 22 ms | 23936 KB | Output is correct |
4 | Correct | 20 ms | 23808 KB | Output is correct |
5 | Correct | 18 ms | 23808 KB | Output is correct |
6 | Correct | 23 ms | 23808 KB | Output is correct |
7 | Correct | 18 ms | 23808 KB | Output is correct |
8 | Correct | 18 ms | 23808 KB | Output is correct |
9 | Correct | 18 ms | 23808 KB | Output is correct |
10 | Correct | 18 ms | 23808 KB | Output is correct |
11 | Correct | 18 ms | 23808 KB | Output is correct |
# | 결과 | 실행 시간 | 메모리 | Grader output |
---|---|---|---|---|
1 | Correct | 21 ms | 23808 KB | Output is correct |
2 | Correct | 19 ms | 23808 KB | Output is correct |
# | 결과 | 실행 시간 | 메모리 | Grader output |
---|---|---|---|---|
1 | Correct | 19 ms | 23808 KB | Output is correct |
2 | Correct | 20 ms | 24192 KB | Output is correct |
# | 결과 | 실행 시간 | 메모리 | Grader output |
---|---|---|---|---|
1 | Correct | 25 ms | 24448 KB | Output is correct |
2 | Correct | 33 ms | 25728 KB | Output is correct |
3 | Correct | 29 ms | 24448 KB | Output is correct |
4 | Correct | 27 ms | 24312 KB | Output is correct |
# | 결과 | 실행 시간 | 메모리 | Grader output |
---|---|---|---|---|
1 | Correct | 39 ms | 26448 KB | Output is correct |
2 | Correct | 56 ms | 28148 KB | Output is correct |
# | 결과 | 실행 시간 | 메모리 | Grader output |
---|---|---|---|---|
1 | Correct | 101 ms | 30500 KB | Output is correct |
2 | Correct | 97 ms | 37604 KB | Output is correct |
3 | Correct | 144 ms | 41060 KB | Output is correct |
# | 결과 | 실행 시간 | 메모리 | Grader output |
---|---|---|---|---|
1 | Correct | 162 ms | 36984 KB | Output is correct |
2 | Correct | 193 ms | 48656 KB | Output is correct |
3 | Correct | 220 ms | 63964 KB | Output is correct |
4 | Correct | 282 ms | 66268 KB | Output is correct |
# | 결과 | 실행 시간 | 메모리 | Grader output |
---|---|---|---|---|
1 | Correct | 299 ms | 36572 KB | Output is correct |
2 | Correct | 603 ms | 80244 KB | Output is correct |
3 | Correct | 302 ms | 46712 KB | Output is correct |
4 | Correct | 399 ms | 70592 KB | Output is correct |
5 | Correct | 384 ms | 68444 KB | Output is correct |
6 | Correct | 824 ms | 51512 KB | Output is correct |
7 | Correct | 442 ms | 91440 KB | Output is correct |
# | 결과 | 실행 시간 | 메모리 | Grader output |
---|---|---|---|---|
1 | Correct | 457 ms | 125304 KB | Output is correct |
2 | Correct | 407 ms | 96504 KB | Output is correct |
3 | Correct | 461 ms | 131072 KB | Output is correct |
4 | Correct | 401 ms | 78840 KB | Output is correct |
5 | Correct | 384 ms | 82700 KB | Output is correct |
6 | Correct | 372 ms | 73060 KB | Output is correct |
7 | Correct | 809 ms | 67252 KB | Output is correct |