Submission #871904

# Submission time Handle Problem Language Result Execution time Memory
871904 2023-11-11T20:24:00 Z Matjaz Islands (IOI08_islands) C++14
24 / 100
2000 ms 131072 KB
//
//  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,int> 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);
        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(c[0], -1 ).first;
            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:51:19: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   51 |     for (int i=0;i<c.size();i++){
      |                  ~^~~~~~~~~
islands.cpp:52:23: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   52 |         for (int j=0;j<s[c[i]].size();j++){
      |                      ~^~~~~~~~~~~~~~~
islands.cpp: In function 'int main()':
islands.cpp:98:31: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   98 |                 for (int j=0;j<s[x].size();j++){
      |                              ~^~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 0 ms 344 KB Output is correct
2 Correct 1 ms 348 KB Output is correct
3 Incorrect 10 ms 348 KB Output isn't correct
4 Correct 0 ms 348 KB Output is correct
5 Incorrect 0 ms 348 KB Output isn't correct
6 Correct 0 ms 344 KB Output is correct
7 Incorrect 0 ms 344 KB Output isn't correct
8 Correct 0 ms 348 KB Output is correct
9 Correct 0 ms 348 KB Output is correct
10 Correct 1 ms 348 KB Output is correct
11 Correct 0 ms 348 KB Output is correct
# Verdict Execution time Memory Grader output
1 Incorrect 42 ms 568 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 3 ms 604 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Execution timed out 2032 ms 2136 KB Time limit exceeded
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Execution timed out 2044 ms 8152 KB Time limit exceeded
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Execution timed out 2041 ms 25736 KB Time limit exceeded
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Execution timed out 2066 ms 45764 KB Time limit exceeded
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Execution timed out 2017 ms 131072 KB Time limit exceeded
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Runtime error 753 ms 131072 KB Execution killed with signal 9
2 Halted 0 ms 0 KB -