# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|
1133301 | | qrn | ICC (CEOI16_icc) | C++20 | | 2 ms | 584 KiB |
// ivan carvalhoya minnetdarligimi bildirirem
#include "icc.h"
#include <bits/stdc++.h>
using namespace std;
#define pb push_back
// vector<pair<int,int>>roads;
// int ind = 0;
// int query(int a, int b, int a1[], int b1[]) {
// for(int i = 0; i < a; i++) {
// for(int j = 0; j < b; j++) {
// if(roads[ind].first == a1[i] && roads[ind].second == b1[j]) {
// return 1;
// }
// }
// }
// return 0;
// }
// void setRoad(int a, int b) {
// cout << "Setting the road was succesful. " << a << " " << b << endl;
// }
struct DSU {
vector<vector<int>> component;
vector<int> parent, sze;
DSU(int n) {
parent.assign(n + 1, 0ll);
sze.assign(n + 1, 0ll);
component.resize(n + 1);
for(int i = 1; i <= n; i++) {
sze[i] = 1;
parent[i] = i;
component[i].push_back(i);
}
}
int root(int x) {
if(x == parent[x]) return x;
return parent[x] = root(parent[x]);
}
void unite(int a, int b) {
a = root(a);
b = root(b);
if(a == b) return;
if(sze[a] > sze[b]) swap(a, b);
sze[a] += sze[b];
parent[b] = a;
for(auto u : component[b]) component[a].push_back(u);
component[b].clear();
}
};
void run(int N) {
// cout << "ZOR" << endl;
srand(time(0));
DSU dsu(N + 1);
for(int turn = 0; turn < N - 1; turn++) {
vector<int> upA, downA, total, UpA;
for(int i = 1; i <= N; i++) {
if(dsu.root(i) == i) {
total.push_back(i);
}
}
// cout << "Z" << endl;
random_shuffle(total.begin(), total.end());
// cout << "ZOR" << endl;
while(true) {
for(int i = 0; i < total.size() / 2; i++) {
int head = total[i];
for(int j = 0; j < dsu.component[head].size(); j++) {
upA.push_back(dsu.component[head][j]);
}
}
for(int i = total.size() / 2; i < total.size(); i++) {
int head = total[i];
for(int j = 0; j < dsu.component[head].size(); j++) {
downA.push_back(dsu.component[head][j]);
}
}
if(query(upA.size(), downA.size(), upA.data(), downA.data())) {
break;
} else {
random_shuffle(total.begin(), total.end());
upA.clear();
downA.clear();
}
}
UpA = upA;
// cout << "Current turn: " << turn << endl;
// for(int i : upA) cout << i << " ";
// cout << endl;
// for(int i : downA) cout << i << " ";
// cout << endl;
int l = 0, r = upA.size() - 1;
while(l < r) {
int mid = (l + r) / 2;
vector<int> left, right;
for(int i = 0; i <= mid; i++) left.push_back(upA[i]);
for(int i = mid + 1; i < upA.size(); i++) right.push_back(upA[i]);
if(query(left.size(), downA.size(), left.data(), downA.data())) {
upA = left;
r = mid;
} else {
upA = right;
l = mid + 1;
}
}
l = 0, r = downA.size() - 1;
while(l < r) {
int mid = (l + r) / 2;
vector<int> left, right;
for(int i = 0; i <= mid; i++) left.push_back(downA[i]);
for(int i = mid + 1; i < downA.size(); i++) right.push_back(downA[i]);
if(query(UpA.size(), left.size(), UpA.data(), left.data())) {
downA = left;
r = mid;
} else {
downA = right;
l = mid + 1;
}
}
int ep1 = upA[0], ep2 = downA[0];
setRoad(ep1, ep2);
dsu.unite(ep1, ep2);
// ind++;
}
}
// int main() {
// int n;
// cin >> n;
// roads.pb({2, 4});
// roads.pb({1, 3});
// roads.pb({4, 1});
// run(n);
// }
# | 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... |