# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
858506 | Tenis0206 | ICC (CEOI16_icc) | C++11 | 0 ms | 0 KiB |
This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <bits/stdc++.h>
using namespace std;
const int nmax = 100;
#ifdef home
pair<int,int> e[nmax + 5];
void output(string msg)
{
cout<<msg<<'\n';
exit(0);
}
int cnt = 1;
int query(int sza, int szb, int a[], int b[])
{
bool ok = false;
for(int i=0;i<sza;i++)
{
for(int j=0;j<szb;j++)
{
for(int nr=1;nr<=cnt;nr++)
{
if(e[nr].first==a[i] && e[nr].second==b[i])
{
ok = true;
break;
}
if(e[nr].first==b[i] && e[nr].second==a[i])
{
ok = true;
break;
}
}
}
}
return ok;
}
void setRoad(int a, int b)
{
bool ok = false;
if(a==e[cnt].first && b==e[cnt].second)
{
ok = true;
}
if(a==e[cnt].second && b==e[cnt].first)
{
ok = true;
}
if(!ok)
{
output("Bad edge");
}
++cnt;
}
#endif // home
int n;
int a[nmax + 5], b[nmax + 5];
int vst[nmax + 5];
int t[nmax + 5];
vector<int> l[nmax + 5];
int rep(int x)
{
if(t[x]==x)
{
return x;
}
return rep(t[x]);
}
void uneste(int x, int y)
{
x = rep(x);
y = rep(y);
if(x==y)
{
return;
}
if(l[x].size() > l[y].size())
{
for(auto it : l[y])
{
l[x].push_back(it);
}
l[y].clear();
t[y] = x;
}
else
{
for(auto it : l[x])
{
l[y].push_back(it);
}
l[x].clear();
t[x] = y;
}
}
pair<int,int> find_dif(int nra, int nrb, int a[], int b[])
{
int val_a = 0, val_b = 0;
int st = 0;
int dr = nra - 1;
while(st<=dr)
{
int mij = (st + dr) >> 1;
int nrv = 0;
for(int i=st;i<=mij;i++)
{
vst[nrv++] = a[i];
}
int ok = query(nrv, nrb, vst, b);
if(ok)
{
dr = mij;
}
else
{
st = mij + 1;
}
}
val_a = a[st];
st = 0;
dr = nrb - 1;
while(st<=dr)
{
int mij = (st + dr) >> 1;
int nrv = 0;
for(int i=st;i<=mij;i++)
{
vst[nrv++] = a[i];
}
int ok = query(nrv, nra, vst, a);
if(ok)
{
dr = mij;
}
else
{
st = mij + 1;
}
}
val_b = b[st];
return {val_a, val_b};
}
void find_new_edge()
{
vector<int> c;
for(int i=1;i<=n;i++)
{
if(rep(i)==i)
{
c.push_back(i);
}
}
pair<int,int> rez = {0,0};
for(int bit=0;(1<<bit)<c.size();bit++)
{
int nra = 0, nrb = 0;
for(int i=0;i<c.size();i++)
{
if((i & (1<<bit)) == 0)
{
for(auto it : l[c[i]])
{
a[nra++] = it;
}
}
else
{
for(auto it : l[c[i]])
{
b[nrb++] = it;
}
}
}
int ok = query(nra, nrb, a, b);
if(ok)
{
rez = find_dif(nra, nrb, a, b);
break;
}
}
setRoad(rez.first,rez.second);
uneste(rez.first, rez.second);
}
void run(int N)
{
n = N;
for(int i=1;i<=n;i++)
{
t[i] = i;
l[i].push_back(i);
}
for(int i=1;i<n;i++)
{
find_new_edge();
}
}
#ifdef home
int main()
{
freopen("nr.in","r",stdin);
freopen("nr.out","w",stdout);
cin>>n;
for(int i=1;i<n;i++)
{
int x,y;
cin>>x>>y;
e[i] = {x,y};
}
output("OK");
return 0;
}
#endif // home