Submission #871906

#TimeUsernameProblemLanguageResultExecution timeMemory
871906MatjazIslands (IOI08_islands)C++14
30 / 100
2075 ms131072 KiB
// // 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(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 (stderr)

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:99:31: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   99 |                 for (int j=0;j<s[x].size();j++){
      |                              ~^~~~~~~~~~~~
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...