#include "meetings.h"
#include<bits/stdc++.h>
using namespace std;
using ll = long long;
using ull = unsigned long long;
using vi = vector<int>;
using vl = vector<ll>;
using vb = vector<bool>;
using pii = pair<int, int>;
using pll = pair<ll, ll>;
using ld = long double;
using str = string;
#define all(a) a.begin(), a.end()
#define print(a) for (auto elem:a) cout<<elem<<' '; cout<<'\n'
#define segprep(b) resize(1<<((int)ceil(log2(b.size()))+1))
#define FOR(a) for (int _ = 0; _ < a; _++)
const int SEED = 1;
void srt(int &a, int &b, int &c){
vi tmp = {a, b, c};
sort(all(tmp));
a = tmp.front();
b = tmp.at(1);
c = tmp.back();
}
int randint(const int l, const int r){
int res = rand()%(r-l)+l;
assert(l <= res && res < r);
return res;
}
struct query{
int u, v, w;
};
bool operator<(const query &a, const query&b){
return (vi{a.u, a.v, a.w} < vi{b.u, b.v, b.w});
}
map<query, int> asked;
int ask_query(int u, int v, int w){
srt(u, v, w);
query quest{u, v, w};
if (asked.find(quest) != asked.end()){
return asked.at(quest);
}
int ans = Query(u, v, w);
asked.insert({quest, ans});
return ans;
// return Query(u, v, w);
}
int n;
vector<vi> graph;
void solve_set(vi verts){
// print(verts);
if (verts.size() == 1) return;
if (verts.size() == 2){
int first = verts.front(), second = verts.back();
if (first > second) swap(first, second);
Bridge(first, second);
graph.at(first).push_back(second);
graph.at(second).push_back(first);
return;
}
int root_ind = randint(0, (int)verts.size());
int root = verts.at(root_ind);
verts.erase(verts.begin()+root_ind);
int second_ind = randint(0, (int)verts.size());
int second = verts.at(second_ind);
verts.erase(verts.begin()+second_ind);
// test roota
int a = 0, c = 0, b = 0, ans, sz = verts.size();
for (int i = 0; i < sz; i++){
ans = ask_query(root, second, verts.at(i));
if (ans == root){
c++;
}
else if (ans == second){
a++;
}
else{
b++;
}
}
if (abs(2*a-sz) < abs(2*c-sz)) swap(root, second);
vi subtr = {second};
vi left_nodes = {root};
vb in_path_to_second(n, false);
int vert;
for (int i = 0; i < sz; i++){
vert = verts.at(i);
ans = ask_query(root, second, vert);
if (ans == root){
left_nodes.push_back(vert);
continue;
}
subtr.push_back(vert);
if (ans == vert){
in_path_to_second.at(vert) = true;
}
// if (i == 5 && subtr.size() == 1){
// // slaby second, rerun
// second_ind = randint(0, (int)verts.size()-1);
// if (second_ind >= root_ind) second_ind++;
// second = verts.at(second_ind);
// subtr = {second};
// left_nodes = {root};
// fill(all(in_path_to_second), false);
// i = -1;
// }
}
solve_set(subtr);
solve_set(left_nodes);
// znalezc polaczenie
vert = second;
int parent = second;
bool moved;
// cout<<"przed petla: ";
// print(verts);
do{
// cout<<"hi\n";
moved = false;
for (const auto &son:graph.at(vert)){
if (son != parent && in_path_to_second.at(son)){
parent = vert;
vert = son;
moved = true;
break;
}
}
} while(moved);
if (vert > root) swap(vert, root);
Bridge(vert, root);
graph.at(vert).push_back(root);
graph.at(root).push_back(vert);
}
void Solve(int N){
n = N;
srand(SEED);
graph.resize(n);
vi nodes(n);
for (int i = 0; i < n; i++) nodes.at(i) = i;
shuffle(all(nodes), mt19937(SEED));
solve_set(nodes);
}
/*
const int MAX_N = 2000;
const int MAX_CALLS = 100000;
void WrongAnswer(int code) {
printf("Wrong Answer [%d]\n", code);
exit(0);
}
int N, num_calls;
std::vector<int> graph_[MAX_N];
std::set<std::pair<int, int>> edges, edges_reported;
int weight[MAX_N];
bool Visit(int p, int e, int rt = -1) {
if (p == e) {
++weight[p];
return true;
}
for (int q : graph_[p]) {
if (q != rt) {
if (Visit(q, e, p)) {
++weight[p];
return true;
}
}
}
return false;
}
int Query(int u, int v, int w) {
if (!(0 <= u && u <= N - 1 && 0 <= v && v <= N - 1 && 0 <= w && w <= N - 1 &&
u != v && u != w && v != w)) {
WrongAnswer(1);
}
if (++num_calls > MAX_CALLS) {
WrongAnswer(2);
}
std::fill(weight, weight + N, 0);
Visit(u, v);
Visit(u, w);
Visit(v, w);
for (int x = 0; x < N; ++x) {
if (weight[x] == 3) {
return x;
}
}
printf("Error: the input may be invalid\n");
exit(0);
}
void Bridge(int u, int v) {
if (!(0 <= u && u < v && v <= N - 1)) {
printf("u: %d, v: %d\n", u, v);
WrongAnswer(3);
}
if (!(edges.count(std::make_pair(u, v)) >= 1)) {
WrongAnswer(4);
}
if (!(edges_reported.count(std::make_pair(u, v)) == 0)) {
WrongAnswer(5);
}
edges_reported.insert(std::make_pair(u, v));
}
int main() {
if (scanf("%d", &N) != 1) {
fprintf(stderr, "Error while reading input\n");
exit(1);
}
for (int i = 0; i < N - 1; ++i) {
int u, v;
if (scanf("%d%d", &u, &v) != 2) {
fprintf(stderr, "Error while reading input\n");
exit(1);
}
graph_[u].push_back(v);
graph_[v].push_back(u);
edges.insert(std::make_pair(u, v));
}
num_calls = 0;
Solve(N);
if (edges_reported.size() != static_cast<size_t>(N - 1)) {
WrongAnswer(6);
}
printf("Accepted: %d\n", num_calls);
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... |