Submission #1060371

# Submission time Handle Problem Language Result Execution time Memory
1060371 2024-08-15T13:27:28 Z mychecksedad Longest Trip (IOI23_longesttrip) C++17
60 / 100
884 ms 1736 KB
#include "longesttrip.h"
#include <bits/stdc++.h>
using namespace std;
#define pb push_back
#define all(x) x.begin(),x.end()
#define ll long long
#define ff first
#define ss second
#define vi vector<int>
const int N = 200005;
 
std::vector<int> longest_trip(int n, int D)
{
  if(D == 3){
    vector<int> v;
    for(int i = 0; i < n; ++i) v.pb(i);
    return v;
  }
  if(D == 2){
    deque<int> v;
    v.pb(0);
    int o = -1;
    for(int i = 1; i < n; ++i){
      bool x = are_connected(vi{0}, vi{i});
      if(x){
        o = i;
        v.pb(i);
        break;
      }
    }
    if(o == -1){
      vi res;
      for(int i = 1; i < n; ++i){
        res.pb(i);
      }
      return res;
    }
    for(int i = 1; i < n; ++i){
      if(o == i) continue;
      bool x = are_connected(vi{v[0]}, vi{i});
      if(x){
        v.push_front(i);
      }else v.pb(i);
    }
    vector<int> res(all(v));
    return res;
  }
  vector<vector<int>> g(n);
  vector<vector<bool>> is(n, vector<bool>(n));
  for(int i = 0; i < n; ++i){
    for(int j = i + 1; j < n; ++j){
      bool ok = are_connected(vi{i}, vi{j});
      if(ok){
        is[i][j] = is[j][i] = 1;
        g[i].pb(j);
        g[j].pb(i);
      }
    }
  }
  vector<vector<int>> S;
  for(int i = 0; i < n; ++i){
    if(g[i].empty()){
      vi res;
      for(int j = 0; j < n; ++j){
        if(j != i) res.pb(j);
      }
      return res;
    }
    bool okk = 0;
    for(auto &s: S){
      bool ok = 1;
      for(int x: s){
        if(is[x][i] == 0) ok = 0;
      }
      if(ok == 1){
        s.pb(i);
        okk = 1;
        break;
      }
    }
    if(okk == 0){
      S.pb(vi{i});
    }
  }
  if(S.size() == 1) return S[0];
  if(S.size() == 2){
    for(int &x: S[0]){
      for(int &y: S[1]){
        if(is[x][y]){
          swap(x, S[0].back());
          swap(y, S[1][0]);
          vi res;
          for(int f: S[0]) res.pb(f);
          for(int f: S[1]) res.pb(f);
          return res;
        }   
      }
    }
    if(S[0].size() > S[1].size()) return S[0];
    return S[1];
  }
  
  deque<int> a, b;
  a.pb(0);
  for(int i = 1; i < n; ++i){
    if(is[a[0]][i]){
      a.push_front(i);
    }else if(is[a.back()][i]){
      a.pb(i);
    }else{
      if(b.empty()) b.pb(i);
      else{
        if(is[b[0]][i]){
          b.push_front(i);
        }else if(is[b.back()][i]){
          b.pb(i);
        }else{
          if(is[a.back()][b[0]]){
            for(int x: b) a.pb(x);
            b.clear();
            b.pb(i);
          }else{
            assert(false);
          }
        }
      }
    }
  }
  if(b.size()>a.size())swap(a,b);

  int u = -1, v;

  while(a.size() < n){
    if(is[a[0]][a.back()] == 0){
      if(is[a[0]][b.back()] == 1){
        a.push_front(b.back());
        b.pop_back();
      }else{
        a.pb(b.back());
        b.pop_back();
      }
    }else{
      if(u == -1){
        for(int i = 0; i < a.size(); ++i){
          for(int j = i + 1; j < a.size(); ++j){
            if(is[a[i]][a[j]] == 0){
              u = a[i], v = a[j];
              break;
            }
          }
          if(u != -1) break;
        }
      }
      assert(u != -1);
      assert(b.size());
      if(is[b.back()][u] == 1){
        int cc = 0;
        while(a.front() != u){
          int x = a.front();
          a.pop_front();
          a.pb(x);
          if(cc>n) assert(false);
          ++cc;
        }
        a.push_front(b.back());
        b.pop_back();
      }else{
        int cc = 0;
        while(a.front() != v){
          int x = a.front();
          a.pop_front();
          a.pb(x);
          ++cc;
          if(cc>n) assert(false);
        }
        a.push_front(b.back());
        b.pop_back();
      }
    }
  }
  vi res(all(a));
  return res;
}

Compilation message

