55532023-07-25 12:43:05szilRajzcpp17Accepted 100/10093ms30752 KiB
#include <bits/stdc++.h>
#define xy pair<ll, ll>

using ll = long long;
using namespace std;

const int MAXN = 1001;
const ll INF = 1e8;

struct Edge {
    ll dist; int u, v;
    bool cross;
};

int dsu[2*MAXN];
vector<xy> points;
vector<Edge> edges;

int find(int u) {
    return dsu[u]==u?u:dsu[u]=find(dsu[u]);
}

void unio(int u, int v) {
    u = find(u); v = find(v);
    if (u != v) {
        dsu[u] = v;
    }
}

bool cross(ll x1, ll y1, ll x2, ll y2) {
    return x1*y2 - x2*y1 < 0;
}

int main() {
    cin.sync_with_stdio(0); cin.tie(0);
    int n; cin >> n;
    iota(dsu, dsu+2*MAXN, 0);
    for (int i = 1; i <= n; i++) {
        ll x, y; cin >> x >> y;
        points.emplace_back(x, y);
    }
    for (int i = 0; i < n; i++) {
        for (int j = i + 1; j < n; j++) {
            Edge e;
            e.u = i; e.v = j;
            auto [x1, y1] = points[i];
            auto [x2, y2] = points[j];
            ll dx = x1 - x2;
            ll dy = y1 - y2;
            e.dist = dx*dx + dy*dy;
            if (y1 > y2) {
                swap(x1, x2);
                swap(y1, y2);
            }
            if (y1 <= 0 && y2 > 0 && cross(-x1, -y1, x2-x1, y2-y1)) e.cross = true;
            else e.cross = false;
            edges.emplace_back(e);
        }
    }
    sort(edges.begin(), edges.end(), [](Edge a, Edge b){
        return a.dist < b.dist;
    });
    for (Edge e : edges) {
        if (e.cross) {
            unio(e.u, e.v+n);
            unio(e.u+n, e.v);
        } else {
            unio(e.u, e.v);
            unio(e.u+n, e.v+n);
        }
        if (find(e.u) == find(e.u+n) || find(e.v) == find(e.v+n)) {
            cout << e.dist << "\n";
            return 0;
        }
    }
    cout << "-1\n";
    return 0;
}
SubtaskSumTestVerdictTimeMemory
subtask10/0
1Accepted3ms1904 KiB
2Accepted3ms2104 KiB
subtask220/20
3Accepted3ms2444 KiB
4Accepted3ms2500 KiB
5Accepted3ms2588 KiB
6Accepted3ms2676 KiB
7Accepted3ms2808 KiB
8Accepted3ms3040 KiB
9Accepted3ms3388 KiB
10Accepted3ms3484 KiB
11Accepted3ms3696 KiB
12Accepted3ms3768 KiB
subtask320/20
13Accepted3ms2444 KiB
14Accepted3ms2500 KiB
15Accepted3ms2588 KiB
16Accepted3ms2676 KiB
17Accepted3ms2808 KiB
18Accepted3ms3040 KiB
19Accepted3ms3388 KiB
20Accepted3ms3484 KiB
21Accepted3ms3696 KiB
22Accepted3ms3768 KiB
23Accepted3ms3772 KiB
24Accepted3ms3844 KiB
25Accepted3ms3852 KiB
26Accepted2ms3856 KiB
27Accepted3ms3944 KiB
28Accepted3ms4080 KiB
29Accepted2ms3980 KiB
30Accepted3ms4056 KiB
31Accepted3ms3792 KiB
subtask420/20
32Accepted3ms2444 KiB
33Accepted3ms2500 KiB
34Accepted3ms2588 KiB
35Accepted3ms2676 KiB
36Accepted3ms2808 KiB
37Accepted3ms3040 KiB
38Accepted3ms3388 KiB
39Accepted3ms3484 KiB
40Accepted3ms3696 KiB
41Accepted3ms3768 KiB
42Accepted3ms3772 KiB
43Accepted3ms3844 KiB
44Accepted3ms3852 KiB
45Accepted2ms3856 KiB
46Accepted3ms3944 KiB
47Accepted3ms4080 KiB
48Accepted2ms3980 KiB
49Accepted3ms4056 KiB
50Accepted3ms3792 KiB
51Accepted3ms4604 KiB
52Accepted3ms4564 KiB
53Accepted3ms4500 KiB
54Accepted3ms4508 KiB
55Accepted3ms4524 KiB
56Accepted3ms4528 KiB
57Accepted3ms4768 KiB
58Accepted3ms4788 KiB
59Accepted3ms4776 KiB
60Accepted3ms5048 KiB
61Accepted3ms5008 KiB
62Accepted3ms5272 KiB
63Accepted3ms5224 KiB
64Accepted3ms5144 KiB
65Accepted3ms5408 KiB
66Accepted3ms5368 KiB
67Accepted3ms5376 KiB
68Accepted3ms5372 KiB
69Accepted3ms5632 KiB
70Accepted3ms5592 KiB
subtask540/40
71Accepted3ms2444 KiB
72Accepted3ms2500 KiB
73Accepted3ms2588 KiB
74Accepted3ms2676 KiB
75Accepted3ms2808 KiB
76Accepted3ms3040 KiB
77Accepted3ms3388 KiB
78Accepted3ms3484 KiB
79Accepted3ms3696 KiB
80Accepted3ms3768 KiB
81Accepted3ms3772 KiB
82Accepted3ms3844 KiB
83Accepted3ms3852 KiB
84Accepted2ms3856 KiB
85Accepted3ms3944 KiB
86Accepted3ms4080 KiB
87Accepted2ms3980 KiB
88Accepted3ms4056 KiB
89Accepted3ms3792 KiB
90Accepted3ms4604 KiB
91Accepted3ms4564 KiB
92Accepted3ms4500 KiB
93Accepted3ms4508 KiB
94Accepted3ms4524 KiB
95Accepted3ms4528 KiB
96Accepted3ms4768 KiB
97Accepted3ms4788 KiB
98Accepted3ms4776 KiB
99Accepted3ms5048 KiB
100Accepted3ms5008 KiB
101Accepted3ms5272 KiB
102Accepted3ms5224 KiB
103Accepted3ms5144 KiB
104Accepted3ms5408 KiB
105Accepted3ms5368 KiB
106Accepted3ms5376 KiB
107Accepted3ms5372 KiB
108Accepted3ms5632 KiB
109Accepted3ms5592 KiB
110Accepted82ms29848 KiB
111Accepted82ms29860 KiB
112Accepted82ms29912 KiB
113Accepted82ms29920 KiB
114Accepted82ms29936 KiB
115Accepted86ms29988 KiB
116Accepted86ms30032 KiB
117Accepted85ms30028 KiB
118Accepted85ms30040 KiB
119Accepted85ms30064 KiB
120Accepted85ms30112 KiB
121Accepted90ms30116 KiB
122Accepted90ms30148 KiB
123Accepted90ms30184 KiB
124Accepted90ms30172 KiB
125Accepted86ms30224 KiB
126Accepted90ms30240 KiB
127Accepted90ms30288 KiB
128Accepted92ms30344 KiB
129Accepted90ms30484 KiB
130Accepted90ms30480 KiB
131Accepted76ms30744 KiB
132Accepted93ms30752 KiB