# | 제출 시각 | 아이디 | 문제 | 언어 | 결과 | 실행 시간 | 메모리 |
---|---|---|---|---|---|---|---|
57248 | PeppaPig | Islands (IOI08_islands) | C++14 | 465 ms | 49976 KiB |
이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#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;
pll par[N], dp[N];
long long dm[N], ans;
int deg[N];
bitset<N> vis;
int main() {
scanf("%d", &n);
for(int i = 1; i <= n; i++) {
long long v, w;
scanf("%lld%lld", &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();
dm[now] = max(dm[now], dp[now].x + dp[now].y);
dm[par[now].x] = max(dm[par[now].x], dm[now]);
long long dis = dp[now].x + par[now].y;
if(dis > dp[par[now].x].x) swap(dis, dp[par[now].x].x);
if(dis > dp[par[now].x].y) swap(dis, dp[par[now].x].y);
if(!--deg[par[now].x]) Q.emplace(par[now].x);
}
for(int k = 1; k <= n; k++) if(!vis[k] && deg[k]) {
int p = k;
vector<pll> cyc;
deque<pll> Q;
while(!vis[p]) {
vis[p] = true;
dm[p] = max(dm[p], dp[p].x + dp[p].y);
cyc.emplace_back(p, par[p].y);
p = par[p].x;
}
long long ret = 0, dis = cyc[0].y, pdis = 0;
for(int i = 1; i <= n; i++) ret = max(ret, max(dp[ret].x, dm[ret]));
for(int i = 1; i < cyc.size(); i++) {
while(!Q.empty() && Q.back().x < dis + dp[cyc[i].x].x) Q.pop_back();
Q.emplace_back(dis + dp[cyc[i].x].x, i);
dis += cyc[i].y;
}
ret = max(ret, dp[cyc[0].x].x + Q.front().x);
for(int i = 0; i < cyc.size() - 1; i++) {
while(!Q.empty() && Q.front().y <= i + 1) Q.pop_front();
while(!Q.empty() && Q.back().x < dis + dp[cyc[i].x].x) Q.pop_back();
Q.emplace_back(dis + dp[cyc[i].x].x, i);
dis += cyc[i].y;
pdis += cyc[i].y;
ret = max(ret, dp[cyc[i + 1].x].x + Q.front().x - pdis);
}
ans += ret;
}
printf("%lld", 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... |