답안 #1038221

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
1038221 2024-07-29T14:30:28 Z aymanrs 가장 긴 여행 (IOI23_longesttrip) C++17
5 / 100
10 ms 592 KB
#include "longesttrip.h"
#include <bits/stdc++.h>
#pragma GCC optimize("O3,unroll-loops")
#pragma GCC target("avx2,bmi,bmi2,lzcnt,popcnt")
using namespace std;
char g[256][256] = {{0}};
bool isg(int a, int b){
    if(g[a][b]) {
        return g[a][b] == 1;
    }
    if(are_connected({a},{b})){
        g[a][b]=g[b][a]=1;
        return true;
    }
    g[a][b]=g[b][a]=-1;
    return false;
}
bool arc(const vector<int>& a, const vector<int>& b){
    if(a.empty()||b.empty()) return false;
    bool at0=false;
    for(int i : a) for(int j : b) {
        if(g[i][j]==1) return true;
        else if(!g[i][j]) at0=true;
    }
    if(!at0) return false;
    return are_connected(a, b);
}
pair<vector<int>,vector<int>> fl(const vector<int>& V){
    if(V.size()<=1) return {V, {}};
    int i, j;
    vector<int> a;a.push_back(V[0]);
    for(i = 1;i < V.size();i++){
        if(!g[a.back()][V[i]]){
            if(isg(a.back(), V[i])){
                a.push_back(V[i]);
            } else {
                i = V[i];
                j = a.back();
                break;
            }
        }
    }
    if(a.size() == V.size()) return {a, {}};
    vector<int> b, c;
    a.clear();
    for(int k : V){
        if(k==i||k==j) continue;
        if(!isg(k,j)) a.push_back(k);
        else if(!isg(k,i)) c.push_back(k);
        else {
            b.push_back(k);
        }
    }
    if(b.empty()){
        if(!arc(a,c)) {
            a.push_back(i);
            c.push_back(j);
            return {a,c};
        }
        int l=0,r=c.size()-1, ll=0,rr=a.size()-1;
        while(l<r&&ll<rr){
            if(r-l > rr-ll){
                int m = r+l>>1;
                if(arc(vector<int>{c.begin()+l,c.begin()+m+1}, vector<int>{a.begin()+ll,a.begin()+rr+1})){
                    r=m;
                } else l = m+1;
            } else {
                int m = ll+rr>>1;
                if(arc(vector<int>{c.begin()+l,c.begin()+r+1}, vector<int>{a.begin()+ll,a.begin()+m+1})){
                    rr=m;
                } else ll = m+1;
            }
        }
        a.push_back(i);swap(a[ll], a.back());
        c.push_back(j);swap(c[l], c[0]);
        for(int k : c) a.push_back(k);
    }
    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(isg(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++) {
        V.push_back(i);
        for(int j = 0;j < N;j++) g[i][j] = 0;
    }
    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:32:17: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   32 |     for(i = 1;i < V.size();i++){
      |               ~~^~~~~~~~~~
longesttrip.cpp:63:26: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
   63 |                 int m = r+l>>1;
      |                         ~^~
longesttrip.cpp:68:27: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
   68 |                 int m = ll+rr>>1;
      |                         ~~^~~
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 344 KB Output is correct
2 Incorrect 10 ms 592 KB invalid array
3 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 4 ms 344 KB Output is correct
2 Correct 5 ms 344 KB Output is correct
3 Correct 7 ms 344 KB Output is correct
4 Correct 5 ms 344 KB Output is correct
5 Correct 5 ms 344 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 5 ms 344 KB Output is correct
2 Correct 5 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 Incorrect 1 ms 344 KB Incorrect
8 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 7 ms 344 KB Output is correct
2 Correct 5 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 7 ms 344 KB Output is correct
7 Incorrect 1 ms 344 KB Incorrect
8 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 5 ms 344 KB Output is correct
2 Correct 5 ms 344 KB Output is correct
3 Correct 5 ms 344 KB Output is correct
4 Correct 6 ms 472 KB Output is correct
5 Correct 4 ms 344 KB Output is correct
6 Correct 6 ms 340 KB Output is correct
7 Incorrect 1 ms 344 KB Incorrect
8 Halted 0 ms 0 KB -