답안 #846569

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
846569 2023-09-08T00:01:22 Z math_rabbit_1028 가장 긴 여행 (IOI23_longesttrip) C++17
5 / 100
1000 ms 1836 KB
#include <bits/stdc++.h>
#include "longesttrip.h"
using namespace std;

vector<int> longest_trip(int N, int D) {
    int n = N; 
    vector<int> adj[256];
    vector<int> res;
    res.clear();
    for (int i = 0; i < n; i++) adj[i].clear();
    for (int i = 0; i < n; i++) {
        for (int j = i + 1; j < n; j++) {
            if (are_connected({i}, {j})) {
                adj[i].push_back(j);
                adj[j].push_back(i);
            }
        }
    }

    int ch[256]; for (int i = 0; i < n; i++) ch[i] = 0;
    vector<int> path, clique;
    path.push_back(0);
    ch[0] = 1;
    while (true) {
        int bef = path.size();
        for (int i = 0; i < adj[path.back()].size(); i++) {
            int v = adj[path.back()][i];
            if (ch[v] == 0) {
                ch[v] = 1;
                path.push_back(v);
                break;
            }
        }
        if (bef == path.size()) break;
    }
    for (int i = 0; i < n; i++) if (ch[i] == 0) clique.push_back(i);

    bool connected = false;
    for (int i = 0; i < path.size(); i++) {
        for (int j = 0; j < adj[path[i]].size(); j++) {
            if (ch[adj[path[i]][j]] == 0) connected = true;
        }
    }
    if (connected) {
        for (int i = 0; i < adj[path[0]].size(); i++) {
            if (ch[adj[path[0]][i]] == 0) {
                int target = ch[adj[path[0]][i]];
                res = path;
                reverse(res.begin(), res.end());
                while (clique[0] != target) {
                    int front = clique[0];
                    clique.erase(clique.begin());
                    clique.push_back(front);
                }
                for (int j = 0; j < clique.size(); j++) res.push_back(clique[j]);
                return res;
            }
        }
        for (int i = 0; i < adj[path.back()].size(); i++) {
            if (ch[adj[path.back()][i]] == 0) {
                int target = ch[adj[path.back()][i]];
                res = path;
                while (clique[0] != target) {
                    int front = clique[0];
                    clique.erase(clique.begin());
                    clique.push_back(front);
                }
                for (int j = 0; j < clique.size(); j++) res.push_back(clique[j]);
                return res;
            }
        }
        for (int i = 0; i < clique.size(); i++) {
            for (int j = 0; j < adj[clique[i]].size(); j++) {
                if (ch[adj[clique[i]][j]] == 1) {
                    int target = adj[clique[i]][j];
                    swap(clique[i], clique.back());
                    res = clique;
                    while (path[0] != target) {
                        int front = path[0];
                        path.erase(path.begin());
                        path.push_back(front);
                    }
                    for (int k = 0; k < path.size(); k++) res.push_back(path[k]);
                    return res;
                }
            }
        }
    }
    else {
        if (path.size() > clique.size()) return path;
        return clique;
    }
    return {}; // never reached
}

Compilation message

longesttrip.cpp: In function 'std::vector<int> longest_trip(int, int)':
longesttrip.cpp:26:27: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   26 |         for (int i = 0; i < adj[path.back()].size(); i++) {
      |                         ~~^~~~~~~~~~~~~~~~~~~~~~~~~
longesttrip.cpp:34:17: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   34 |         if (bef == path.size()) break;
      |             ~~~~^~~~~~~~~~~~~~
longesttrip.cpp:39:23: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   39 |     for (int i = 0; i < path.size(); i++) {
      |                     ~~^~~~~~~~~~~~~
longesttrip.cpp:40:27: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   40 |         for (int j = 0; j < adj[path[i]].size(); j++) {
      |                         ~~^~~~~~~~~~~~~~~~~~~~~
longesttrip.cpp:45:27: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   45 |         for (int i = 0; i < adj[path[0]].size(); i++) {
      |                         ~~^~~~~~~~~~~~~~~~~~~~~
longesttrip.cpp:55:35: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   55 |                 for (int j = 0; j < clique.size(); j++) res.push_back(clique[j]);
      |                                 ~~^~~~~~~~~~~~~~~
longesttrip.cpp:59:27: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   59 |         for (int i = 0; i < adj[path.back()].size(); i++) {
      |                         ~~^~~~~~~~~~~~~~~~~~~~~~~~~
longesttrip.cpp:68:35: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   68 |                 for (int j = 0; j < clique.size(); j++) res.push_back(clique[j]);
      |                                 ~~^~~~~~~~~~~~~~~
longesttrip.cpp:72:27: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   72 |         for (int i = 0; i < clique.size(); i++) {
      |                         ~~^~~~~~~~~~~~~~~
longesttrip.cpp:73:31: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   73 |             for (int j = 0; j < adj[clique[i]].size(); j++) {
      |                             ~~^~~~~~~~~~~~~~~~~~~~~~~
longesttrip.cpp:83:39: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   83 |                     for (int k = 0; k < path.size(); k++) res.push_back(path[k]);
      |                                     ~~^~~~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 344 KB Output is correct
2 Correct 169 ms 1836 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 7 ms 344 KB Output is correct
2 Correct 27 ms 344 KB Output is correct
3 Correct 131 ms 868 KB Output is correct
4 Correct 372 ms 972 KB Output is correct
5 Correct 761 ms 1624 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 9 ms 344 KB Output is correct
2 Correct 26 ms 344 KB Output is correct
3 Correct 124 ms 952 KB Output is correct
4 Correct 368 ms 740 KB Output is correct
5 Correct 729 ms 1328 KB Output is correct
6 Execution timed out 3025 ms 344 KB Time limit exceeded
7 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 7 ms 344 KB Output is correct
2 Correct 25 ms 344 KB Output is correct
3 Correct 134 ms 704 KB Output is correct
4 Correct 385 ms 1232 KB Output is correct
5 Correct 770 ms 1776 KB Output is correct
6 Execution timed out 3040 ms 344 KB Time limit exceeded
7 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 7 ms 344 KB Output is correct
2 Correct 27 ms 344 KB Output is correct
3 Partially correct 120 ms 700 KB Output is partially correct
4 Partially correct 374 ms 1212 KB Output is partially correct
5 Partially correct 729 ms 1260 KB Output is partially correct
6 Execution timed out 3036 ms 352 KB Time limit exceeded
7 Halted 0 ms 0 KB -