답안 #820537

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
820537 2023-08-11T04:41:16 Z Faisal_Saqib 도서관 (JOI18_library) C++17
100 / 100
131 ms 592 KB
#include <cstdio>
#include <vector>
#include "library.h"
using namespace std;
 
const int MAXN = 1001;
 
static int pai[MAXN],v1[MAXN],v2[MAXN],N;
static vector<int> grafo[MAXN],temporario[MAXN],filhos[MAXN],M;
 
static void addEdge(int a,int b){
	grafo[a].push_back(b);
	grafo[b].push_back(a);
}
 
static int isAdj(int a,int b){
 
	M.clear();
	M.assign(N,0);
 
	M[a-1] = 1;
	M[b-1] = 1;
 
	int ans = Query(M);
	return (ans == 1);
 
}
 
static int find(int x){
	if(x == pai[x]) return x;
	return pai[x] = find(pai[x]);
}
 
static void join_case1(int x,int i){
 
	pai[i] = x;
 
	if(isAdj(v1[x],i)){
		addEdge(v1[x],i);
		v1[x] = i;
	}
	else{
		addEdge(v2[x],i);
		v2[x] = i;
	}
 
	filhos[x].push_back(i);
 
}
 
static void join_case2(int x,int y,int i){
 
	if(x > y) swap(x,y);
 
	int adj_v1x = isAdj(v1[x],i);
	int adj_v2x = 1 - adj_v1x;
	int adj_v1y = isAdj(v1[y],i);
	int adj_v2y = 1 - adj_v1y;
 
	if(adj_v1x) addEdge(v1[x],i);
	else addEdge(v2[x],i);
	if(adj_v1y) addEdge(v1[y],i);
	else addEdge(v2[y],i);
 
	if(adj_v1x == 1 && adj_v1y == 1){
		v1[x] = v2[y];
 
	}
	else if(adj_v1x == 1 && adj_v1y == 0){
		v1[x] = v1[y];
	}
	else if(adj_v1x == 0 && adj_v1y == 1){
		v2[x] = v2[y];
	}
	else{
		v2[x] = v1[y];
	}
 
	pai[y] = x;
	pai[i] = x;
 
	filhos[x].push_back(i);
	for(int j : filhos[y]) filhos[x].push_back(j);
 
	filhos[y].clear();;
 
}
 
static void create(int i){
	pai[i] = i;
	v1[i] = i;
	v2[i] = i;
	filhos[i].push_back(i);
}
 
static int doQuery(int i,vector<int>& V){
 
	M.clear();
	M.assign(N,0);
 
	M[i-1] = 1;
	for(int j : V){
		for(int k : filhos[j]){
			M[k-1] = 1;
		}
	}
 
	return Query(M);
 
}
 
static void divide_and_conquer(int i, vector<int>& V, int caso ){
  
	if(caso == 0) return;
	if(V.size() == 1){
		temporario[i].push_back(V[0]);
		return;
	}
 
	int mid = V.size()/2;
	vector<int> firstHalf,lastHalf;
 
	for(int j = 0;j<V.size();j++){
		if(j < mid) firstHalf.push_back(V[j]);
		else lastHalf.push_back(V[j]);
	}
 
	int qtd = doQuery(i,firstHalf);
	if(caso == 1){
		if(qtd == firstHalf.size()){
			lastHalf.clear();
			divide_and_conquer(i,firstHalf,1);
		}
		else{
			firstHalf.clear();
			divide_and_conquer(i,lastHalf,1);
		}
	}
	else if(caso == 2){
		if(qtd == firstHalf.size()){
			divide_and_conquer(i,firstHalf,1);
			divide_and_conquer(i,lastHalf,1);
		}
		else if(qtd == firstHalf.size() - 1){
			lastHalf.clear();
			divide_and_conquer(i,firstHalf,2);
		}
		else{
			firstHalf.clear();
			divide_and_conquer(i,lastHalf,2);
		}
	}
 
}
 
void Solve(int n){
  
	N = n;
 
	vector<int> M(N);
 
	create(1);
	for(int i = 2;i<=N;i++){
		vector<int> V;
		for(int j = 1;j<i;j++) if(find(j) == j) V.push_back(j);
		int qtd = doQuery(i,V);
		if(qtd == V.size() + 1){
			create(i);
			continue;
		}
		else if(qtd == V.size()){
			divide_and_conquer(i, V, 1);
			join_case1(temporario[i][0], i );
		}
		else{
			divide_and_conquer(i, V, 2);
			join_case2(temporario[i][0], temporario[i][1], i );
		}
	}
 
	int last = -1;
	vector<int> resposta;
	for(int i = 1;i<=N;i++){
		if(grafo[i].size() <= 1){
			resposta.push_back(i);
			break;
		}
	}
  
	while(resposta.size() != N){
		int x = resposta.back();
		if(grafo[x][0] != last){
			resposta.push_back(grafo[x][0]);
		}
		else{
			resposta.push_back(grafo[x][1]);
		}
		last = x;
	}
 
	Answer(resposta);
 
}

