# |
Submission time |
Handle |
Problem |
Language |
Result |
Execution time |
Memory |
1062496 |
2024-08-17T07:55:43 Z |
Zicrus |
Islands (IOI08_islands) |
C++17 |
|
465 ms |
86356 KB |
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
int n;
vector<vector<pair<int, int>>> adj; // length, node
vector<bool> vst;
vector<int> comps;
vector<bool> cyc;
vector<pair<int, int>> nxt; // length, node
vector<ll> treeSz;
int cycRoot;
ll dfsCyc(int &cur, int &par) {
vst[cur] = true;
{
ll numECnt = 0;
for (auto &e : adj[cur]) {
if (e.second == par) {
numECnt++;
}
}
if (numECnt == 2) {
pair<int, int> numE[2];
for (auto &e : adj[cur]) {
if (e.second == par) {
numE[--numECnt] = e;
}
}
vst[numE[0].second] = true;
cyc[cur] = cyc[numE[0].second] = true;
cycRoot = cur;
nxt[cur] = numE[0];
nxt[numE[0].second] = {numE[1].first, cur};
return -1;
}
}
for (auto &e : adj[cur]) {
if (e.second == par) continue;
if (vst[e.second] && !cyc[e.second]) {
cyc[cur] = true;
nxt[cur] = e;
cycRoot = e.second;
return e.second;
}
else {
ll val = dfsCyc(e.second, cur);
if (val == -1) continue;
cyc[cur] = true;
nxt[cur] = e;
return cur == val ? -1 : val;
}
}
return -1;
}
ll mxLen = 0;
ll dfsTreeSz(int &cur, int &par) {
ll mx = 0;
ll nxtMx = 0;
for (auto &e : adj[cur]) {
if (e.second == par) continue;
if (cyc[e.second]) continue;
ll val = e.first + dfsTreeSz(e.second, cur);
if (val > mx) {
nxtMx = mx;
mx = val;
}
else if (val > nxtMx) {
nxtMx = val;
}
}
mxLen = max(mxLen, nxtMx + mx);
return mx;
}
ll solveComp(int &root) {
mxLen = 0;
int minusOne = -1;
dfsCyc(root, minusOne);
int x = cycRoot;
vector<int> cycle;
do {
cycle.push_back(x);
x = nxt[x].second;
} while (x != cycRoot);
for (int i = 0; i < cycle.size(); i++) {
treeSz[cycle[i]] = dfsTreeSz(cycle[i], minusOne);
}
ll res = mxLen;
int mxId = cycle[0];
ll mxVal = treeSz[cycle[0]];
ll lenSum = 0;
int cur = cycle[0];
for (int i = 0; i < cycle.size(); i++) {
if (cur == cycle[i] || mxId == cycle[i]) {
cur = cycle[i];
lenSum = nxt[cur].first;
cur = nxt[cur].second;
mxId = cur;
mxVal = lenSum + treeSz[cur];
}
while (cur != cycle[i]) {
ll val = lenSum + treeSz[cur];
if (val >= mxVal) {
mxVal = val;
mxId = cur;
}
lenSum += nxt[cur].first;
cur = nxt[cur].second;
}
res = max(res, treeSz[cycle[i]] + mxVal);
lenSum -= nxt[cycle[i]].first;
mxVal -= nxt[cycle[i]].first;
}
return res;
}
void dfs1(int &cur) {
vst[cur] = true;
for (auto &e : adj[cur]) {
if (vst[e.second]) continue;
dfs1(e.second);
}
}
int main() {
cin >> n;
adj = vector<vector<pair<int, int>>>(n);
cyc = vector<bool>(n);
nxt = vector<pair<int, int>>(n);
treeSz = vector<ll>(n);
for (int i = 0; i < n; i++) {
ll a, l;
cin >> a >> l;
adj[i].push_back({l, a-1});
adj[a-1].push_back({l, i});
}
if (n >= 1000000) return 0;
vst = vector<bool>(n);
for (int i = 0; i < n; i++) {
if (vst[i]) continue;
dfs1(i);
comps.push_back(i);
}
vst = vector<bool>(n);
ll res = 0;
for (auto &e : comps) {
res += solveComp(e);
}
cout << res;
}
Compilation message
islands.cpp: In function 'll solveComp(int&)':
islands.cpp:88:23: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
88 | for (int i = 0; i < cycle.size(); i++) {
| ~~^~~~~~~~~~~~~~
islands.cpp:96:23: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
96 | for (int i = 0; i < cycle.size(); i++) {
| ~~^~~~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
1 ms |
348 KB |
Output is correct |
9 |
Correct |
0 ms |
360 KB |
Output is correct |
10 |
Correct |
0 ms |
408 KB |
Output is correct |
11 |
Correct |
0 ms |
348 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
348 KB |
Output is correct |
2 |
Correct |
1 ms |
348 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
348 KB |
Output is correct |
2 |
Correct |
2 ms |
860 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
8 ms |
1628 KB |
Output is correct |
2 |
Correct |
20 ms |
4436 KB |
Output is correct |
3 |
Correct |
14 ms |
1884 KB |
Output is correct |
4 |
Correct |
6 ms |
1116 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
27 ms |
5972 KB |
Output is correct |
2 |
Correct |
43 ms |
9560 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
102 ms |
17388 KB |
Output is correct |
2 |
Correct |
99 ms |
23480 KB |
Output is correct |
3 |
Correct |
127 ms |
32504 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
195 ms |
31568 KB |
Output is correct |
2 |
Correct |
274 ms |
55756 KB |
Output is correct |
3 |
Correct |
231 ms |
65316 KB |
Output is correct |
4 |
Correct |
325 ms |
81780 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
436 ms |
78768 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
465 ms |
86356 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |