답안 #1038169

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
1038169 2024-07-29T13:52:12 Z aymanrs 가장 긴 여행 (IOI23_longesttrip) C++17
5 / 100
846 ms 492 KB
#include "longesttrip.h"
#include <bits/stdc++.h>
using namespace std;
bool g[300][300] = {{false}};
bool ej[256] = {false};
pair<vector<int>,vector<int>> fl(const vector<int>& V){
    memset(ej, 0, sizeof(ej));
    queue<int> q;q.push(V[0]);
    ej[V[0]]=true;
    vector<int> a;a.push_back(V[0]);
    while(!q.empty()){
        int t = q.front();
        q.pop();
        for(int j : V){
            if(g[t][j] && !ej[j]){
                ej[j]=true;
                q.push(j);
                a.push_back(j);
            }
        }
    }
    vector<int> b, c;
    if(a.size()<V.size()){
        for(int i : V) if(!ej[i]) b.push_back(i);
        return {a,b};
    }
    int i, j;
    for(i = 0;i < V.size();i++){
        for(j = i+1;j < V.size();j++){
            if(!g[V[i]][V[j]]) break;
        }
    }   
    if(i >= V.size() || j >= V.size()) return {a, {}};
    a.clear();
    i=V[i];j=V[j];
    for(int k : V){
        if(k==i||k==j) continue;
        if(!g[k][j]) a.push_back(k);
        else if(!g[k][i]) c.push_back(k);
        else {
            b.push_back(k);
        }
    }
    if(b.empty()){
        a.push_back(i);
        for(int& k : a){
            for(int& l : c){
                if(g[k][l]){
                    swap(k, a.back());
                    swap(l, c[0]);
                    c.push_back(j);
                    for(int x : c) a.push_back(x);
                    return {a, {}};
                }
            }
        }
    }
    auto L = fl(b);
    if(L.second.empty()){
        a.push_back(i);
        for(int k : L.first) a.push_back(k);
        a.push_back(j);
        for(int k : c) a.push_back(k);
        return {a, {}};
    } else {
        if(a.empty()){
            L.first.push_back(i);
            for(int k : L.second) L.first.push_back(k);
            L.first.push_back(j);
            for(int k : c) L.first.push_back(k);
            return {L.first, {}};
        }
        if(g[a[0]][L.first[0]]){
            a.push_back(i);
            swap(a.back(), a[0]);
            for(int k : L.first) a.push_back(k);
            for(int k : a) L.second.push_back(k);
            L.second.push_back(j);
            for(int k : c) L.second.push_back(k);
            return {L.second, {}};
        } else {
            a.push_back(i);
            swap(a.back(), a[0]);
            for(int k : L.second) a.push_back(k);
            for(int k : a) L.first.push_back(k);
            L.first.push_back(j);
            for(int k : c) L.first.push_back(k);
            return {L.first, {}};
        }
    }

}
std::vector<int> longest_trip(int N, int D)
{
    vector<int> V;
    for(int i = 0;i < N;i++) for(int j = 0;j < N;j++) g[i][j] = false;
    for(int i = 0;i < N;i++){
        V.push_back(i);
        for(int j = i+1;j < N;j++){
            if(are_connected({i}, {j})){
                g[i][j]=g[j][i]=true;
            }
        }
    }
    auto L = fl(V);
   if(L.first.size()>=L.second.size()) return L.first;
   return L.second;
}

Compilation message

longesttrip.cpp: In function 'std::pair<std::vector<int>, std::vector<int> > fl(const std::vector<int>&)':
longesttrip.cpp:28:17: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   28 |     for(i = 0;i < V.size();i++){
      |               ~~^~~~~~~~~~
longesttrip.cpp:29:23: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   29 |         for(j = i+1;j < V.size();j++){
      |                     ~~^~~~~~~~~~
longesttrip.cpp:33:10: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   33 |     if(i >= V.size() || j >= V.size()) return {a, {}};
      |        ~~^~~~~~~~~~~
longesttrip.cpp:33:27: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   33 |     if(i >= V.size() || j >= V.size()) return {a, {}};
      |                         ~~^~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 344 KB Output is correct
2 Incorrect 213 ms 488 KB Incorrect
3 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 6 ms 340 KB Output is correct
2 Correct 18 ms 344 KB Output is correct
3 Correct 133 ms 432 KB Output is correct
4 Correct 433 ms 452 KB Output is correct
5 Correct 815 ms 492 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 7 ms 344 KB Output is correct
2 Correct 22 ms 344 KB Output is correct
3 Correct 129 ms 432 KB Output is correct
4 Correct 377 ms 468 KB Output is correct
5 Correct 808 ms 492 KB Output is correct
6 Incorrect 0 ms 344 KB Incorrect
7 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 6 ms 344 KB Output is correct
2 Correct 20 ms 344 KB Output is correct
3 Correct 149 ms 432 KB Output is correct
4 Correct 383 ms 448 KB Output is correct
5 Correct 800 ms 492 KB Output is correct
6 Incorrect 0 ms 344 KB Incorrect
7 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 6 ms 344 KB Output is correct
2 Correct 31 ms 344 KB Output is correct
3 Partially correct 125 ms 436 KB Output is partially correct
4 Partially correct 397 ms 452 KB Output is partially correct
5 Partially correct 846 ms 492 KB Output is partially correct
6 Incorrect 0 ms 344 KB Incorrect
7 Halted 0 ms 0 KB -