답안 #1059903

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
1059903 2024-08-15T09:00:00 Z mychecksedad 가장 긴 여행 (IOI23_longesttrip) C++17
15 / 100
957 ms 1256 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];
  }
  int s = S.size();
  vector<vector<bool>> si(s, vector<bool>(s));
  for(int i = 0; i < s; ++i){
    for(int j = i + 1; j < s; ++j){
       bool ok = 0;
       for(int x: S[i]){
        for(int y: S[j]){
            if(is[x][y]) ok = 1;
        }
       }
       si[i][j] = si[j][i] = ok;
    }
  }
  deque<int> v;
    v.pb(0);
    int o = -1;
    for(int i = 1; i < s; ++i){
      bool x = si[0][i];
      if(x){
        o = i;
        v.pb(i);
        break;
      }
    }
    if(o == -1){
      assert(false);
    }
    for(int i = 1; i < s; ++i){
      if(o == i) continue;
      bool x = si[v[0]][i];
      if(x){
        v.push_front(i);
      }else{
        assert(si[v.back()][i]);
        v.pb(i);
      }
    }
  
  vector<vector<int>> SS(s);
  for(int i = 0; i < s; ++i) SS[i] = S[v[i]];
  SS.swap(S);

  vector<bool> nextgoer(n);
  vector<int> nxt(s);
  for(int i = 0; i + 1 < s; ++i){
    for(int x: S[i]){
      for(int y: S[i + 1]){
        if(is[x][y]) nextgoer[x] = 1;
      }
      if(nextgoer[x]) ++nxt[i];
    }
  }

  vi res;
  for(int i = 0; i < S.size(); ++i){
    if(i + 1 == S.size()){
      for(int x: S[i]) res.pb(x);
    }else{
      bool ok = 0;
      for(int j = (int(S[i].size() == 1 ? 0 : 1)); j < S[i].size(); ++j){
        for(int l = 0; l < S[i + 1].size(); ++l){
          if(i + 2 < S.size() && nxt[i + 1] == 1 && nextgoer[S[i + 1][l]]) continue;
          if(is[S[i][j]][S[i + 1][l]]){
            swap(S[i][j], S[i].back());
            swap(S[i + 1][l], S[i + 1][0]);
            ok = 1;
            break;
          }
        }
        if(ok) break;
      }
      if(!ok) assert(false);
      for(int x: S[i]) res.pb(x);
    }
  }
  return res;
}

Compilation message

longesttrip.cpp: In function 'std::vector<int> longest_trip(int, int)':
longesttrip.cpp:156:20: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::vector<int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  156 |   for(int i = 0; i < S.size(); ++i){
      |                  ~~^~~~~~~~~~
longesttrip.cpp:157:14: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::vector<int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  157 |     if(i + 1 == S.size()){
      |        ~~~~~~^~~~~~~~~~~
longesttrip.cpp:161:54: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  161 |       for(int j = (int(S[i].size() == 1 ? 0 : 1)); j < S[i].size(); ++j){
      |                                                    ~~^~~~~~~~~~~~~
longesttrip.cpp:162:26: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  162 |         for(int l = 0; l < S[i + 1].size(); ++l){
      |                        ~~^~~~~~~~~~~~~~~~~
longesttrip.cpp:163:20: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::vector<int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  163 |           if(i + 2 < S.size() && nxt[i + 1] == 1 && nextgoer[S[i + 1][l]]) continue;
      |              ~~~~~~^~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 344 KB Output is correct
2 Correct 202 ms 1044 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 344 KB Output is correct
2 Correct 1 ms 344 KB Output is correct
3 Correct 0 ms 344 KB Output is correct
4 Correct 0 ms 344 KB Output is correct
5 Correct 0 ms 344 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 5 ms 344 KB Output is correct
2 Correct 6 ms 344 KB Output is correct
3 Correct 5 ms 344 KB Output is correct
4 Correct 5 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 5 ms 344 KB Output is correct
8 Correct 5 ms 416 KB Output is correct
9 Correct 5 ms 344 KB Output is correct
10 Correct 5 ms 344 KB Output is correct
11 Correct 5 ms 344 KB Output is correct
12 Correct 5 ms 344 KB Output is correct
13 Correct 5 ms 344 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 7 ms 344 KB Output is correct
2 Correct 26 ms 344 KB Output is correct
3 Correct 152 ms 600 KB Output is correct
4 Correct 410 ms 500 KB Output is correct
5 Correct 849 ms 1112 KB Output is correct
6 Correct 7 ms 344 KB Output is correct
7 Correct 21 ms 344 KB Output is correct
8 Correct 141 ms 440 KB Output is correct
9 Correct 310 ms 600 KB Output is correct
10 Correct 648 ms 828 KB Output is correct
11 Correct 726 ms 736 KB Output is correct
12 Correct 873 ms 940 KB Output is correct
13 Correct 820 ms 1152 KB Output is correct
14 Correct 6 ms 596 KB Output is correct
15 Correct 13 ms 344 KB Output is correct
16 Runtime error 34 ms 432 KB Execution killed with signal 6
17 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 7 ms 344 KB Output is correct
2 Correct 30 ms 344 KB Output is correct
3 Partially correct 142 ms 448 KB Output is partially correct
4 Partially correct 421 ms 600 KB Output is partially correct
5 Partially correct 881 ms 956 KB Output is partially correct
6 Correct 6 ms 344 KB Output is correct
7 Correct 33 ms 344 KB Output is correct
8 Partially correct 158 ms 600 KB Output is partially correct
9 Partially correct 297 ms 744 KB Output is partially correct
10 Partially correct 957 ms 1152 KB Output is partially correct
11 Partially correct 773 ms 1072 KB Output is partially correct
12 Partially correct 842 ms 1256 KB Output is partially correct
13 Partially correct 880 ms 1068 KB Output is partially correct
14 Correct 8 ms 344 KB Output is correct
15 Correct 14 ms 344 KB Output is correct
16 Runtime error 13 ms 592 KB Execution killed with signal 6
17 Halted 0 ms 0 KB -