#include <iostream>
#include <vector>
#include <algorithm>
#include <cmath>
#include <cassert>
using namespace std;
using ll = long long;
#define MIN(a, b) (((a) < (b)) ? (a) : (b))
#define MAX(a, b) (((a) < (b)) ? (b) : (a))
int const nmax = 100000;
int const inf = 1000000000;
int edge[5 + nmax][3], seen[5 + nmax];
vector<int> g[5 + nmax];
void mark(int node){
if(seen[node] == 1)
return ;
seen[node] = 1;
for(int h = 0; h < g[node].size(); h++)
mark(g[node][h]);
}
vector<int> st, cycle;
int in_stack[1 + nmax];
int oth(int node, int id){
return edge[id][0] + edge[id][1] - node;
}
bool exist(int node, int id){
return ((edge[id][0] == node) || (edge[id][1] == node));
}
void dfs(int node, bool &done){
if(in_stack[node] == 1){
cycle.push_back(st.back());
st.pop_back();
while(exist(node, st.back()) == 0){
cycle.push_back(st.back());
st.pop_back();
}
cycle.push_back(st.back());
st.pop_back();
done = 1;
}
in_stack[node] = 1;
for(int h = 0; h < g[node].size(); h++){
int id = g[node][h];
if(done == 1)
return ;
if(st.size() == 0 || st.back() != id) {
st.push_back(id);
dfs(oth(node, id), done);
st.pop_back();
}
}
in_stack[node] = 0;
}
ll dp[1 + nmax];
ll maxdist(int node){
for(int h = 0; h < g[node].size(); h++){
int id = g[node][h];
int to = oth(node, id);
if(seen[to] < 2) {
maxdist(to);
dp[node] = max(dp[node], dp[to] + edge[id][2]);
}
}
return dp[node];
}
ll solve(int node){
cycle.clear();
st.clear();
mark(node);
bool done = 0;
dfs(node, done);
ll sum = 0;
for(int i = 0; i < cycle.size(); i++) {
int id = cycle[i];
seen[edge[id][0]] = 2;
seen[edge[id][1]] = 2;
sum += edge[id][2];
}
vector<int> path;
if(exist(edge[cycle[0]][0], cycle[1]) == 1)
path.push_back(edge[cycle[0]][0]);
else
path.push_back(edge[cycle[0]][1]);
for(int i = 1; i < cycle.size(); i++)
path.push_back(oth(path.back(), cycle[i]));
ll result = 0, smax1 = -inf, smax2 = -inf, part = 0;
for(int i = 0; i < path.size(); i++){
if(0 < i) {
part += edge[cycle[i]][2];
result = max(result, dp[path[i]] + part + smax1);
result = max(result, dp[path[i]] + sum - part + smax2);
}
smax1 = max(smax1, dp[path[i]] - part);
smax2 = max(smax2, dp[path[i]] + part);
}
return result;
}
int main()
{
int n;
cin >> n;
for(int i = 1; i <= n; i++){
edge[i][0] = i;
cin >> edge[i][1] >> edge[i][2];
g[edge[i][0]].push_back(i);
g[edge[i][1]].push_back(i);
}
ll result = 0;
for(int i = 1;i <= n; i++)
if(seen[i] == 0)
result += solve(i);
cout << result;
return 0;
}
Compilation message
islands.cpp: In function 'void mark(int)':
islands.cpp:23:20: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for(int h = 0; h < g[node].size(); h++)
~~^~~~~~~~~~~~~~~~
islands.cpp: In function 'void dfs(int, bool&)':
islands.cpp:52:20: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for(int h = 0; h < g[node].size(); h++){
~~^~~~~~~~~~~~~~~~
islands.cpp: In function 'll maxdist(int)':
islands.cpp:69:20: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for(int h = 0; h < g[node].size(); h++){
~~^~~~~~~~~~~~~~~~
islands.cpp: In function 'll solve(int)':
islands.cpp:87:20: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for(int i = 0; i < cycle.size(); i++) {
~~^~~~~~~~~~~~~~
islands.cpp:99:20: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for(int i = 1; i < cycle.size(); i++)
~~^~~~~~~~~~~~~~
islands.cpp:104:20: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for(int i = 0; i < path.size(); i++){
~~^~~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
6 ms |
2688 KB |
Output is correct |
2 |
Runtime error |
9 ms |
5376 KB |
Execution killed with signal 11 (could be triggered by violating memory limits) |
3 |
Correct |
6 ms |
2688 KB |
Output is correct |
4 |
Incorrect |
6 ms |
2688 KB |
Output isn't correct |
5 |
Correct |
6 ms |
2688 KB |
Output is correct |
6 |
Incorrect |
6 ms |
2688 KB |
Output isn't correct |
7 |
Incorrect |
7 ms |
2688 KB |
Output isn't correct |
8 |
Runtime error |
9 ms |
5248 KB |
Execution killed with signal 11 (could be triggered by violating memory limits) |
9 |
Incorrect |
6 ms |
2688 KB |
Output isn't correct |
10 |
Incorrect |
6 ms |
2688 KB |
Output isn't correct |
11 |
Correct |
6 ms |
2688 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Runtime error |
10 ms |
5376 KB |
Execution killed with signal 11 (could be triggered by violating memory limits) |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Runtime error |
11 ms |
5504 KB |
Execution killed with signal 11 (could be triggered by violating memory limits) |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Runtime error |
32 ms |
6808 KB |
Execution killed with signal 11 (could be triggered by violating memory limits) |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Runtime error |
78 ms |
13544 KB |
Execution killed with signal 11 (could be triggered by violating memory limits) |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Runtime error |
36 ms |
7928 KB |
Execution killed with signal 11 (could be triggered by violating memory limits) |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Runtime error |
112 ms |
14112 KB |
Execution killed with signal 11 (could be triggered by violating memory limits) |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Runtime error |
8 ms |
5248 KB |
Execution killed with signal 11 (could be triggered by violating memory limits) |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Runtime error |
32 ms |
7296 KB |
Execution killed with signal 11 (could be triggered by violating memory limits) |
2 |
Halted |
0 ms |
0 KB |
- |