답안 #840719

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
840719 2023-08-31T16:12:56 Z de_sousa 가장 긴 여행 (IOI23_longesttrip) C++17
5 / 100
12 ms 332 KB
#include "longesttrip.h"
#include <bits/stdc++.h>
using namespace std;

std::vector<int> longest_trip(int N, int D)
{


    vector<vector<int>> S;
    {
	
	vector<int> R;
	{
	    vector<int> tmp;
	    for(int i=0;i<N;i++)tmp.push_back(i);
	    for(int i=0;i<N;i++){
		int x=rand()%tmp.size();
		swap(tmp[x],tmp.back());
		R.push_back(tmp.back());
		tmp.pop_back();
	    }
	}
    
	vector<int> C;
	for(int i=1;i<N;i++){
	    C.push_back(are_connected({R[i-1]},{R[i]}));
	}

	vector<int> wS(N,-1);
	S.push_back({R[0]});
	wS[R[0]]=0;
	if(C[0]){
	    wS[R[1]]=0;
	    S[0].push_back(R[1]);
	}
	else{
	    wS[R[1]]=1;
	    S.push_back({R[1]});
	}
	for(int i=2;i<N;i++){
	    if(C[i-1]){
		wS[R[i]]=wS[R[i-1]];
		S[wS[R[i-1]]].push_back(R[i]);
	    }
	    else if(!C[i-2]){
		wS[R[i]]=wS[R[i-2]];
		S[wS[R[i-2]]].push_back(R[i]);
	    }
	    else{
		wS[R[i]]=S.size();
		S.push_back({R[i]});
	    }
	}
    }

    
    while(S.size()>2){
	
	vector<int> R;
	for(int i=0;i<S.size();i++){
	    R.push_back(S[i].back());
	}
	
	vector<int> C;
	for(int i=1;i<R.size();i++){
	    C.push_back(are_connected({R[i-1]},{R[i]}));
	}

	vector<int> con(R.size(),-1);
	int iter=0;
	while(iter<C.size()){
	    if(C[iter]){
		con[iter]=iter+1;
		con[iter+1]=-2;
		iter+=2;
		continue;
	    }
	    if(iter+1>=C.size())break;
	    if(C[iter+1]){
		con[iter+1]=iter+2;
		con[iter+2]=-2;
		iter+=3;
		continue;
	    }
	    con[iter]=iter+2;
	    con[iter+2]=-2;
	    iter+=3;
	    continue;
	}
	
	vector<vector<int>> nS;
	for(int i=0;i<S.size();i++){
	    if(con[i]==-2)continue;
	    if(con[i]==-1){
		nS.push_back(S[i]);
		continue;
	    }
	    reverse(S[con[i]].begin(),S[con[i]].end());
	    while(S[con[i]].size()){
		S[i].push_back(S[con[i]].back());
		S[con[i]].pop_back();
	    }
	    nS.push_back(S[i]);
	}
	S=nS;
    }
    
    if(S.size()==1)return S[0];

    if(S[0].size()<S[1].size())swap(S[0],S[1]);
    
    if(!are_connected(S[0],S[1]))
	return S[0];

    if(!are_connected({S[0][0]},{S[0].back()})){
	if(!are_connected({S[0].back()},{S[1].back()}))reverse(S[0].begin(),S[0].end());
	while(S[1].size()){
	    S[0].push_back(S[1].back());
	    S[1].pop_back();
	}
	return S[0];
    }

    if(S[1].size()!=1&&!are_connected({S[1][0]},{S[1].back()})){
	if(!are_connected({S[1].back()},{S[0].back()}))reverse(S[1].begin(),S[1].end());
	while(S[0].size()){
	    S[1].push_back(S[0].back());
	    S[0].pop_back();
	}
	return S[1];
    }


    vector<int> cand1=S[0];
    vector<int> cand2=S[1];

    while(cand1.size()!=1){
	vector<int> tmp1=cand1;
	vector<int> tmp2;
	int target=tmp1.size()/2;
	while(tmp1.size()!=target){
	    tmp2.push_back(tmp1.back());
	    tmp1.pop_back();
	}
	if(are_connected(tmp1,cand2))cand1=tmp1;
	else cand1=tmp2;	
    }
    swap(cand1,cand2);
    
    while(cand1.size()!=1){
	vector<int> tmp1=cand1;
	vector<int> tmp2;
	int target=tmp1.size()/2;
	while(tmp1.size()!=target){
	    tmp2.push_back(tmp1.back());
	    tmp1.pop_back();
	}
	if(are_connected(tmp1,cand2))cand1=tmp1;
	else cand1=tmp2;	
    }
    swap(cand1,cand2);

    int pos1=0;
    for(int i=0;i<S[0].size();i++){
	if(S[0][i]==cand1[0])pos1=i;
    }

    int pos2=0;
    for(int i=0;i<S[1].size();i++){
	if(S[1][i]==cand2[0])pos2=i;
    }

    vector<int> S3;
    S3.push_back(S[0][pos1]);
    for(int i=(pos1+1)%S[0].size();i!=pos1;i=(i+1)%S[0].size())S3.push_back(S[0][i]);
    reverse(S3.begin(),S3.end());
    S3.push_back(S[1][pos2]);
    for(int i=(pos2+1)%S[1].size();i!=pos2;i=(i+1)%S[1].size())S3.push_back(S[1][i]);
    return S3;
    

    
}

