//#pragma GCC optimize("O3")
#include <bits/stdc++.h>
#include "icc.h"
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#include <ext/rope>
#define mem(a,v) memset((a), (v), sizeof (a))
#define enl printf("\n")
#define case(t) printf("Case #%d: ", (t))
#define ni(n) scanf("%d", &(n))
#define nl(n) scanf("%I64d", &(n))
#define nai(a, n) for (int i = 0; i < (n); i++) ni(a[i])
#define nal(a, n) for (int i = 0; i < (n); i++) nl(a[i])
#define pri(n) printf("%d\n", (n))
#define prl(n) printf("%I64d\n", (n))
#define pii pair<int, int>
#define pil pair<int, long long>
#define pll pair<long long, long long>
#define vii vector<pii>
#define vil vector<pil>
#define vll vector<pll>
#define vi vector<int>
#define vl vector<long long>
#define pb push_back
#define mp make_pair
#define fi first
#define se second
using namespace std;
using namespace __gnu_pbds;
typedef long long ll;
typedef cc_hash_table<int,int,hash<int>> ht;
typedef tree<int,null_type,less<int>,rb_tree_tag,tree_order_statistics_node_update> oset;
const double pi = acos(-1);
const int MOD = 1e9 + 7;
const int INF = 1e9 + 7;
const int MAXN = 1e2 + 5;
const double eps = 1e-9;
int par[MAXN], sz[MAXN];
vi comp[MAXN];
int qry(int x) { return par[x] == x ? par[x] : qry(par[x]); }
void join(int u, int v) {
u = qry(u), v = qry(v);
if (u == v) return;
if (sz[u] > sz[v]) swap(u, v);
par[u] = v;
comp[v].insert(comp[v].end(), comp[u].begin(), comp[u].end());
}
void print(vi cur) {
for (int i: cur) cerr << i << " "; cerr << "\n";
}
pii solve(int N) {
pii ret;
vi cand;
for (int i = 1; i <= N; i++) {
if (qry(i) != i)
continue;
cand.pb(i);
}
vi l, r;
int msk = 0;
for (int ind = 0; (1<<ind) < (int)cand.size(); ind++) {
l.clear();
r.clear();
for (int j = 0; j < (int)cand.size(); j++) {
if ((j >> ind) & 1)
l.insert(l.end(), comp[cand[j]].begin(), comp[cand[j]].end());
else
r.insert(r.end(), comp[cand[j]].begin(), comp[cand[j]].end());
}
int la[l.size()], ra[r.size()];
copy(l.begin(), l.end(), la);
copy(r.begin(), r.end(), ra);
int fl = query((int)l.size(),(int)r.size(), la, ra);
msk |= fl * (1 << ind);
}
l.clear();
r.clear();
for (int i = 0; i < (int)cand.size(); i++) {
if (i & (msk & -msk))
l.insert(l.end(), comp[cand[i]].begin(), comp[cand[i]].end());
else if ((i ^ msk) < (int)cand.size())
r.insert(r.end(), comp[cand[i]].begin(), comp[cand[i]].end());
}
int lo = 0, hi = l.size()-1;
if (hi == lo)
hi--;
while (lo <= hi) {
int mi = (lo+hi) / 2;
vi tmp;
for (int i = 0; i <= mi; i++)
tmp.pb(l[i]);
int la[tmp.size()], ra[r.size()];
copy(tmp.begin(), tmp.end(), la);
copy(r.begin(), r.end(), ra);
int fl = query((int)tmp.size(),(int)r.size(), la, ra);
if (fl == 1)
hi = mi - 1;
else
lo = mi + 1;
}
ret.fi = l[lo];
int ind = 0;
for (; ind < (int)cand.size(); ind++)
if (cand[ind] == qry(l[lo]))
break;
//cerr << l[lo] << " " << ind << " " << msk << "\n";
//print(l);
//print(r);
if (ind & (msk & -msk)) {
r.clear();
r.insert(r.end(), comp[cand[ind^msk]].begin(), comp[cand[ind^msk]].end());
} else {
l.clear();
l.insert(l.end(), comp[cand[ind^msk]].begin(), comp[cand[ind^msk]].end());
swap(l,r);
}
//print(l);
//print(r);
lo = 0, hi = r.size()-1;
if (hi == lo)
hi--;
while (lo <= hi) {
int mi = (lo+hi) / 2;
vi tmp;
for (int i = 0; i <= mi; i++)
tmp.pb(r[i]);
int la[l.size()], ra[tmp.size()];
copy(l.begin(), l.end(), la);
copy(tmp.begin(), tmp.end(), ra);
int fl = query((int)l.size(),(int)tmp.size(), la, ra);
if (fl == 1)
hi = mi - 1;
else
lo = mi + 1;
}
ret.se = r[lo];
//cerr << ret.fi << " " << ret.se << "\n-----------------------\n";
return ret;
}
void run(int N) {
for (int i = 1; i <= N; i++)
par[i] = i, sz[i] = 1, comp[i] = {i};
for (int t = 1; t < N; t++) {
int a, b;
tie(a, b) = solve(N);
setRoad(a, b);
join(a, b);
}
}
Compilation message
icc.cpp: In function 'void print(std::vector<int>)':
icc.cpp:51:2: warning: this 'for' clause does not guard... [-Wmisleading-indentation]
for (int i: cur) cerr << i << " "; cerr << "\n";
^~~
icc.cpp:51:37: note: ...this statement, but the latter is misleadingly indented as if it were guarded by the 'for'
for (int i: cur) cerr << i << " "; cerr << "\n";
^~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
9 ms |
504 KB |
Ok! 114 queries used. |
2 |
Correct |
9 ms |
504 KB |
Ok! 108 queries used. |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
48 ms |
504 KB |
Ok! 594 queries used. |
2 |
Correct |
43 ms |
596 KB |
Ok! 524 queries used. |
3 |
Correct |
44 ms |
564 KB |
Ok! 554 queries used. |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
152 ms |
564 KB |
Ok! 1443 queries used. |
2 |
Correct |
133 ms |
568 KB |
Ok! 1221 queries used. |
3 |
Correct |
155 ms |
600 KB |
Ok! 1528 queries used. |
4 |
Correct |
154 ms |
576 KB |
Ok! 1450 queries used. |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
150 ms |
572 KB |
Ok! 1459 queries used. |
2 |
Correct |
152 ms |
564 KB |
Ok! 1443 queries used. |
3 |
Correct |
159 ms |
604 KB |
Ok! 1510 queries used. |
4 |
Correct |
153 ms |
504 KB |
Ok! 1506 queries used. |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
151 ms |
504 KB |
Ok! 1452 queries used. |
2 |
Correct |
156 ms |
580 KB |
Ok! 1484 queries used. |
3 |
Correct |
155 ms |
604 KB |
Ok! 1448 queries used. |
4 |
Correct |
157 ms |
632 KB |
Ok! 1507 queries used. |
5 |
Correct |
154 ms |
568 KB |
Ok! 1497 queries used. |
6 |
Correct |
152 ms |
660 KB |
Ok! 1474 queries used. |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
154 ms |
504 KB |
Ok! 1453 queries used. |
2 |
Correct |
149 ms |
632 KB |
Ok! 1381 queries used. |