Submission #165391

#TimeUsernameProblemLanguageResultExecution timeMemory
165391AkashiMeetings (JOI19_meetings)C++14
100 / 100
1371 ms1016 KiB
#include "meetings.h"
#include <bits/stdc++.h>
using namespace std;

int global_A;
int n;
vector< pair<int, int> > sol;

inline void add_edge(int a, int b) {
    sol.push_back(make_pair(a, b));
}


bool cmp(int a, int b) {
    if (a == b) return false;
    return Query(global_A, a, b) == a;
}

void solve(vector<int> nodes) {
    if (nodes.size() == 1) return;
    if (nodes.size() == 2) {
        add_edge(nodes[0], nodes[1]);
        return;
    }
    int n = nodes.size();

    int A = nodes[rand() % n];
    int B;
    do {
        B = nodes[rand() % n];
    } while (A == B);

    vector<int> path;
    vector<int> query_ans(n);

    for (int i = 0; i < n; ++i)
        if (nodes[i] != A && nodes[i] != B) {
            query_ans[i] = Query(A, B, nodes[i]);
            if (query_ans[i] == nodes[i])
                path.push_back(nodes[i]);
        } else {
            if (nodes[i] == A) query_ans[i] = A;
            if (nodes[i] == B) query_ans[i] = B;
        }

    global_A = A;
    sort(path.begin(), path.end(), cmp);
    path.insert(path.begin(), A);
    path.push_back(B);
    for (size_t i = 0; i + 1 < path.size(); ++i)
        add_edge(path[i], path[i + 1]);

    vector< vector<int> > groups(path.size());
    for (int i = 0; i < n; ++i) {
        for (size_t j = 0; j < path.size(); ++j)
            if (path[j] == query_ans[i]) {
                groups[j].push_back(nodes[i]);
                break;
            }
    }
    for(auto it : groups)
        solve(it);
}

void print_tree() {
	for(auto it : sol) Bridge(min(it.first, it.second), max(it.first, it.second));
}

void Solve(int N) {
    n = N;
    srand(time(NULL));
    vector<int> nodes;
    for (int i = 0; i < n; ++i)
        nodes.push_back(i);
    sol.clear();
    solve(nodes);

    print_tree();
}

#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...