Compilation message

longesttrip.cpp: In function 'std::vector<int> longest_trip(int, int)':
longesttrip.cpp:60:15: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::vector<int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   60 |  for(int i=0;i<S.size();i++){
      |              ~^~~~~~~~~
longesttrip.cpp:65:15: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   65 |  for(int i=1;i<R.size();i++){
      |              ~^~~~~~~~~
longesttrip.cpp:71:12: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   71 |  while(iter<C.size()){
      |        ~~~~^~~~~~~~~
longesttrip.cpp:78:15: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   78 |      if(iter+1>=C.size())break;
      |         ~~~~~~^~~~~~~~~~
longesttrip.cpp:92:15: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::vector<int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   92 |  for(int i=0;i<S.size();i++){
      |              ~^~~~~~~~~
longesttrip.cpp:141:19: warning: comparison of integer expressions of different signedness: 'std::vector<int>::size_type' {aka 'long unsigned int'} and 'int' [-Wsign-compare]
  141 |  while(tmp1.size()!=target){
      |        ~~~~~~~~~~~^~~~~~~~
longesttrip.cpp:154:19: warning: comparison of integer expressions of different signedness: 'std::vector<int>::size_type' {aka 'long unsigned int'} and 'int' [-Wsign-compare]
  154 |  while(tmp1.size()!=target){
      |        ~~~~~~~~~~~^~~~~~~~
longesttrip.cpp:164:18: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  164 |     for(int i=0;i<S[0].size();i++){
      |                 ~^~~~~~~~~~~~
longesttrip.cpp:169:18: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  169 |     for(int i=0;i<S[1].size();i++){
      |                 ~^~~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 208 KB Output is correct
2 Incorrect 2 ms 208 KB Incorrect
3 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 7 ms 256 KB Output is correct
2 Correct 7 ms 208 KB Output is correct
3 Correct 8 ms 208 KB Output is correct
4 Correct 7 ms 208 KB Output is correct
5 Correct 6 ms 208 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 8 ms 208 KB Output is correct
2 Correct 7 ms 208 KB Output is correct
3 Correct 7 ms 208 KB Output is correct
4 Correct 7 ms 208 KB Output is correct
5 Correct 7 ms 208 KB Output is correct
6 Correct 12 ms 208 KB Output is correct
7 Incorrect 1 ms 208 KB Incorrect
8 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 6 ms 208 KB Output is correct
2 Correct 7 ms 208 KB Output is correct
3 Correct 8 ms 208 KB Output is correct
4 Correct 5 ms 208 KB Output is correct
5 Correct 6 ms 208 KB Output is correct
6 Correct 8 ms 208 KB Output is correct
7 Incorrect 0 ms 208 KB Incorrect
8 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 8 ms 332 KB Output is correct
2 Correct 7 ms 208 KB Output is correct
3 Correct 7 ms 208 KB Output is correct
4 Correct 7 ms 208 KB Output is correct
5 Correct 7 ms 256 KB Output is correct
6 Correct 11 ms 208 KB Output is correct
7 Incorrect 1 ms 208 KB Incorrect
8 Halted 0 ms 0 KB -