#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
int n;
vector<list<pair<int, int>>> adj; // length, node
vector<bool> vst;
vector<int> comps;
vector<bool> cyc;
vector<pair<int, int>> nxt; // length, node
vector<int> globToCyc;
int cycRoot;
ll dfsCyc(int &cur, int &par) {
vst[cur] = true;
{
vector<pair<int, int>> numE;
for (auto &e : adj[cur]) {
if (e.second == par) {
numE.push_back(e);
}
}
if (numE.size() == 2) {
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);
ll totLen = 0;
int x = cycRoot;
vector<int> 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], minusOne);
}
ll res = mxLen;
int mxId = cycle[0];
ll mxVal = treeSz[0];
ll lenSum = 0;
int cur = cycle[0];
for (int i = 0; i < cycle.size(); i++) {
if (cur == cycle[i] || mxId == i) {
cur = 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(int &cur) {
vst[cur] = true;
for (auto &e : adj[cur]) {
if (vst[e.second]) continue;
dfs1(e.second);
}
}
int main() {
cin >> n;
adj = vector<list<pair<int, int>>>(n);
cyc = vector<bool>(n);
nxt = vector<pair<int, int>>(n);
globToCyc = vector<int>(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});
}
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:86:23: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
86 | for (int i = 0; i < cycle.size(); i++) {
| ~~^~~~~~~~~~~~~~
islands.cpp:94:23: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
94 | for (int i = 0; i < cycle.size(); i++) {
| ~~^~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
348 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 |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
0 ms |
344 KB |
Output is correct |
11 |
Correct |
0 ms |
348 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
344 KB |
Output is correct |
2 |
Correct |
1 ms |
344 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
604 KB |
Output is correct |
2 |
Correct |
2 ms |
860 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
7 ms |
2136 KB |
Output is correct |
2 |
Correct |
19 ms |
5212 KB |
Output is correct |
3 |
Correct |
14 ms |
2392 KB |
Output is correct |
4 |
Correct |
5 ms |
1276 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
27 ms |
7260 KB |
Output is correct |
2 |
Correct |
37 ms |
11780 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
80 ms |
21836 KB |
Output is correct |
2 |
Correct |
84 ms |
30100 KB |
Output is correct |
3 |
Correct |
120 ms |
38356 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
152 ms |
41104 KB |
Output is correct |
2 |
Correct |
195 ms |
67564 KB |
Output is correct |
3 |
Correct |
216 ms |
83472 KB |
Output is correct |
4 |
Correct |
295 ms |
97800 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
427 ms |
98388 KB |
Output is correct |
2 |
Runtime error |
591 ms |
131072 KB |
Execution killed with signal 9 |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Runtime error |
426 ms |
131072 KB |
Execution killed with signal 9 |
2 |
Halted |
0 ms |
0 KB |
- |