# | 제출 시각 | 아이디 | 문제 | 언어 | 결과 | 실행 시간 | 메모리 |
---|---|---|---|---|---|---|---|
216630 | songc | Islands (IOI08_islands) | C++14 | 833 ms | 131072 KiB |
이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#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];
LL R[1000010], mx[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;
R[u] = cyc(P[u]);
if (R[u]){
V.pb(u), S.pb(W[u]);
return (R[u]==u)?0:R[u];
}
chk[u] = false;
return 0;
}
inline LL dfs(int u){
mx[u]=0;
chk[u] = true;
for (int v : adj[u]){
if (chk[v]) continue;
R[u] = dfs(v) + W[v];
ret = max(ret, (LL)mx[u]+R[u]);
mx[u] = max(mx[u], R[u]);
}
return mx[u];
}
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;
}
컴파일 시 표준 에러 (stderr) 메시지
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |