답안 #865454

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
865454 2023-10-24T08:42:02 Z boris_mihov Roads (CEOI20_roads) C++17
0 / 100
1000 ms 5824 KB
#include <algorithm>
#include <iostream>
#include <numeric>
#include <cassert>
#include <vector>
#include <random>
#include <queue>
#include <stack>
#include <set>

typedef long long llong;
const int MAXN = 200000 + 10;
const int MOD = 1e9 + 7;
const int INF = 2e9;

int n;
struct DSU
{
    int par[MAXN];
    int dep[MAXN];

    void build()
    {
        std::iota(par + 1, par + 1 + 2 * n, 1);
        std::fill(dep + 1, dep + 1 + 2 * n, 1);
    }

    int find(int node)
    {
        if (par[node] == node) return node;
        return par[node] = find(par[node]);
    }

    void connect(int u, int v)
    {
        u = find(u);
        v = find(v);
        assert(u != v);
    
        if (dep[u] > dep[v])
        {
            std::swap(u, v);
        }

        if (dep[u] == dep[v])
        {
            dep[v]++;
        }

        par[u] = v;
    }

    bool areConnected(int u, int v)
    {
        return find(u) == find(v);
    }
};

DSU dsu;
struct Point
{
    llong x, y;
};

llong dist(Point a, Point b)
{
    return abs(a.x - b.x) + abs(a.y - b.y);
}

Point p[MAXN];
int idx[MAXN];

struct MstElement
{
    int x, y;
    friend bool operator < (const MstElement &a, const MstElement &b)
    {
        return dist(p[a.x], p[a.y]) > dist(p[b.x], p[b.y]);
    }
};

std::mt19937 rng(69420);
void solve()
{
    dsu.build();
    for (int i = 1 ; i <= n ; ++i)
    {
        dsu.connect(i, n + i);
    }

    std::vector <std::pair <Point,Point>> answer;
    std::priority_queue <MstElement> mst;
    for (int i = 1 ; i <= n ; ++i)
    {   
        for (int j = 1 ; j <= n ; ++j)
        {
            if (i == j) continue;
            if (idx[i] == 0 || dist(p[i], p[j]) < dist(p[i], p[idx[i]]))
            {
                idx[i] = j;
            }
        }
        
        mst.push({i, idx[i]});
    } 

    while (answer.size() < n - 1)
    {
        auto [x, y] = mst.top();
        mst.pop();

        if (dsu.areConnected(x, y))
        {
            continue;
        }

        answer.push_back({p[x], p[y]});
        dsu.connect(x, y);

        idx[x] = idx[y] = 0;
        for (int i = 1 ; i <= n ; ++i)
        {
            if (dsu.areConnected(x, i)) continue;
            if (idx[x] == 0 || dist(p[x], p[i]) < dist(p[x], p[idx[x]]))
            {
                idx[x] = i;
            }
        }

        for (int i = 1 ; i <= n ; ++i)
        {
            if (dsu.areConnected(y, i)) continue;
            if (idx[y] == 0 || dist(p[y], p[i]) < dist(p[y], p[idx[y]]))
            {
                idx[y] = i;
            }
        }   

        mst.push({x, idx[x]});
        mst.push({y, idx[y]});
    }

    // assert(answer.size() == n - 1);
    for (const auto &[p1, p2] : answer)
    {
        std::cout << p1.x << ' ' << p1.y << ' ' << p2.x << ' ' << p2.y << '\n';
    }
}   

void input()
{
    std::cin >> n;
    for (int i = 1 ; i <= n ; ++i)
    {
        std::cin >> p[i].x >> p[i].y >> p[n + i].x >> p[n + i].y;
    }
}

void fastIOI()
{
    std::ios_base :: sync_with_stdio(0);
    std::cout.tie(nullptr);
    std::cin.tie(nullptr);
}

int main()
{
    fastIOI();
    input();
    solve();

    return 0;
}

Compilation message

roads.cpp: In function 'void solve()':
roads.cpp:107:26: warning: comparison of integer expressions of different signedness: 'std::vector<std::pair<Point, Point> >::size_type' {aka 'long unsigned int'} and 'int' [-Wsign-compare]
  107 |     while (answer.size() < n - 1)
      |            ~~~~~~~~~~~~~~^~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Failed 1 ms 4440 KB Condition failed: "!Cross(S[*pi], S[*pa])"
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 4444 KB Output is correct
2 Correct 1 ms 4444 KB Output is correct
3 Correct 12 ms 4444 KB Output is correct
4 Execution timed out 1027 ms 5824 KB Time limit exceeded
5 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 4444 KB Output is correct
2 Correct 1 ms 4440 KB Output is correct
3 Correct 12 ms 4656 KB Output is correct
4 Execution timed out 1034 ms 5400 KB Time limit exceeded
5 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 4444 KB Output is correct
2 Correct 1 ms 4444 KB Output is correct
3 Correct 12 ms 4664 KB Output is correct
4 Execution timed out 1043 ms 5636 KB Time limit exceeded
5 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Failed 1 ms 4444 KB Condition failed: "!Cross(S[*pi], S[*pa])"
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Failed 1 ms 4444 KB Condition failed: "!Cross(S[*pi], S[*pa])"
2 Halted 0 ms 0 KB -