#include <bits/stdc++.h>
#include "library.h"
using namespace std;
int p[1024];
int fn(int x){
if(p[x] == -1) return x;
else return p[x] = fn(p[x]);
}
bool un(int x, int y){
x = fn(x);
y = fn(y);
if(x == y) return false;
p[x] = y;
return true;
}
int queryRange(int L, int R, int N, int sp = -1){
vector<int> M(N);
set<int> s;
for(int i = L; i <= R; i++){
s.insert(fn(i));
}
if(sp != -1) s.insert(fn(sp));
for(int i = 0; i < N; i++){
if(s.count(fn(i))) M[i] = 1;
else M[i] = 0;
}
return Query(M);
}
int countRange(int L, int R, int sp = -1){
set<int> s;
for(int i = L; i <= R; i++){
s.insert(fn(i));
}
if(sp != -1) s.insert(fn(sp));
return s.size();
}
int LeftTurn(int L, int N){
// L -> x duplicates
// L -> x-1 does not duplicate
if(queryRange(L, N-1, N, L) == countRange(L, N-1, L)) return -1;
int lo = L;
int hi = N-1;
int mid;
while(lo < hi){
mid = (lo + hi)/2;
if(queryRange(L, mid, N, L) == countRange(L, mid, L)) lo = mid+1;
else hi = mid;
}
return lo;
}
int RightTurn(int R, int N){
// x <- R duplicates
// x+1 <- R does not duplicate
if(queryRange(0, R, N, R) == countRange(0, R, R)) return -1;
int lo = 0;
int hi = R;
int mid;
while(lo < hi){
mid = (lo + hi + 1)/2;
if(queryRange(mid, R, N, R) == countRange(mid, R, R)) hi = mid-1;
else lo = mid;
}
return lo;
}
vector<int> g[1024];
void addEdge(int u, int v){
g[u].push_back(v);
g[v].push_back(u);
}
int findLeaf(int u, int p){
for(int v : g[u]){
if(v == p) continue;
return findLeaf(v, u);
}
return u;
}
pair<int,int> findExtremum(int u){
if(g[u].size() > 1){
return make_pair(findLeaf(g[u][0], u), findLeaf(g[u][1], u));
}else if(g[u].size() == 1){
return make_pair(u, findLeaf(g[u][0], u));
}else{
return make_pair(u, u);
}
}
bool checkPair(int l, int r, int N){
vector<int> M(N);
fill(M.begin(), M.end(), 0);
M[l] = M[r] = 1;
return Query(M) == 1;
}
void Solve(int N)
{
memset(p, -1, sizeof(p));
vector<int> M(N);
for(int i = 1; i < N; i++){
int l = LeftTurn(0, N);
int r = RightTurn(l, N);
cout << l << " " << r << endl;
int l1, l2;
tie(l1, l2) = findExtremum(l);
int r1, r2;
tie(r1, r2) = findExtremum(r);
if(checkPair(l1, r1, N)){
l = l1;
r = r1;
}else if(checkPair(l1, r2, N)){
l = l1;
r = r2;
}else if(checkPair(l2, r1, N)){
l = l2;
r = r1;
}else{
l = l2;
r = r2;
}
un(l, r);
addEdge(l, r);
}
int last = -1;
int leaf = findLeaf(0, 0);
vector<int> ans;
for(int i = 0; i < N; i++){
ans.push_back(leaf+1);
int nxt = -1;
for(int v: g[leaf]){
if(v != last) nxt = v;
}
last = leaf;
leaf = nxt;
}
Answer(ans);
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Runtime error |
1 ms |
364 KB |
Execution killed with signal 13 (could be triggered by violating memory limits) |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Runtime error |
1 ms |
364 KB |
Execution killed with signal 13 (could be triggered by violating memory limits) |
2 |
Halted |
0 ms |
0 KB |
- |