# |
Submission time |
Handle |
Problem |
Language |
Result |
Execution time |
Memory |
1059289 |
2024-08-14T20:29:07 Z |
Zicrus |
Islands (IOI08_islands) |
C++17 |
|
419 ms |
131072 KB |
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
ll n;
vector<vector<pair<ll, ll>>> adj; // length, node
vector<ll> rep;
vector<bool> vst;
vector<ll> comps;
vector<bool> cyc;
vector<pair<ll, ll>> nxt; // length, node
vector<ll> globToCyc;
ll cycRoot;
ll dfsCyc(ll cur, ll par) {
vst[cur] = true;
vector<pair<ll, ll>> numE;
for (auto &e : adj[cur]) {
if (e.second == par) continue;
numE.push_back(e);
}
if (numE.size() == 2 && par == -1) {
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(ll cur, ll 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(ll root) {
mxLen = 0;
dfsCyc(root, -1);
ll totLen = 0;
ll x = cycRoot;
vector<ll> cycle;
do {
globToCyc[x] = cycle.size();
cycle.push_back(x);
totLen += nxt[x].first;
x = nxt[x].second;
} while (x != cycRoot);
vector<ll> treeSz(cycle.size());
for (int i = 0; i < cycle.size(); i++) {
treeSz[i] = dfsTreeSz(cycle[i], -1);
}
ll res = mxLen;
ll mxId = cycle[0], mxVal = treeSz[0];
ll lenSum = 0;
ll cur = cycle[0];
for (int i = 0; i < cycle.size(); i++) {
if (cur == cycle[i] || mxId == cycle[i]) {
lenSum = nxt[cur].first;
cur = nxt[cur].second;
mxId = globToCyc[cur];
mxVal = lenSum + treeSz[globToCyc[cur]];
}
while (cur != cycle[i]) {
ll val = lenSum + treeSz[globToCyc[cur]];
if (val >= mxVal) {
mxVal = val;
mxId = globToCyc[cur];
}
lenSum += nxt[cur].first;
cur = nxt[cur].second;
}
res = max(res, treeSz[i] + mxVal);
lenSum -= nxt[cycle[i]].first;
mxVal -= nxt[cycle[i]].first;
}
return res;
}
void dfs1(ll cur, ll rep1) {
vst[cur] = true;
rep[cur] = rep1;
for (auto &e : adj[cur]) {
if (vst[e.second]) continue;
dfs1(e.second, rep1);
}
}
int main() {
cin >> n;
adj = vector<vector<pair<ll, ll>>>(n);
cyc = vector<bool>(n);
nxt = vector<pair<ll, ll>>(n);
globToCyc = 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});
}
rep = vector<ll>(n);
vst = vector<bool>(n);
for (int i = 0; i < n; i++) {
if (vst[i]) continue;
dfs1(i, 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(ll)':
islands.cpp:83:23: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
83 | for (int i = 0; i < cycle.size(); i++) {
| ~~^~~~~~~~~~~~~~
islands.cpp:90:23: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
90 | for (int i = 0; i < cycle.size(); i++) {
| ~~^~~~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
1 ms |
348 KB |
Output isn't correct |
2 |
Incorrect |
0 ms |
348 KB |
Output isn't correct |
3 |
Incorrect |
0 ms |
348 KB |
Output isn't correct |
4 |
Runtime error |
88 ms |
131072 KB |
Execution killed with signal 9 |
5 |
Incorrect |
1 ms |
348 KB |
Output isn't correct |
6 |
Runtime error |
91 ms |
131072 KB |
Execution killed with signal 9 |
7 |
Runtime error |
87 ms |
131072 KB |
Execution killed with signal 9 |
8 |
Runtime error |
87 ms |
131072 KB |
Execution killed with signal 9 |
9 |
Runtime error |
90 ms |
131072 KB |
Execution killed with signal 9 |
10 |
Incorrect |
0 ms |
348 KB |
Output isn't correct |
11 |
Correct |
0 ms |
348 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
604 KB |
Output is correct |
2 |
Correct |
1 ms |
604 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Runtime error |
89 ms |
131072 KB |
Execution killed with signal 9 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
8 ms |
2396 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
27 ms |
9856 KB |
Output is correct |
2 |
Correct |
42 ms |
15020 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
85 ms |
28056 KB |
Output is correct |
2 |
Incorrect |
82 ms |
32080 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
168 ms |
50352 KB |
Output is correct |
2 |
Correct |
235 ms |
95552 KB |
Output is correct |
3 |
Incorrect |
217 ms |
87636 KB |
Output isn't correct |
4 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
419 ms |
131072 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Runtime error |
419 ms |
131072 KB |
Execution killed with signal 9 |
2 |
Halted |
0 ms |
0 KB |
- |