longesttrip.cpp: In function 'std::vector<int> longest_trip(int, int)':
longesttrip.cpp:133:18: warning: comparison of integer expressions of different signedness: 'std::deque<int>::size_type' {aka 'long unsigned int'} and 'int' [-Wsign-compare]
  133 |   while(a.size() < n){
      |         ~~~~~~~~~^~~
longesttrip.cpp:144:26: warning: comparison of integer expressions of different signedness: 'int' and 'std::deque<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  144 |         for(int i = 0; i < a.size(); ++i){
      |                        ~~^~~~~~~~~~
longesttrip.cpp:145:32: warning: comparison of integer expressions of different signedness: 'int' and 'std::deque<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  145 |           for(int j = i + 1; j < a.size(); ++j){
      |                              ~~^~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 1 ms 344 KB Output is correct
2 Correct 209 ms 1236 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 340 KB Output is correct
2 Correct 1 ms 344 KB Output is correct
3 Correct 0 ms 344 KB Output is correct
4 Correct 1 ms 344 KB Output is correct
5 Correct 1 ms 340 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 5 ms 344 KB Output is correct
2 Correct 10 ms 344 KB Output is correct
3 Correct 5 ms 344 KB Output is correct
4 Correct 3 ms 344 KB Output is correct
5 Correct 5 ms 344 KB Output is correct
6 Correct 6 ms 344 KB Output is correct
7 Correct 4 ms 344 KB Output is correct
8 Correct 7 ms 344 KB Output is correct
9 Correct 5 ms 344 KB Output is correct
10 Correct 4 ms 344 KB Output is correct
11 Correct 6 ms 344 KB Output is correct
12 Correct 5 ms 344 KB Output is correct
13 Correct 5 ms 344 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 5 ms 344 KB Output is correct
2 Correct 22 ms 344 KB Output is correct
3 Correct 143 ms 344 KB Output is correct
4 Correct 393 ms 504 KB Output is correct
5 Correct 781 ms 1184 KB Output is correct
6 Correct 5 ms 344 KB Output is correct
7 Correct 28 ms 344 KB Output is correct
8 Correct 131 ms 436 KB Output is correct
9 Correct 328 ms 600 KB Output is correct
10 Correct 782 ms 1016 KB Output is correct
11 Correct 841 ms 908 KB Output is correct
12 Correct 839 ms 980 KB Output is correct
13 Correct 839 ms 924 KB Output is correct
14 Correct 6 ms 344 KB Output is correct
15 Correct 11 ms 344 KB Output is correct
16 Correct 28 ms 344 KB Output is correct
17 Correct 100 ms 344 KB Output is correct
18 Correct 142 ms 344 KB Output is correct
19 Correct 304 ms 756 KB Output is correct
20 Correct 326 ms 856 KB Output is correct
21 Correct 800 ms 1576 KB Output is correct
22 Correct 840 ms 1248 KB Output is correct
23 Correct 801 ms 756 KB Output is correct
24 Correct 865 ms 1040 KB Output is correct
25 Correct 9 ms 344 KB Output is correct
26 Correct 12 ms 344 KB Output is correct
27 Correct 22 ms 412 KB Output is correct
28 Correct 22 ms 344 KB Output is correct
29 Correct 29 ms 344 KB Output is correct
30 Correct 191 ms 600 KB Output is correct
31 Correct 181 ms 440 KB Output is correct
32 Correct 183 ms 436 KB Output is correct
33 Correct 286 ms 496 KB Output is correct
34 Correct 301 ms 600 KB Output is correct
35 Correct 305 ms 720 KB Output is correct
36 Correct 848 ms 856 KB Output is correct
37 Correct 698 ms 1232 KB Output is correct
38 Correct 762 ms 684 KB Output is correct
39 Correct 788 ms 1096 KB Output is correct
40 Correct 841 ms 1000 KB Output is correct
41 Correct 860 ms 780 KB Output is correct
42 Correct 853 ms 708 KB Output is correct
43 Correct 844 ms 800 KB Output is correct
44 Correct 827 ms 1004 KB Output is correct
45 Correct 10 ms 344 KB Output is correct
46 Correct 9 ms 344 KB Output is correct
47 Correct 17 ms 344 KB Output is correct
48 Correct 30 ms 344 KB Output is correct
49 Correct 20 ms 344 KB Output is correct
50 Correct 194 ms 608 KB Output is correct
51 Correct 220 ms 344 KB Output is correct
52 Correct 190 ms 344 KB Output is correct
53 Correct 307 ms 940 KB Output is correct
54 Correct 297 ms 736 KB Output is correct
55 Correct 298 ms 736 KB Output is correct
56 Correct 836 ms 720 KB Output is correct
57 Correct 825 ms 916 KB Output is correct
58 Correct 808 ms 776 KB Output is correct
59 Correct 840 ms 1212 KB Output is correct
60 Correct 829 ms 1140 KB Output is correct
61 Correct 828 ms 800 KB Output is correct
62 Correct 850 ms 816 KB Output is correct
63 Correct 727 ms 1080 KB Output is correct
64 Correct 820 ms 1360 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 8 ms 344 KB Output is correct
2 Correct 16 ms 344 KB Output is correct
3 Partially correct 140 ms 436 KB Output is partially correct
4 Partially correct 405 ms 848 KB Output is partially correct
5 Partially correct 796 ms 764 KB Output is partially correct
6 Correct 5 ms 344 KB Output is correct
7 Correct 23 ms 344 KB Output is correct
8 Partially correct 147 ms 600 KB Output is partially correct
9 Partially correct 315 ms 600 KB Output is partially correct
10 Partially correct 874 ms 960 KB Output is partially correct
11 Partially correct 825 ms 1368 KB Output is partially correct
12 Partially correct 807 ms 1136 KB Output is partially correct
13 Partially correct 847 ms 900 KB Output is partially correct
14 Correct 6 ms 344 KB Output is correct
15 Correct 9 ms 344 KB Output is correct
16 Correct 30 ms 344 KB Output is correct
17 Partially correct 83 ms 600 KB Output is partially correct
18 Partially correct 165 ms 452 KB Output is partially correct
19 Partially correct 274 ms 952 KB Output is partially correct
20 Partially correct 297 ms 664 KB Output is partially correct
21 Correct 9 ms 344 KB Output is correct
22 Correct 11 ms 344 KB Output is correct
23 Correct 22 ms 424 KB Output is correct
24 Correct 31 ms 592 KB Output is correct
25 Correct 21 ms 416 KB Output is correct
26 Partially correct 198 ms 600 KB Output is partially correct
27 Partially correct 190 ms 600 KB Output is partially correct
28 Partially correct 185 ms 600 KB Output is partially correct
29 Partially correct 258 ms 728 KB Output is partially correct
30 Partially correct 282 ms 476 KB Output is partially correct
31 Partially correct 252 ms 708 KB Output is partially correct
32 Correct 10 ms 344 KB Output is correct
33 Correct 10 ms 344 KB Output is correct
34 Correct 26 ms 344 KB Output is correct
35 Correct 23 ms 436 KB Output is correct
36 Correct 23 ms 344 KB Output is correct
37 Partially correct 202 ms 596 KB Output is partially correct
38 Partially correct 142 ms 448 KB Output is partially correct
39 Partially correct 198 ms 344 KB Output is partially correct
40 Partially correct 327 ms 592 KB Output is partially correct
41 Partially correct 316 ms 496 KB Output is partially correct
42 Partially correct 307 ms 1216 KB Output is partially correct
43 Partially correct 765 ms 1736 KB Output is partially correct
44 Partially correct 884 ms 1000 KB Output is partially correct
45 Partially correct 789 ms 900 KB Output is partially correct
46 Partially correct 795 ms 984 KB Output is partially correct
47 Partially correct 808 ms 1108 KB Output is partially correct
48 Partially correct 813 ms 712 KB Output is partially correct
49 Partially correct 820 ms 872 KB Output is partially correct
50 Partially correct 833 ms 856 KB Output is partially correct
51 Partially correct 795 ms 680 KB Output is partially correct
52 Partially correct 826 ms 732 KB Output is partially correct
53 Partially correct 794 ms 592 KB Output is partially correct
54 Partially correct 834 ms 1272 KB Output is partially correct
55 Partially correct 848 ms 1332 KB Output is partially correct
56 Partially correct 820 ms 976 KB Output is partially correct
57 Partially correct 802 ms 1052 KB Output is partially correct
58 Partially correct 806 ms 908 KB Output is partially correct
59 Partially correct 856 ms 932 KB Output is partially correct
60 Partially correct 838 ms 856 KB Output is partially correct
61 Partially correct 873 ms 736 KB Output is partially correct
62 Partially correct 839 ms 1160 KB Output is partially correct
63 Partially correct 870 ms 948 KB Output is partially correct
64 Partially correct 819 ms 880 KB Output is partially correct
65 Partially correct 805 ms 1132 KB Output is partially correct
66 Partially correct 751 ms 780 KB Output is partially correct
67 Partially correct 747 ms 564 KB Output is partially correct
68 Partially correct 837 ms 812 KB Output is partially correct
69 Partially correct 818 ms 1108 KB Output is partially correct
70 Partially correct 838 ms 888 KB Output is partially correct
71 Partially correct 808 ms 876 KB Output is partially correct
72 Partially correct 809 ms 868 KB Output is partially correct
73 Partially correct 744 ms 1120 KB Output is partially correct
74 Partially correct 759 ms 836 KB Output is partially correct
75 Partially correct 792 ms 812 KB Output is partially correct
76 Partially correct 767 ms 600 KB Output is partially correct
77 Partially correct 799 ms 1004 KB Output is partially correct
78 Partially correct 682 ms 876 KB Output is partially correct
79 Partially correct 762 ms 928 KB Output is partially correct
80 Partially correct 840 ms 1044 KB Output is partially correct
81 Partially correct 774 ms 1048 KB Output is partially correct
82 Partially correct 828 ms 1016 KB Output is partially correct