#include "icc.h"
#include <cstdio>
#include <vector>
#include <cassert>
struct UnionFind{
std::vector<int> uf;
UnionFind(int N):uf(N){
for(int i=0;i<N;i++){
uf[i]=i;
}
}
int find(int a){
while(a!=uf[a]){
a=uf[a]=uf[uf[a]];
}
return a;
}
void merge(int a,int b){
uf[find(a)]=find(b);
}
int size(){
return uf.size();
}
bool isRoot(int a){
return a==uf[a];
}
};
bool query(std::vector<int> a,std::vector<int> b){
for(int& x:a) x++;
for(int& x:b) x++;
return query(a.size(),b.size(),a.data(),b.data());
}
template<class Iterator>
bool query(Iterator b1,Iterator e1,Iterator b2,Iterator e2){
return query(std::vector<int>(b1,e1),std::vector<int>(b2,e2));
}
template<class Iterator>
std::pair<int,int> locate(Iterator b1,Iterator e1,Iterator b2,Iterator e2){
while(e1-b1!=1){
Iterator m1=b1+(e1-b1)/2;
if(query(b1,m1,b2,e2)){
e1=m1;
}else{
b1=m1;
}
}
while(e2-b2!=1){
Iterator m2=b2+(e2-b2)/2;
if(query(b1,e1,b2,m2)){
e2=m2;
}else{
b2=m2;
}
}
return std::pair<int,int>(*b1,*b2);
}
template<class Iterator>
std::pair<int,int> locate(Iterator begin,Iterator end){
for(int k=0;(1<<k)<end-begin;k++){
std::vector<int> as,bs;
for(int i=0;i<end-begin;i++){
if(i&(1<<k)){
as.push_back(*(begin+i));
}else{
bs.push_back(*(begin+i));
}
}
if(query(as,bs)){
return locate(as.begin(),as.end(),bs.begin(),bs.end());
}
}
assert(0);
}
void solve(UnionFind& uf){
std::vector<int> reps;
for(int i=0;i<uf.size();i++){
if(uf.isRoot(i)){
reps.push_back(i);
}
}
std::pair<int,int> pair=locate(reps.begin(),reps.end());
std::vector<int> as,bs;
for(int i=0;i<uf.size();i++){
if(uf.find(i)==uf.find(pair.first)){
as.push_back(i);
}
if(uf.find(i)==uf.find(pair.second)){
bs.push_back(i);
}
}
pair=locate(as.begin(),as.end(),bs.begin(),bs.end());
setRoad(pair.first+1,pair.second+1);
uf.merge(pair.first,pair.second);
}
void run(int N){
struct UnionFind uf(N);
for(int i=0;i<N-1;i++){
solve(uf);
}
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Runtime error |
5 ms |
888 KB |
Execution killed with signal 11 (could be triggered by violating memory limits) |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Runtime error |
4 ms |
888 KB |
Execution killed with signal 11 (could be triggered by violating memory limits) |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Runtime error |
6 ms |
892 KB |
Execution killed with signal 11 (could be triggered by violating memory limits) |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Runtime error |
6 ms |
888 KB |
Execution killed with signal 11 (could be triggered by violating memory limits) |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Runtime error |
6 ms |
888 KB |
Execution killed with signal 11 (could be triggered by violating memory limits) |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Runtime error |
6 ms |
888 KB |
Execution killed with signal 11 (could be triggered by violating memory limits) |
2 |
Halted |
0 ms |
0 KB |
- |