Compilation message

library.cpp: In function 'void join_case2(int, int, int)':
library.cpp:56:6: warning: unused variable 'adj_v2x' [-Wunused-variable]
   56 |  int adj_v2x = 1 - adj_v1x;
      |      ^~~~~~~
library.cpp:58:6: warning: unused variable 'adj_v2y' [-Wunused-variable]
   58 |  int adj_v2y = 1 - adj_v1y;
      |      ^~~~~~~
library.cpp: In function 'void divide_and_conquer(int, std::vector<int>&, int)':
library.cpp:123:17: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  123 |  for(int j = 0;j<V.size();j++){
      |                ~^~~~~~~~~
library.cpp:130:10: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  130 |   if(qtd == firstHalf.size()){
      |      ~~~~^~~~~~~~~~~~~~~~~~~
library.cpp:140:10: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  140 |   if(qtd == firstHalf.size()){
      |      ~~~~^~~~~~~~~~~~~~~~~~~
library.cpp:144:15: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  144 |   else if(qtd == firstHalf.size() - 1){
      |           ~~~~^~~~~~~~~~~~~~~~~~~~~~~
library.cpp: In function 'void Solve(int)':
library.cpp:167:10: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  167 |   if(qtd == V.size() + 1){
      |      ~~~~^~~~~~~~~~~~~~~
library.cpp:171:15: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  171 |   else if(qtd == V.size()){
      |           ~~~~^~~~~~~~~~~
library.cpp:190:24: warning: comparison of integer expressions of different signedness: 'std::vector<int>::size_type' {aka 'long unsigned int'} and 'int' [-Wsign-compare]
  190 |  while(resposta.size() != N){
      |        ~~~~~~~~~~~~~~~~^~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 15 ms 396 KB # of queries: 1201
2 Correct 13 ms 336 KB # of queries: 1216
3 Correct 16 ms 336 KB # of queries: 1244
4 Correct 14 ms 388 KB # of queries: 1280
5 Correct 14 ms 464 KB # of queries: 1255
6 Correct 18 ms 336 KB # of queries: 1248
7 Correct 16 ms 464 KB # of queries: 1264
8 Correct 13 ms 336 KB # of queries: 1185
9 Correct 15 ms 336 KB # of queries: 1255
10 Correct 9 ms 336 KB # of queries: 724
11 Correct 0 ms 336 KB # of queries: 0
12 Correct 0 ms 336 KB # of queries: 2
13 Correct 1 ms 336 KB # of queries: 4
14 Correct 1 ms 336 KB # of queries: 7
15 Correct 1 ms 336 KB # of queries: 48
16 Correct 1 ms 336 KB # of queries: 107
# 결과 실행 시간 메모리 Grader output
1 Correct 15 ms 396 KB # of queries: 1201
2 Correct 13 ms 336 KB # of queries: 1216
3 Correct 16 ms 336 KB # of queries: 1244
4 Correct 14 ms 388 KB # of queries: 1280
5 Correct 14 ms 464 KB # of queries: 1255
6 Correct 18 ms 336 KB # of queries: 1248
7 Correct 16 ms 464 KB # of queries: 1264
8 Correct 13 ms 336 KB # of queries: 1185
9 Correct 15 ms 336 KB # of queries: 1255
10 Correct 9 ms 336 KB # of queries: 724
11 Correct 0 ms 336 KB # of queries: 0
12 Correct 0 ms 336 KB # of queries: 2
13 Correct 1 ms 336 KB # of queries: 4
14 Correct 1 ms 336 KB # of queries: 7
15 Correct 1 ms 336 KB # of queries: 48
16 Correct 1 ms 336 KB # of queries: 107
17 Correct 118 ms 440 KB # of queries: 8537
18 Correct 119 ms 396 KB # of queries: 8405
19 Correct 123 ms 548 KB # of queries: 8447
20 Correct 99 ms 424 KB # of queries: 7925
21 Correct 98 ms 540 KB # of queries: 7460
22 Correct 131 ms 536 KB # of queries: 8529
23 Correct 120 ms 412 KB # of queries: 8510
24 Correct 48 ms 392 KB # of queries: 3896
25 Correct 115 ms 560 KB # of queries: 8330
26 Correct 78 ms 388 KB # of queries: 7781
27 Correct 59 ms 400 KB # of queries: 3880
28 Correct 26 ms 428 KB # of queries: 1998
29 Correct 26 ms 592 KB # of queries: 1996
30 Correct 27 ms 428 KB # of queries: 1998