Submission #871913

# Submission time Handle Problem Language Result Execution time Memory
871913 2023-11-11T20:54:08 Z Matjaz Islands (IOI08_islands) C++14
40 / 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;

struct edge{
    int u,v,l;
    int other(int x){
        if (u == x) return v; else return u;
    }
};

vector<vector<int> > s;
vector<vector<int> > l;
vector<edge> E;
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];
        int L = E[w].l;
        if (w == idx) continue;
        w = E[w].other(x);
        if (w == p) continue;
        
        pair<int,long long> best = find_furthest(w, x);
        //if (best.first == -1) return make_pair(-1, -1000000000);
        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++){
            idx = s[c[i]][j];
            seen.assign(N, false);
            int d = find_furthest(c[0], -1 ).first;
            if (d == -1) continue;
            seen.assign(N, false);
            long long longest = find_furthest(d , -1).second;
            best = max(longest, best);
        }
    }
    
    return best;
    
}


int main(){
    cin >> N;
    s.assign(N, vector<int> ());
    
    for (int i=0;i<N;i++){
        int U,L;
        cin >> U >> L;
        U--;
        edge e;
        e.u = i;
        e.v = U;
        e.l = L;
        E.push_back(e);
        s[i].push_back(i);
        s[U].push_back(i);
    }
    
    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++){
                    int u = E[s[x][j]].other(x);
                    if (discovered[u]) continue;
                    discovered[u] = true;
                    component.push_back(u);
                    Q.push(u);
                }
            }
            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:38:19: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   38 |     for (int i=0;i<s[x].size();i++){
      |                  ~^~~~~~~~~~~~
islands.cpp: In function 'long long int longest_path(std::vector<int>)':
islands.cpp:61:19: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   61 |     for (int i=0;i<c.size();i++){
      |                  ~^~~~~~~~~
islands.cpp:62:23: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   62 |         for (int j=0;j<s[c[i]].size();j++){
      |                      ~^~~~~~~~~~~~~~~
islands.cpp: In function 'int main()':
islands.cpp:108:31: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  108 |                 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 2 ms 348 KB Output is correct
3 Correct 9 ms 348 KB Output is correct
4 Correct 0 ms 344 KB Output is correct
5 Correct 0 ms 348 KB Output is correct
6 Correct 0 ms 344 KB Output is correct
7 Correct 1 ms 348 KB Output is 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 Correct 56 ms 348 KB Output is correct
2 Correct 1 ms 344 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 4 ms 600 KB Output is correct
2 Correct 1473 ms 1108 KB Output is correct
# Verdict Execution time Memory Grader output
1 Execution timed out 2066 ms 1492 KB Time limit exceeded
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Execution timed out 2051 ms 5644 KB Time limit exceeded
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Execution timed out 2055 ms 17156 KB Time limit exceeded
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Execution timed out 2064 ms 28720 KB Time limit exceeded
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Execution timed out 2043 ms 93472 KB Time limit exceeded
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Runtime error 684 ms 131072 KB Execution killed with signal 9
2 Halted 0 ms 0 KB -