제출 #820537

#제출 시각아이디문제언어결과실행 시간메모리
820537Faisal_Saqib도서관 (JOI18_library)C++17
100 / 100
131 ms592 KiB
#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); }

컴파일 시 표준 에러 (stderr) 메시지

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){
      |        ~~~~~~~~~~~~~~~~^~~~
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...