Submission #80681

# Submission time Handle Problem Language Result Execution time Memory
80681 2018-10-22T02:47:30 Z admin Library (JOI18_library) C++17
100 / 100
235 ms 848 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 ){
 
	//printf("Divide %d %d %d\n",i,(int)V.size(),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()){
			//printf("Foi 1 %d\n",i);
			lastHalf.clear();
			divide_and_conquer(i,firstHalf,1);
		}
		else{
			//printf("Foi 2 %d\n",i);
			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){
 
	//printf("Comeca\n");
 
	N = n;
 
	vector<int> M(N);
 
	create(1);
	for(int i = 2;i<=N;i++){
		//printf("Testando %d\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){
			//printf("Solitario %d\n",i);
			create(i);
			continue;
		}
		else if(qtd == V.size()){
			divide_and_conquer(i, V, 1);
			//printf("Achei %d %d\n",i,temporario[i][0]);
			join_case1(temporario[i][0], i );
		}
		else{
			divide_and_conquer(i, V, 2);
			//printf("Achei %d %d %d\n",i,temporario[i][0],temporario[i][1]);
			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;
		}
	}
 
	//for(int i = 1;i<=N;i++){
	//	printf("I %d:",i);
	//	for(int j : grafo[i]) printf(" %d",j);
	//	printf("\n");
	//}
 
	while(resposta.size() != N){
		int x = resposta.back();
		//printf("Adicionando %d\n",x);
		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]
  int adj_v2x = 1 - adj_v1x;
      ^~~~~~~
library.cpp:58:6: warning: unused variable 'adj_v2y' [-Wunused-variable]
  int adj_v2y = 1 - adj_v1y;
      ^~~~~~~
library.cpp: In function 'void divide_and_conquer(int, std::vector<int>&, int)':
library.cpp:125:17: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
  for(int j = 0;j<V.size();j++){
                ~^~~~~~~~~
library.cpp:132:10: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
   if(qtd == firstHalf.size()){
      ~~~~^~~~~~~~~~~~~~~~~~~
library.cpp:144:10: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
   if(qtd == firstHalf.size()){
      ~~~~^~~~~~~~~~~~~~~~~~~
library.cpp:148:15: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
   else if(qtd == firstHalf.size() - 1){
           ~~~~^~~~~~~~~~~~~~~~~~~~~~~
library.cpp: In function 'void Solve(int)':
library.cpp:174:10: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
   if(qtd == V.size() + 1){
      ~~~~^~~~~~~~~~~~~~~
library.cpp:179:15: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
   else if(qtd == V.size()){
           ~~~~^~~~~~~~~~~
library.cpp:206:24: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
  while(resposta.size() != N){
        ~~~~~~~~~~~~~~~~^~~~
# Verdict Execution time Memory Grader output
1 Correct 19 ms 376 KB # of queries: 1201
2 Correct 20 ms 588 KB # of queries: 1216
3 Correct 22 ms 588 KB # of queries: 1244
4 Correct 15 ms 684 KB # of queries: 1280
5 Correct 21 ms 684 KB # of queries: 1255
6 Correct 16 ms 684 KB # of queries: 1248
7 Correct 16 ms 684 KB # of queries: 1264
8 Correct 19 ms 684 KB # of queries: 1185
9 Correct 20 ms 684 KB # of queries: 1255
10 Correct 11 ms 684 KB # of queries: 724
11 Correct 2 ms 684 KB # of queries: 0
12 Correct 2 ms 684 KB # of queries: 2
13 Correct 2 ms 684 KB # of queries: 4
14 Correct 2 ms 684 KB # of queries: 7
15 Correct 2 ms 684 KB # of queries: 48
16 Correct 3 ms 684 KB # of queries: 107
# Verdict Execution time Memory Grader output
1 Correct 19 ms 376 KB # of queries: 1201
2 Correct 20 ms 588 KB # of queries: 1216
3 Correct 22 ms 588 KB # of queries: 1244
4 Correct 15 ms 684 KB # of queries: 1280
5 Correct 21 ms 684 KB # of queries: 1255
6 Correct 16 ms 684 KB # of queries: 1248
7 Correct 16 ms 684 KB # of queries: 1264
8 Correct 19 ms 684 KB # of queries: 1185
9 Correct 20 ms 684 KB # of queries: 1255
10 Correct 11 ms 684 KB # of queries: 724
11 Correct 2 ms 684 KB # of queries: 0
12 Correct 2 ms 684 KB # of queries: 2
13 Correct 2 ms 684 KB # of queries: 4
14 Correct 2 ms 684 KB # of queries: 7
15 Correct 2 ms 684 KB # of queries: 48
16 Correct 3 ms 684 KB # of queries: 107
17 Correct 220 ms 740 KB # of queries: 8537
18 Correct 211 ms 740 KB # of queries: 8405
19 Correct 208 ms 748 KB # of queries: 8447
20 Correct 191 ms 848 KB # of queries: 7925
21 Correct 173 ms 848 KB # of queries: 7460
22 Correct 215 ms 848 KB # of queries: 8529
23 Correct 176 ms 848 KB # of queries: 8510
24 Correct 68 ms 848 KB # of queries: 3896
25 Correct 235 ms 848 KB # of queries: 8330
26 Correct 180 ms 848 KB # of queries: 7781
27 Correct 77 ms 848 KB # of queries: 3880
28 Correct 56 ms 848 KB # of queries: 1998
29 Correct 51 ms 848 KB # of queries: 1996
30 Correct 79 ms 848 KB # of queries: 1998