This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
/* Bismillahir Rahmanir Rahim */
#include <bits/stdc++.h>
#include "icc.h"
using namespace std;
typedef pair<int, int> pii;
typedef pair<pii, int> fii;
const int N = 105;
int n, par[N], mat[N][N];
set<int>comp;
vector<int>nudes[N];
int Find(int at){
return par[at] == at ? at : par[at] = Find(par[at]);
}
void Union(int a, int b){
int x = Find(a);
int y = Find(b);
for(auto u : nudes[y]){
nudes[x].push_back(u);
}
par[y] = x;
comp.erase(y);
}
/* void setRoad(int a, int b){ */
/* if(a) Union(a, b); */
/* int x, y; */
/* cin >> x >> y; */
/* mat[x][y] = 1; */
/* mat[y][x] = 1; */
/* } */
/* int query(int sa, int sb, int a[], int b[]){ */
/* for(int i=0;i<sa;i++){ */
/* for(int j=0;j<sb;j++){ */
/* if(mat[a[i]][b[j]]) return true; */
/* } */
/* } */
/* return false; */
/* } */
int bs[N], tt;
int get(int sa, int sb, int a[], int b[]){
int lo = 0, hi = sb - 1;
while(lo < hi){
int mid = (lo + hi) / 2; tt = 0;
for(int i=lo;i<=mid;i++) bs[tt] = b[i], ++tt;
if(query(sa, tt, a, bs)) hi = mid;
else lo = mid + 1;
}
return b[lo];
}
int aa[N], bb[N];
int sa, sb;
vector<int>vv[10][2];
void run(int _n){
srand(time(NULL));
n = _n;
comp.clear();
for(int i=1;i<=n;i++){
par[i] = i;
comp.insert(i);
nudes[i].push_back(i);
}
int road = n - 1;
while(road--){
int f1 = 0, f2 = 0;
vector<fii>F;
for(int bit=7;bit>=0;bit--){
vv[bit][0].clear();
vv[bit][1].clear();
for(auto u : comp){
if(u & (1 << bit)) for(auto v : nudes[u]) vv[bit][0].push_back(v);
else for(auto v : nudes[u]) vv[bit][1].push_back(v);
}
if(vv[bit][0].size() == 0) continue;
if(vv[bit][1].size() == 0) continue;
if(query(vv[bit][0].size(), vv[bit][1].size(), vv[bit][0].data(), vv[bit][1].data())){
F.push_back({{vv[bit][0].size(), bit}, 0});
F.push_back({{vv[bit][1].size(), bit}, 1});
//break;
}
}
sort(F.begin(), F.end());
for(auto u : F){
int b = u.first.second, t = u.second;
if(find(vv[b][t].begin(), vv[b][t].end(), f1) != vv[b][t].end()) continue;
if(find(vv[b][t].begin(), vv[b][t].end(), f2) != vv[b][t].end()) continue;
if(!f1){
f1 = get(vv[b][t^1].size(), vv[b][t].size(), vv[b][t^1].data(), vv[b][t].data());
}
else if(!f2){
f2 = get(vv[b][t^1].size(), vv[b][t].size(), vv[b][t^1].data(), vv[b][t].data());
}
}
assert(f1);
assert(f2);
Union(f1, f2);
setRoad(f1, f2);
}
}
/* int main(){ */
/* int ff, x, y; */
/* scanf("%d", &ff); */
/* setRoad(0, 0); */
/* run(ff); */
/* return 0; */
/* } */
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |