//
// IOI2008Islands.cpp
//
//
// Created by Matjaz Leonardis on 11/11/2023.
//
#include <iostream>
#include <vector>
#include <queue>
#include <algorithm>
using namespace std;
vector<vector<int> > s;
vector<vector<int> > l;
int N;
int u,v,idx;
vector<bool> seen;
pair<int,long long> find_furthest(int x, int p){
//cout << x << " " << p << " " << seen.size() << endl;
if (seen[x]) return make_pair(-1, -1000000000);
seen[x] = true;
//cout << x << " " << p << endl;
pair<int,long long> res = make_pair(x, 0);
for (int i=0;i<s[x].size();i++){
int w = s[x][i];
if (w == p) continue;
if (u == x && v == w && idx == i) continue;
pair<int,long long> best = find_furthest(w, x);
//if (best.first == -1) return make_pair(-1, -1000000000);
int L = l[x][i];
if (best.second + L > res.second){
res.first = best.first;
res.second = L + best.second;
}
}
return res;
}
long long longest_path(vector<int> c){
long long best = -1;
for (int i=0;i<c.size();i++){
for (int j=0;j<s[c[i]].size();j++){
u = c[i];
v = s[c[i]][j];
idx = j;
seen.assign(N, false);
int d = find_furthest(u, -1 ).first;
if (d == -1) continue;
seen.assign(N, false);
long long longest = find_furthest(d , -1).second;
best = max(longest, best);
//cout << longest << endl;
}
}
return best;
}
int main(){
cin >> N;
s.assign(N, vector<int> ());
l.assign(N, vector<int> ());
for (int i=0;i<N;i++){
int U,L;
cin >> U >> L;
U--;
s[i].push_back(U);
l[i].push_back(L);
s[U].push_back(i);
l[U].push_back(L);
}
long long total = 0;
vector<bool> discovered(N, false);
for (int i=0;i<N;i++){
if (!discovered[i]){
vector<int> component(1, i);
queue<int> Q;
Q.push(i);
discovered[i] = true;
while (!Q.empty()){
int x = Q.front(); Q.pop();
for (int j=0;j<s[x].size();j++){
if (discovered[s[x][j]]) continue;
discovered[s[x][j]] = true;
component.push_back(s[x][j]);
Q.push(s[x][j]);
}
}
total += longest_path(component);
}
}
cout << total << endl;
return 0;
}
Compilation message
islands.cpp: In function 'std::pair<int, long long int> find_furthest(int, int)':
islands.cpp:30:19: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
30 | for (int i=0;i<s[x].size();i++){
| ~^~~~~~~~~~~~
islands.cpp: In function 'long long int longest_path(std::vector<int>)':
islands.cpp:52:19: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
52 | for (int i=0;i<c.size();i++){
| ~^~~~~~~~~
islands.cpp:53:23: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
53 | for (int j=0;j<s[c[i]].size();j++){
| ~^~~~~~~~~~~~~~~
islands.cpp: In function 'int main()':
islands.cpp:100:31: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
100 | for (int j=0;j<s[x].size();j++){
| ~^~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
1 ms |
348 KB |
Output is correct |
3 |
Correct |
8 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
344 KB |
Output is correct |
5 |
Incorrect |
0 ms |
348 KB |
Output isn't correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Incorrect |
0 ms |
348 KB |
Output isn't correct |
8 |
Incorrect |
1 ms |
344 KB |
Output isn't correct |
9 |
Incorrect |
0 ms |
348 KB |
Output isn't correct |
10 |
Correct |
1 ms |
348 KB |
Output is correct |
11 |
Incorrect |
1 ms |
348 KB |
Output isn't correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
34 ms |
348 KB |
Output is correct |
2 |
Incorrect |
1 ms |
348 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
3 ms |
604 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Execution timed out |
2096 ms |
2144 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Execution timed out |
2017 ms |
7892 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Execution timed out |
2053 ms |
25976 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Execution timed out |
2021 ms |
45864 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Execution timed out |
2062 ms |
131072 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Runtime error |
721 ms |
131072 KB |
Execution killed with signal 9 |
2 |
Halted |
0 ms |
0 KB |
- |