Submission #871913

#TimeUsernameProblemLanguageResultExecution timeMemory
871913MatjazIslands (IOI08_islands)C++14
40 / 100
2066 ms131072 KiB
// // 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 (stderr)

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 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...