#include <iostream>
#include <cstdio>
#include <vector>
#include <algorithm>
#include <cstdlib>
#include "icc.h"
using namespace std;
const int maxn = 105, inf = int(1e9)+5;
int n, P[maxn];
pair<vector<int>, vector<int>> Q[10];
vector<int> vec[maxn];
inline int root(int node)
{
return ((P[node] == node)?node:(P[node] = root(P[node])));
}
void dsu(int x, int y)
{
x = root(x), y = root(y);
P[y] = x;
}
/*void setRoad(int a, int b)
{
cout << "SETROAD " << a << " " << b << "\n";
}
int query(int n, int m, int* a, int* b)
{
cout << "ASKQUERY\n";
cout << n << ": ";
for(int i = 0;i < n;i++) cout << a[i] << " ";
cout << "\n";
cout << m << ": ";
for(int i = 0;i < m;i++) cout << b[i] << " ";
cout << "\n";
int ret;
cin >> ret;
return ret;
}*/
bool ask(pair<vector<int>, vector<int>>& A)
{
int a_sz = int(A.first.size()), b_sz = int(A.second.size());
int q1[a_sz], q2[b_sz];
for(int i = 0;i < a_sz;i++) q1[i] = A.first[i]+1;
for(int i = 0;i < b_sz;i++) q2[i] = A.second[i]+1;
return query(a_sz, b_sz, q1, q2);
}
inline bool cmp(int a, int b)
{
return (int(vec[a].size()) > int(vec[b].size()));
}
int build(vector<int> todo, int ht)
{
if(int(todo.size()) == 1) return ht;
else
{
sort(todo.begin(), todo.end(), cmp);
vector<int> lef, righ;
for(int i = 0;i < int(todo.size());i++)
{
if(i%2) righ.push_back(todo[i]);
else lef.push_back(todo[i]);
}
for(auto it: lef)
{
for(auto gt: vec[it]) Q[ht].first.push_back(gt);
}
for(auto it: righ)
{
for(auto gt: vec[it]) Q[ht].second.push_back(gt);
}
return max(build(lef, ht+1), build(righ, ht+1));
}
}
pair<int, int> precise(pair<vector<int>, vector<int>>& A)
{
int L = 0, R = int(A.first.size())-2, ans = int(A.first.size())-1;
while(L <= R)
{
int mid = (L+R)/2;
pair<vector<int>, vector<int>> yolo;
yolo.second = A.second;
for(int i = 0;i <= mid;i++) yolo.first.push_back(A.first[i]);
if(ask(yolo))
{
ans = min(ans, mid);
R = mid-1;
}
else L = mid+1;
}
pair<int, int> ret = {A.first[ans], inf};
swap(A.first, A.second);
L = 0, R = int(A.first.size())-2, ans = int(A.first.size())-1;
while(L <= R)
{
int mid = (L+R)/2;
pair<vector<int>, vector<int>> yolo;
yolo.second = A.second;
for(int i = 0;i <= mid;i++) yolo.first.push_back(A.first[i]);
if(ask(yolo))
{
ans = min(ans, mid);
R = mid-1;
}
else L = mid+1;
}
ret.second = A.first[ans];
swap(A.first, A.second);
return ret;
}
void solve()
{
vector<int> complet;
for(int i = 0;i < n;i++) vec[i].clear();
for(int i = 0;i < n;i++)
{
vec[root(i)].push_back(i);
if(i == root(i)) complet.push_back(root(i));
}
int maxht = build(complet, 0);
pair<int, int> res;
for(int i = 0;i < maxht;i++)
{
if(Q[i].first.empty() || Q[i].second.empty()) continue;
if(ask(Q[i]))
{
res = precise(Q[i]);
break;
}
}
dsu(res.first, res.second);
setRoad(res.first+1, res.second+1);
for(int i = 0;i < maxht;i++) Q[i].first.clear(), Q[i].second.clear();
}
void run(int N)
{
srand(1234567890);
n = N;
for(int i = 0;i < n;i++) P[i] = i;
for(int i = 1;i < n;i++) solve();
}
/*int main(void)
{
int v;
cin >> v;
run(v);
}*/
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
6 ms |
2096 KB |
Ok! 100 queries used. |
2 |
Correct |
6 ms |
2092 KB |
Ok! 105 queries used. |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
33 ms |
2092 KB |
Ok! 519 queries used. |
2 |
Correct |
39 ms |
2088 KB |
Ok! 633 queries used. |
3 |
Correct |
43 ms |
2092 KB |
Ok! 644 queries used. |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
149 ms |
2220 KB |
Ok! 1513 queries used. |
2 |
Correct |
129 ms |
2220 KB |
Ok! 1584 queries used. |
3 |
Correct |
136 ms |
2220 KB |
Ok! 1493 queries used. |
4 |
Correct |
156 ms |
2228 KB |
Ok! 1622 queries used. |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
143 ms |
2220 KB |
Ok! 1597 queries used. |
2 |
Correct |
166 ms |
2220 KB |
Ok! 1638 queries used. |
3 |
Correct |
156 ms |
2224 KB |
Ok! 1584 queries used. |
4 |
Correct |
196 ms |
2224 KB |
Ok! 1575 queries used. |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
139 ms |
2224 KB |
Ok! 1594 queries used. |
2 |
Correct |
139 ms |
2228 KB |
Ok! 1590 queries used. |
3 |
Correct |
163 ms |
2220 KB |
Ok! 1599 queries used. |
4 |
Correct |
143 ms |
2220 KB |
Ok! 1637 queries used. |
5 |
Correct |
169 ms |
2224 KB |
Ok! 1575 queries used. |
6 |
Correct |
169 ms |
2220 KB |
Ok! 1639 queries used. |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
146 ms |
2224 KB |
Ok! 1590 queries used. |
2 |
Correct |
146 ms |
2224 KB |
Ok! 1584 queries used. |