# | 제출 시각 | 아이디 | 문제 | 언어 | 결과 | 실행 시간 | 메모리 |
---|---|---|---|---|---|---|---|
56536 | aquablitz11 | Islands (IOI08_islands) | C++14 | 582 ms | 132096 KiB |
이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
const int N = 1000010;
const ll INF = 1e18;
ll par[N], len[N], deg[N], h1[N], h2[N], dp[N];
int main()
{
int n;
scanf("%d", &n);
for (int i = 1; i <= n; ++i) {
int v, w;
scanf("%d%d", &v, &w);
par[i] = v;
len[i] = w;
++deg[v];
}
queue<int> Q;
for (int i = 1; i <= n; ++i) {
if (deg[i] == 0)
Q.push(i);
}
while (!Q.empty()) {
int u = Q.front(); Q.pop();
int v = par[u];
ll w = len[u];
if (h1[u]+w > h1[v]) {
h2[v] = h1[v];
h1[v] = h1[u]+w;
} else if (h1[u]+w > h2[v]) {
h2[v] = h1[u]+w;
}
dp[u] = max(dp[u], h1[u]+h2[u]);
dp[v] = max(dp[v], dp[u]);
if (--deg[v] == 0)
Q.push(v);
}
ll ttans = 0;
for (int i = 1; i <= n; ++i) {
if (deg[i] == 1) {
int u = i;
vector<int> cyc;
ll ttd = 0;
do {
cyc.push_back(u);
deg[u] = 0;
ttd += len[u];
u = par[u];
} while (u != i);
ll mxm = -INF, mxp = -INF, d = 0, ans = -INF;
for (auto u : cyc) {
dp[u] = max(dp[u], h1[u]+h2[u]);
ans = max(ans, dp[u]);
ans = max(ans, h1[u]+d+mxm);
ans = max(ans, h1[u]-d+ttd+mxp);
mxm = max(mxm, h1[u]-d);
mxp = max(mxp, h1[u]+d);
d += len[u];
}
ttans += ans;
}
}
printf("%lld\n", ttans);
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... |