#include<bits/stdc++.h>
using namespace std;
const int MAXN = 10000002;
int n, A, par[MAXN], ed, bg;
long long int dist[MAXN], mx_dist, ans, c[MAXN];
vector<int> graph[MAXN], cost[MAXN], tree, cycle;
bool mark[MAXN];
void dfs1(int v, int p)
{
// printf("v = %d p = %d\n", v, p);
tree.push_back(v);
mark[v] = true;
par[v] = p;
bool aux = true;
for(int i = 0; i < graph[v].size(); i++)
{
int cur = graph[v][i];
if(cur == p && aux){ aux = false; continue; }
if(mark[cur])
{
if(!bg && !ed) ed = v, bg = cur, par[cur] = v, c[cur] = cost[v][i];
continue;
}
c[cur] = cost[v][i];
dfs1(cur, v);
}
}
void dfs2(int v, int p)
{
mark[v] = true;
tree.push_back(v);
if(dist[v] > mx_dist) mx_dist = dist[v], A = v;
for(int i = 0; i < graph[v].size(); i++)
{
int cur = graph[v][i];
if(mark[cur] || cur == p) continue;
dist[cur] = dist[v] + cost[v][i];
dfs2(cur, v);
}
}
int main ()
{
scanf("%d", &n);
for(int i = 1, j, l; i <= n; i++)
{
scanf("%d %d", &j, &l);
graph[i].push_back(j), cost[i].push_back(l);
graph[j].push_back(i), cost[j].push_back(l);
}
for(int i = 1; i <= n; i++)
{
if(mark[i]) continue;
bg = ed = 0;
tree.clear(); cycle.clear();
dfs1(i, i);
int cur = ed;
while(cur != bg) cycle.push_back(cur), cur = par[cur];
cycle.push_back(cur);
for(int j = 0; j < tree.size(); j++) mark[tree[j]] = false;
for(int j = 0; j < cycle.size(); j++) mark[cycle[j]] = true;
long long int SCT = 0, SC = 0;
long long int max_DI_SCI[2] = {0, 0}, max_DJ_SCJ[2] = {0, 0}, ANS = 0;
for(int j = 0; j < cycle.size(); j++) SCT += c[cycle[j]];
for(int j = 0; j < cycle.size(); j++)
{
int v = A = cycle[j];
mark[v] = false, mx_dist = dist[v] = 0;
tree.clear();
dfs2(v, v);
ANS = max(ANS, max_DI_SCI[0] + mx_dist + SC);
ANS = max(ANS, max_DI_SCI[1] + mx_dist - SC);
max_DI_SCI[0] = max(max_DI_SCI[0], mx_dist - SC);
max_DI_SCI[1] = max(max_DI_SCI[1], mx_dist + SC + SCT);
SC += c[v];
for(int k = 0; k < tree.size(); k++) mark[tree[k]] = false;
dist[A] = 0; tree.clear();
dfs2(A, A);
ANS = max(ANS, mx_dist);
}
ans += ANS;
}
printf("%lld\n", ans);
}
Compilation message
islands.cpp: In function 'void dfs1(int, int)':
islands.cpp:18:19: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for(int i = 0; i < graph[v].size(); i++)
~~^~~~~~~~~~~~~~~~~
islands.cpp: In function 'void dfs2(int, int)':
islands.cpp:40:19: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for(int i = 0; i < graph[v].size(); i++)
~~^~~~~~~~~~~~~~~~~
islands.cpp: In function 'int main()':
islands.cpp:72:20: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for(int j = 0; j < tree.size(); j++) mark[tree[j]] = false;
~~^~~~~~~~~~~~~
islands.cpp:73:20: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for(int j = 0; j < cycle.size(); j++) mark[cycle[j]] = true;
~~^~~~~~~~~~~~~~
islands.cpp:78:20: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for(int j = 0; j < cycle.size(); j++) SCT += c[cycle[j]];
~~^~~~~~~~~~~~~~
islands.cpp:80:20: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for(int j = 0; j < cycle.size(); j++)
~~^~~~~~~~~~~~~~
islands.cpp:96:21: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for(int k = 0; k < tree.size(); k++) mark[tree[k]] = false;
~~^~~~~~~~~~~~~
islands.cpp:76:41: warning: unused variable 'max_DJ_SCJ' [-Wunused-variable]
long long int max_DI_SCI[2] = {0, 0}, max_DJ_SCJ[2] = {0, 0}, ANS = 0;
^~~~~~~~~~
islands.cpp:51:7: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%d", &n);
~~~~~^~~~~~~~~~
islands.cpp:55:8: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%d %d", &j, &l);
~~~~~^~~~~~~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Runtime error |
68 ms |
131072 KB |
Execution killed with signal 9 (could be triggered by violating memory limits) |
2 |
Runtime error |
68 ms |
131076 KB |
Execution killed with signal 9 (could be triggered by violating memory limits) |
3 |
Runtime error |
68 ms |
131076 KB |
Execution killed with signal 9 (could be triggered by violating memory limits) |
4 |
Runtime error |
66 ms |
131076 KB |
Execution killed with signal 9 (could be triggered by violating memory limits) |
5 |
Runtime error |
67 ms |
131076 KB |
Execution killed with signal 9 (could be triggered by violating memory limits) |
6 |
Runtime error |
72 ms |
131076 KB |
Execution killed with signal 9 (could be triggered by violating memory limits) |
7 |
Runtime error |
67 ms |
131076 KB |
Execution killed with signal 9 (could be triggered by violating memory limits) |
8 |
Runtime error |
65 ms |
131072 KB |
Execution killed with signal 9 (could be triggered by violating memory limits) |
9 |
Runtime error |
63 ms |
131072 KB |
Execution killed with signal 9 (could be triggered by violating memory limits) |
10 |
Runtime error |
65 ms |
131072 KB |
Execution killed with signal 9 (could be triggered by violating memory limits) |
11 |
Runtime error |
69 ms |
131072 KB |
Execution killed with signal 9 (could be triggered by violating memory limits) |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Runtime error |
65 ms |
131076 KB |
Execution killed with signal 9 (could be triggered by violating memory limits) |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Runtime error |
62 ms |
131072 KB |
Execution killed with signal 9 (could be triggered by violating memory limits) |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Runtime error |
64 ms |
131076 KB |
Execution killed with signal 9 (could be triggered by violating memory limits) |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Runtime error |
65 ms |
131076 KB |
Execution killed with signal 9 (could be triggered by violating memory limits) |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Runtime error |
69 ms |
131072 KB |
Execution killed with signal 9 (could be triggered by violating memory limits) |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Runtime error |
67 ms |
131076 KB |
Execution killed with signal 9 (could be triggered by violating memory limits) |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Runtime error |
65 ms |
131076 KB |
Execution killed with signal 9 (could be triggered by violating memory limits) |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Runtime error |
74 ms |
131076 KB |
Execution killed with signal 9 (could be triggered by violating memory limits) |
2 |
Halted |
0 ms |
0 KB |
- |