| # | 제출 시각 | 아이디 | 문제 | 언어 | 결과 | 실행 시간 | 메모리 |
|---|---|---|---|---|---|---|---|
| 233614 | dolphingarlic | Islands (IOI08_islands) | C++14 | 646 ms | 131072 KiB |
이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
/*
IOI 2008 Islands
- First, split the graph up into its connected components. We want the sum of the longest
paths in each component
- Notice how each component is made of a single cycle with trees appended to each node on the cycle
- Case 1: The longest path lies in one of the trees
- Just use DP to find this length
- Case 2: The longest path goes from one tree to the other and crosses the cycle
- Let dp[i] = The length of the longest path going through node i
- If we flatten the cycle by removing an edge, then notice how
we can use prefix sums to get the answer in this case
- Just handle these 2 cases for each component
- Complexity: O(N)
*/
#include <bits/stdc++.h>
#define FOR(i, x, y) for (int i = x; i < y; i++)
typedef long long ll;
using namespace std;
struct Edge {
int idx, first;
ll second;
bool operator!=(Edge B) { return idx != B.idx; }
};
vector<Edge> graph[1000001], cycle;
ll discarded, ans = 0, cmp_ans, dp[1000001], p[5][1000001];
int cycle_start = 0;
bool processed[1000001], visited[1000001], on_cycle[1000001], adding = false;
void find_cycle(int node, Edge parent = {0, 0, 0}) {
if (visited[node]) {
cycle_start = node;
cycle.push_back({0, node, 0});
cycle.push_back(parent);
adding = true;
return;
}
visited[node] = true;
for (Edge i : graph[node]) if (i != parent) {
find_cycle(i.first, {i.idx, node, i.second});
if (cycle_start) {
if (node == cycle_start) adding = false;
if (adding) cycle.push_back(parent);
return;
}
}
}
void calc_tree(int node, int parent = 0) {
processed[node] = true;
ll mx = 0, sc = 0;
for (Edge i : graph[node]) if (i.first != parent && !on_cycle[i.first]) {
calc_tree(i.first, node);
if (dp[i.first] + i.second > mx) sc = mx, mx = dp[i.first] + i.second;
else if (dp[i.first] + i.second > sc) sc = dp[i.first] + i.second;
}
dp[node] = mx;
cmp_ans = max(cmp_ans, mx + sc);
}
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
int n;
cin >> n;
FOR(i, 1, n + 1) {
int v;
ll l;
cin >> v >> l;
graph[i].push_back({i, v, l});
graph[v].push_back({i, i, l});
}
FOR(i, 1, n + 1) if (!processed[i]) {
cycle.clear();
cycle_start = 0;
find_cycle(i);
discarded = cycle.back().second;
cycle.pop_back();
for (Edge j : cycle) on_cycle[j.first] = true;
cmp_ans = 0;
FOR(j, 1, cycle.size()) p[0][j] = p[0][j - 1] + cycle[j].second;
p[1][cycle.size() - 1] = 0;
for (int j = cycle.size() - 2; j >= 0; j--) p[1][j] = p[1][j + 1] + cycle[j + 1].second;
FOR(j, 0, cycle.size()) {
calc_tree(cycle[j].first);
p[2][j] = dp[cycle[j].first] - p[0][j];
p[3][j] = dp[cycle[j].first] + p[0][j];
p[4][j] = dp[cycle[j].first] + p[1][j];
}
ll mx = 0;
FOR(j, 0, cycle.size()) {
cmp_ans = max(cmp_ans, p[3][j] + mx);
mx = max(mx, p[2][j]);
}
mx = p[4][cycle.size() - 1];
for (int j = cycle.size() - 2; j >= 0; j--) {
cmp_ans = max(cmp_ans, p[3][j] + mx + discarded);
mx = max(mx, p[4][j]);
}
ans += cmp_ans;
}
cout << 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... | ||||
