This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#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 (stderr)
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 |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |