# | 제출 시각 | 아이디 | 문제 | 언어 | 결과 | 실행 시간 | 메모리 |
---|---|---|---|---|---|---|---|
198880 | 2020-01-28T01:16:28 Z | arnold518 | Islands (IOI08_islands) | C++14 | 2000 ms | 130656 KB |
#include <bits/stdc++.h> using namespace std; typedef long long ll; typedef pair<int, int> pii; typedef pair<ll, ll> pll; const int MAXN = 1e6; int N; vector<pii> adj[MAXN+1]; pii to[MAXN+1]; ll ans, val; bool vis[MAXN+1]; vector<int> cyc; ll A[MAXN+1], D[MAXN+1]; int AS, DS; ll dfs(int now, ll dep) { vis[now]=true; ll ret=dep; ll ta=0, tb=0; for(pii &nxt : adj[now]) { if(vis[nxt.first]) continue; ll p=dfs(nxt.first, dep+nxt.second); ret=max(ret, p); if(p>ta) tb=ta, ta=p; else if(p>tb) tb=p; } val=max(val, ta+tb-dep-dep); return ret; } int main() { int i, j; scanf("%d", &N); for(i=1; i<=N; i++) { int v, w; scanf("%d%d", &v, &w); to[i]={v, w}; adj[v].push_back({i, w}); } for(i=1; i<=N; i++) { if(vis[i]) continue; int now=i; cyc.clear(); while(!vis[now]) { cyc.push_back(now); vis[now]=1; now=to[now].first; } reverse(cyc.begin(), cyc.end()); while(!cyc.empty() && cyc.back()!=now) vis[cyc.back()]=0, cyc.pop_back(); reverse(cyc.begin(), cyc.end()); AS=DS=0; ll s=0; val=0; D[DS++]=0; for(i=0; i<cyc.size(); i++) A[AS++]=dfs(cyc[i], 0); for(i=0; i<cyc.size(); i++) D[DS++]=(D[DS-1]+to[cyc[i]].second), s+=to[cyc[i]].second; DS--; ll t=-1e18; for(j=0; j<cyc.size(); j++) { if(j) val=max(val, D[j]+A[j]+t); t=max(t, A[j]-D[j]); } t=-1e18; for(j=0; j<cyc.size(); j++) { if(j) val=max(val, s-D[j]+A[j]+t); t=max(t, D[j]+A[j]); } ans+=val; } printf("%lld", ans); }
Compilation message
# | 결과 | 실행 시간 | 메모리 | Grader output |
---|---|---|---|---|
1 | Correct | 19 ms | 23800 KB | Output is correct |
2 | Correct | 19 ms | 23804 KB | Output is correct |
3 | Correct | 19 ms | 23800 KB | Output is correct |
4 | Correct | 19 ms | 23800 KB | Output is correct |
5 | Correct | 19 ms | 23800 KB | Output is correct |
6 | Correct | 18 ms | 23800 KB | Output is correct |
7 | Correct | 19 ms | 23800 KB | Output is correct |
8 | Correct | 19 ms | 23804 KB | Output is correct |
9 | Correct | 19 ms | 23800 KB | Output is correct |
10 | Correct | 18 ms | 23800 KB | Output is correct |
11 | Correct | 19 ms | 23800 KB | Output is correct |
# | 결과 | 실행 시간 | 메모리 | Grader output |
---|---|---|---|---|
1 | Correct | 19 ms | 23800 KB | Output is correct |
2 | Correct | 20 ms | 23928 KB | Output is correct |
# | 결과 | 실행 시간 | 메모리 | Grader output |
---|---|---|---|---|
1 | Correct | 22 ms | 23928 KB | Output is correct |
2 | Correct | 21 ms | 24056 KB | Output is correct |
# | 결과 | 실행 시간 | 메모리 | Grader output |
---|---|---|---|---|
1 | Correct | 27 ms | 24312 KB | Output is correct |
2 | Correct | 35 ms | 25080 KB | Output is correct |
3 | Correct | 29 ms | 24440 KB | Output is correct |
4 | Correct | 24 ms | 24188 KB | Output is correct |
# | 결과 | 실행 시간 | 메모리 | Grader output |
---|---|---|---|---|
1 | Correct | 41 ms | 25596 KB | Output is correct |
2 | Correct | 60 ms | 27384 KB | Output is correct |
# | 결과 | 실행 시간 | 메모리 | Grader output |
---|---|---|---|---|
1 | Correct | 114 ms | 30072 KB | Output is correct |
2 | Correct | 97 ms | 34420 KB | Output is correct |
3 | Correct | 122 ms | 33776 KB | Output is correct |
# | 결과 | 실행 시간 | 메모리 | Grader output |
---|---|---|---|---|
1 | Correct | 179 ms | 36600 KB | Output is correct |
2 | Correct | 196 ms | 40812 KB | Output is correct |
3 | Correct | 211 ms | 52716 KB | Output is correct |
4 | Correct | 276 ms | 49380 KB | Output is correct |
# | 결과 | 실행 시간 | 메모리 | Grader output |
---|---|---|---|---|
1 | Correct | 355 ms | 40528 KB | Output is correct |
2 | Correct | 725 ms | 63488 KB | Output is correct |
3 | Correct | 341 ms | 46968 KB | Output is correct |
4 | Correct | 396 ms | 57704 KB | Output is correct |
5 | Correct | 382 ms | 55656 KB | Output is correct |
6 | Correct | 937 ms | 52984 KB | Output is correct |
7 | Correct | 423 ms | 62816 KB | Output is correct |
# | 결과 | 실행 시간 | 메모리 | Grader output |
---|---|---|---|---|
1 | Correct | 471 ms | 130656 KB | Output is correct |
2 | Correct | 454 ms | 95568 KB | Output is correct |
3 | Correct | 428 ms | 83552 KB | Output is correct |
4 | Execution timed out | 2084 ms | 63480 KB | Time limit exceeded |
5 | Halted | 0 ms | 0 KB | - |