Submission #921423

# Submission time Handle Problem Language Result Execution time Memory
921423 2024-02-03T20:06:09 Z boris_mihov Meetings (JOI19_meetings) C++17
0 / 100
1732 ms 780 KB
#include "meetings.h"
#include <algorithm>
#include <iostream>
#include <numeric>
#include <cassert>
#include <random>
#include <vector>

typedef long long llong;
const int MAXN = 2000 + 10;
const int INF  = 1e9;

int n;
int sz[MAXN];
bool bl[MAXN];
int perm[MAXN];
std::vector <int> g[MAXN];
std::vector <int> c[MAXN];
std::vector <int> real[MAXN];
std::mt19937 rng(69420);

void removeEdge(int u, int v)
{
    if (u == -1 || v == -1)
    {
        return;
    }
    
    for (int &x : g[u])
    {
        if (x == v)
        {
            std::swap(x, g[u].back());
            g[u].pop_back();
            break;
        }
    }

    for (int &x : g[v])
    {
        if (x == u)
        {
            std::swap(x, g[v].back());
            g[v].pop_back();
            break;
        }
    }
}

void addEdge(int u, int v)
{
    // std::cout << "add edge: " << u << ' ' << v << '\n';
    if (u == -1 || v == -1)
    {
        return;
    }

    g[u].push_back(v);
    g[v].push_back(u);
}

void calcSize(int node, int par)
{
    sz[node] = 1;
    for (const int &u : g[node])
    {
        if (u == par || bl[u])
        {
            continue;
        }

        calcSize(u, node);
        sz[node] += sz[u];
    }
}

int findCentroid(int node, int par, int compSz)
{
    for (const int &u : g[node])
    {
        if (!bl[u] && u != par && sz[u] > compSz / 2)
        {
            return findCentroid(u, node, compSz);
        }
    }

    return node;
}

int decompose(int node)
{
    calcSize(node, -1);
    int cntr = findCentroid(node, -1, sz[node]);
    bl[cntr] = true;

    for (const int &u : g[cntr])
    {
        if (bl[u])
        {
            continue;
        }

        c[cntr].push_back(decompose(u));
        real[cntr].push_back(u);
    }

    return cntr;
}

void search(int node, int added)
{
    for (int i = 0 ; i < c[node].size() ; i++)
    {
        int res = Query(node, real[node][i], added);
        if (res == added)
        {
            removeEdge(node, real[node][i]);
            addEdge(node, added);
            addEdge(real[node][i], added);
            return;
        }

        if (res == real[node][i])
        {
            search(c[node][i], added);
            return;
        }
    }

    addEdge(node, added);
}

void Solve(int N) 
{
    n = N;
    std::iota(perm, perm + n, 0);
    // std::sort(perm + 1, perm + n, [&](int x, int y)
    // {
    //     int res = Query(0, x, y);
    //     if (res == x) return true;
    //     if (res == y) return false;
    //     return false;
    // });

    for (int i = 1 ; i < n ; ++i)
    {
        for (int j = i + 1 ; j < n ; ++j)
        {
            if (Query(0, perm[i], perm[j]) == perm[i])
            {
                std::swap(perm[i], perm[j]);
            }
        }
    }

    g[perm[0]].push_back(perm[1]);
    g[perm[1]].push_back(perm[0]);

    // std::cout << "first: " << perm[0] << '\n';
    // std::cout << "second: " << perm[1] << '\n';
    for (int i = 2 ; i < n ; ++i)
    {
        // std::cout << "here: " << perm[i] << '\n';
        for (int j = 0 ; j < n ; ++j)
        {
            c[j].clear();
            real[j].clear();
            std::shuffle(g[j].begin(), g[j].end(), rng);
        }

        std::fill(bl, bl + n, false);
        int root = decompose(perm[0]);        
    
        // for (int j = 0 ; j < n ; ++j)
        // {
        //     std::shuffle(c[j].begin(), c[j].end(), rng);
        // }

        search(root, perm[i]);
    }

    for (int i = 0 ; i < n ; ++i)
    {
        for (const int &u : g[i])
        {
            if (i < u)
            {
                Bridge(i, u);
            }
        }
    }
}

Compilation message

meetings.cpp: In function 'void search(int, int)':
meetings.cpp:112:24: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  112 |     for (int i = 0 ; i < c[node].size() ; i++)
      |                      ~~^~~~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 0 ms 600 KB Output is correct
2 Correct 0 ms 600 KB Output is correct
3 Correct 1 ms 600 KB Output is correct
4 Incorrect 0 ms 644 KB Wrong Answer [4]
5 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 0 ms 600 KB Output is correct
2 Correct 0 ms 600 KB Output is correct
3 Correct 1 ms 600 KB Output is correct
4 Incorrect 0 ms 644 KB Wrong Answer [4]
5 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 0 ms 600 KB Output is correct
2 Correct 0 ms 600 KB Output is correct
3 Correct 1 ms 600 KB Output is correct
4 Incorrect 0 ms 644 KB Wrong Answer [4]
5 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 1732 ms 780 KB Wrong Answer [2]
2 Halted 0 ms 0 KB -