#include <bits/stdc++.h>
using namespace std;
#define N 1000001
vector<pair<int, long long> > adj[N];
bool in[N];
vector<int> s;
vector<int> cyc;
vector<long long> dp, pr;
bool inCyc[N];
long long res;
long long dfs(int v, int p){
long long b1 = 0, b2 = 0;
for(auto xx : adj[v]){
int x = xx.first;
long long c = xx.second;
if(inCyc[x] || p == x) continue;
auto q = dfs(x, v);
long long up;
up = q + c;
if(b1 < up){
b2 = b1;
b1 = up;
} else b2 = max(b2, up);
}
res = max(res, b1 + b2);
return b1;
}
long long ans = 0;
unordered_map<int, unordered_map<int, int> > lol;
vector<long long> p;
void solve(){
dp.clear();
p.clear();
cyc.clear();
res = 0;
for(int x : s){
if(inCyc[x]) {
p.push_back(p.empty() ? 0 : p.back() + lol[x][cyc.back()]);
cyc.push_back(x);
dp.push_back(dfs(x, 0));
}
}
long long tmp = dp[0] - p[0];
for(int i = 1; i < cyc.size(); i++){
res = max(res, tmp + dp[i] + p[i]);
tmp = max(tmp, dp[i] - p[i]);
}
reverse(p.begin(), p.end());
for(auto& x : p){
x = p.back() - x;
}
reverse(cyc.begin(), cyc.end());
reverse(dp.begin(), dp.end());
tmp = dp[0] - p[0];
for(int i = 1; i < cyc.size(); i++){
res = max(res, tmp + dp[i] + p[i]);
tmp = max(tmp, dp[i] - p[i]);
}
ans += res;
}
int trav(int i, int p){
s.push_back(i);
in[i] = 1;
int res = 0;
bool ok = 0;
for(auto xx : adj[i]){
int x = xx.first;
if(p == x && ok == 0){
ok = 1;
continue;
}
if(in[x]){
res = x;
continue;
}
int t = trav(x, i);
if(res < t)res = t;
}
if(res) inCyc[i] = 1;
if(res == i) return 0;
return res;
}
int main(){
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
int n;
cin>>n;
for(int i = 1; i <= n; i++){
int l, e;
cin>>e>>l;
adj[e].push_back({i, l});
adj[i].push_back({e, l});
lol[e][i] = max(lol[e][i], l);
lol[i][e] = max(lol[i][e], l);
}
for(int i = 1; i <= n; i++){
if(in[i]) continue;
s.clear();
trav(i, 0);
solve();
}
cout<<ans;
}
Compilation message
islands.cpp: In function 'void solve()':
islands.cpp:50:19: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for(int i = 1; i < cyc.size(); i++){
~~^~~~~~~~~~~~
islands.cpp:62:19: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for(int i = 1; i < cyc.size(); i++){
~~^~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
23 ms |
23808 KB |
Output is correct |
2 |
Incorrect |
22 ms |
23960 KB |
Output isn't correct |
3 |
Incorrect |
22 ms |
23928 KB |
Output isn't correct |
4 |
Correct |
21 ms |
23808 KB |
Output is correct |
5 |
Incorrect |
22 ms |
23800 KB |
Output isn't correct |
6 |
Correct |
22 ms |
23808 KB |
Output is correct |
7 |
Incorrect |
21 ms |
23936 KB |
Output isn't correct |
8 |
Incorrect |
22 ms |
23808 KB |
Output isn't correct |
9 |
Incorrect |
22 ms |
23808 KB |
Output isn't correct |
10 |
Correct |
22 ms |
23792 KB |
Output is correct |
11 |
Correct |
23 ms |
23808 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
23 ms |
24152 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
25 ms |
24320 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
47 ms |
27944 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
121 ms |
39080 KB |
Output is correct |
2 |
Incorrect |
153 ms |
51220 KB |
Output isn't correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
370 ms |
76572 KB |
Output is correct |
2 |
Correct |
367 ms |
88556 KB |
Output is correct |
3 |
Incorrect |
443 ms |
105524 KB |
Output isn't correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
544 ms |
125212 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Runtime error |
409 ms |
131072 KB |
Execution killed with signal 9 (could be triggered by violating memory limits) |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Runtime error |
406 ms |
131072 KB |
Execution killed with signal 9 (could be triggered by violating memory limits) |
2 |
Halted |
0 ms |
0 KB |
- |