답안 #1057573

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
1057573 2024-08-13T21:50:48 Z Zicrus 분수 공원 (IOI21_parks) C++17
55 / 100
914 ms 139080 KB
#include <bits/stdc++.h>
#include "parks.h"
using namespace std;
 
typedef long long ll;
 
ll n;
 
vector<ll> uflnk;
vector<ll> sz;
 
ll find(ll a) {
    if (a != uflnk[a]) uflnk[a] = find(uflnk[a]);
    return uflnk[a];
}

bool same(ll a, ll b) {
    return find(a) == find(b);
}
 
bool unite(ll a, ll b) {
    a = find(a); b = find(b);
    if (a == b) return false;
    if (sz[a] > sz[b]) swap(a, b);
    uflnk[a] = b;
    sz[b] += sz[a];
    return true;
}
 
int construct_roads(vector<int> x, vector<int> y) {
    n = x.size();
    vector<pair<pair<ll, ll>, ll>> coords(n);
    for (int i = 0; i < n; i++) coords[i] = {{x[i], y[i]}, i};
    mt19937 mt(8765);
    shuffle(coords.begin(), coords.end(), mt);
    for (int i = 0; i < n; i++) {
        x[i] = coords[i].first.first;
        y[i] = coords[i].first.second;
    }
    uflnk = vector<ll>(n);
    sz = vector<ll>(n, 1);
    for (int i = 0; i < n; i++) uflnk[i] = i;
    unordered_map<ll, ll> pts;
    vector<pair<ll, ll>> edges;
    unordered_set<ll> hasEdge;
    for (int i = 0; i < n; i++) {
        ll hash = ((ll)x[i] << 31) | (ll)y[i];
        pts[hash] = i;
    }
    for (ll i = 0; i < n; i++) {
        ll hashL = (((ll)x[i]-2) << 31) | ((ll)y[i]);
        if (pts.count(hashL)) {
            if (!same(i, pts[hashL])) {
                edges.push_back({i, pts[hashL]});
                unite(i, pts[hashL]);
            }
        }
        ll hashR = (((ll)x[i]+2) << 31) | ((ll)y[i]);
        if (pts.count(hashR)) {
            if (!same(i, pts[hashR])) {
                edges.push_back({i, pts[hashR]});
                unite(i, pts[hashR]);
            }
        }
        ll hashD = (((ll)x[i]) << 31) | ((ll)y[i]-2);
        if (pts.count(hashD)) {
            if (!same(i, pts[hashD])) {
                edges.push_back({i, pts[hashD]});
                unite(i, pts[hashD]);
            }
        }
        ll hashU = (((ll)x[i]) << 31) | ((ll)y[i]+2);
        if (pts.count(hashU)) {
            if (!same(i, pts[hashU])) {
                edges.push_back({i, pts[hashU]});
                unite(i, pts[hashU]);
            }
        }
    }
 
    unordered_map<ll, ll> poss;
    unordered_set<ll> twos;
    unordered_map<ll, ll> lnk, lnkU;
    vector<pair<ll, ll>> edgeHashes(edges.size());
    shuffle(edges.begin(), edges.end(), mt);
    for (int i = 0; i < edges.size(); i++) {
        auto e = edges[i];
        ll midX = (x[e.first] + x[e.second]) / 2;
        ll midY = (y[e.first] + y[e.second]) / 2;
        ll leftX = midX - (midY - y[e.second]);
        ll leftY = midY + (midX - x[e.second]);
        ll rightX = midX + (midY - y[e.second]);
        ll rightY = midY - (midX - x[e.second]);
        ll hashL = (leftX << 31) | leftY;
        ll hashR = (rightX << 31) | rightY;
        if (twos.count(hashL)) twos.erase(hashL);
        poss[hashL]++;
        if (poss[hashL] >= 2) twos.insert(hashL);
        if (twos.count(hashR)) twos.erase(hashR);
        poss[hashR]++;
        if (poss[hashR] >= 2) twos.insert(hashR);
        lnk[hashL] ^= i;
        lnk[hashR] ^= i;
        lnkU[hashL] = i;
        lnkU[hashR] = i;
        edgeHashes[i] = {hashL, hashR};
    }
 
    vector<int> a(edges.size()), b(edges.size());
    queue<ll> q;
    for (auto &e : poss) {
        if (e.second != 1) continue;
        q.push(e.first);
    }
    unordered_set<ll> used;
    while (used.size() < edges.size()) {
        while (!q.empty()) {
            ll hash = q.front(); q.pop();
            if (poss[hash] != 1 || used.count(hash)) continue;
            ll id = lnk[hash];
            a[id] = hash >> 31;
            b[id] = hash & ~(1 << 31);
            used.insert(id);
            if (twos.count(edgeHashes[id].first)) twos.erase(edgeHashes[id].first);
            if (--poss[edgeHashes[id].first] == 1) q.push(edgeHashes[id].first);
            if (poss[edgeHashes[id].first] >= 2) twos.insert(edgeHashes[id].first);
            if (twos.count(edgeHashes[id].second)) twos.erase(edgeHashes[id].second);
            if (--poss[edgeHashes[id].second] == 1) q.push(edgeHashes[id].second);
            if (poss[edgeHashes[id].second] >= 2) twos.insert(edgeHashes[id].second);
            lnk[edgeHashes[id].first] ^= id;
            lnk[edgeHashes[id].second] ^= id;
        }
        if (used.size() < edges.size() && twos.empty()) {
            for (auto &e : poss) {
                if (e.second == 1) q.push(e.first);
                if (e.second >= 2) twos.insert(e.first);
            }
            if (q.empty() && twos.empty()) throw;
        }
        else if (used.size() < edges.size() && !twos.empty()) {
            ll v = *twos.begin();
            twos.erase(v);
            ll i = lnkU[v];
            if (used.count(i)) continue;
            auto e = edges[i];
            ll midX = (x[e.first] + x[e.second]) / 2;
            ll midY = (y[e.first] + y[e.second]) / 2;
            if (abs(midX - (v >> 31)) + abs(midY - (v & ~(1 << 31))) > 1) continue;
            a[i] = v >> 31;
            b[i] = v & ~(1 << 31);
            poss[v] = 0;
            ll otherX = a[i] + 2 * (midX - a[i]);
            ll otherY = b[i] + 2 * (midY - b[i]);
            ll otherHash = (otherX << 31) | otherY;
            if (twos.count(otherHash)) twos.erase(otherHash);
            poss[otherHash]--;
            if (poss[otherHash] >= 2) twos.insert(otherHash);
            lnk[otherHash] ^= i;
            while (poss[otherHash] == 1) {
                ll edge = lnk[otherHash];
                auto e = edges[edge];
                ll midX = (x[e.first] + x[e.second]) / 2;
                ll midY = (y[e.first] + y[e.second]) / 2;
                a[edge] = otherHash >> 31;
                b[edge] = otherHash & ~(1 << 31);
                ll otherX = a[edge] + 2 * (midX - a[edge]);
                ll otherY = b[edge] + 2 * (midY - b[edge]);
                otherHash = (otherX << 31) | otherY;
                if (twos.count(otherHash)) twos.erase(otherHash);
                poss[otherHash]--;
                if (poss[otherHash] >= 2) twos.insert(otherHash);
                lnk[otherHash] ^= edge;
                used.insert(edge);
            }
            used.insert(i);
        }
    }
 
    uflnk = vector<ll>(n);
    sz = vector<ll>(n, 1);
    for (int i = 0; i < n; i++) uflnk[i] = i;
    vector<int> u, v;
    for (int i = 0; i < edges.size(); i++) {
        u.push_back(coords[edges[i].first].second);
        v.push_back(coords[edges[i].second].second);
        unite(edges[i].first, edges[i].second);
    }
 
    if (sz[find(0)] < n) return 0;
 
    build(u, v, a, b);
    return 1;
}

Compilation message

parks.cpp: In function 'int construct_roads(std::vector<int>, std::vector<int>)':
parks.cpp:86:23: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<long long int, long long int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   86 |     for (int i = 0; i < edges.size(); i++) {
      |                     ~~^~~~~~~~~~~~~~
parks.cpp:183:23: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<long long int, long long int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  183 |     for (int i = 0; i < edges.size(); i++) {
      |                     ~~^~~~~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 344 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 0 ms 348 KB Output is correct
4 Correct 0 ms 348 KB Output is correct
5 Correct 0 ms 348 KB Output is correct
6 Correct 0 ms 348 KB Output is correct
7 Correct 0 ms 348 KB Output is correct
8 Correct 0 ms 348 KB Output is correct
9 Correct 197 ms 51204 KB Output is correct
10 Correct 11 ms 5080 KB Output is correct
11 Correct 81 ms 27040 KB Output is correct
12 Correct 17 ms 7524 KB Output is correct
13 Correct 57 ms 22152 KB Output is correct
14 Correct 1 ms 860 KB Output is correct
15 Correct 2 ms 1116 KB Output is correct
16 Correct 201 ms 51144 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 344 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 0 ms 348 KB Output is correct
4 Correct 0 ms 348 KB Output is correct
5 Correct 0 ms 348 KB Output is correct
6 Correct 0 ms 348 KB Output is correct
7 Correct 0 ms 348 KB Output is correct
8 Correct 0 ms 348 KB Output is correct
9 Correct 197 ms 51204 KB Output is correct
10 Correct 11 ms 5080 KB Output is correct
11 Correct 81 ms 27040 KB Output is correct
12 Correct 17 ms 7524 KB Output is correct
13 Correct 57 ms 22152 KB Output is correct
14 Correct 1 ms 860 KB Output is correct
15 Correct 2 ms 1116 KB Output is correct
16 Correct 201 ms 51144 KB Output is correct
17 Correct 0 ms 348 KB Output is correct
18 Correct 0 ms 348 KB Output is correct
19 Correct 0 ms 348 KB Output is correct
20 Correct 0 ms 348 KB Output is correct
21 Correct 0 ms 348 KB Output is correct
22 Correct 0 ms 348 KB Output is correct
23 Correct 485 ms 78660 KB Output is correct
24 Correct 1 ms 344 KB Output is correct
25 Correct 1 ms 860 KB Output is correct
26 Correct 3 ms 1372 KB Output is correct
27 Correct 4 ms 1628 KB Output is correct
28 Correct 123 ms 31036 KB Output is correct
29 Correct 225 ms 45416 KB Output is correct
30 Correct 343 ms 63736 KB Output is correct
31 Correct 488 ms 79144 KB Output is correct
32 Correct 1 ms 348 KB Output is correct
33 Correct 0 ms 348 KB Output is correct
34 Correct 1 ms 348 KB Output is correct
35 Correct 0 ms 348 KB Output is correct
36 Correct 0 ms 348 KB Output is correct
37 Correct 0 ms 348 KB Output is correct
38 Correct 0 ms 348 KB Output is correct
39 Correct 0 ms 348 KB Output is correct
40 Correct 0 ms 344 KB Output is correct
41 Correct 0 ms 348 KB Output is correct
42 Correct 0 ms 348 KB Output is correct
43 Correct 2 ms 1116 KB Output is correct
44 Correct 3 ms 1372 KB Output is correct
45 Correct 178 ms 41580 KB Output is correct
46 Correct 330 ms 62940 KB Output is correct
47 Correct 313 ms 62052 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 344 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 0 ms 348 KB Output is correct
4 Correct 0 ms 348 KB Output is correct
5 Correct 0 ms 348 KB Output is correct
6 Correct 0 ms 348 KB Output is correct
7 Correct 0 ms 348 KB Output is correct
8 Correct 0 ms 348 KB Output is correct
9 Correct 197 ms 51204 KB Output is correct
10 Correct 11 ms 5080 KB Output is correct
11 Correct 81 ms 27040 KB Output is correct
12 Correct 17 ms 7524 KB Output is correct
13 Correct 57 ms 22152 KB Output is correct
14 Correct 1 ms 860 KB Output is correct
15 Correct 2 ms 1116 KB Output is correct
16 Correct 201 ms 51144 KB Output is correct
17 Correct 0 ms 348 KB Output is correct
18 Correct 0 ms 348 KB Output is correct
19 Correct 0 ms 348 KB Output is correct
20 Correct 0 ms 348 KB Output is correct
21 Correct 0 ms 348 KB Output is correct
22 Correct 0 ms 348 KB Output is correct
23 Correct 485 ms 78660 KB Output is correct
24 Correct 1 ms 344 KB Output is correct
25 Correct 1 ms 860 KB Output is correct
26 Correct 3 ms 1372 KB Output is correct
27 Correct 4 ms 1628 KB Output is correct
28 Correct 123 ms 31036 KB Output is correct
29 Correct 225 ms 45416 KB Output is correct
30 Correct 343 ms 63736 KB Output is correct
31 Correct 488 ms 79144 KB Output is correct
32 Correct 1 ms 348 KB Output is correct
33 Correct 0 ms 348 KB Output is correct
34 Correct 1 ms 348 KB Output is correct
35 Correct 0 ms 348 KB Output is correct
36 Correct 0 ms 348 KB Output is correct
37 Correct 0 ms 348 KB Output is correct
38 Correct 0 ms 348 KB Output is correct
39 Correct 0 ms 348 KB Output is correct
40 Correct 0 ms 344 KB Output is correct
41 Correct 0 ms 348 KB Output is correct
42 Correct 0 ms 348 KB Output is correct
43 Correct 2 ms 1116 KB Output is correct
44 Correct 3 ms 1372 KB Output is correct
45 Correct 178 ms 41580 KB Output is correct
46 Correct 330 ms 62940 KB Output is correct
47 Correct 313 ms 62052 KB Output is correct
48 Correct 0 ms 348 KB Output is correct
49 Correct 0 ms 348 KB Output is correct
50 Correct 0 ms 348 KB Output is correct
51 Correct 0 ms 348 KB Output is correct
52 Correct 0 ms 348 KB Output is correct
53 Correct 0 ms 348 KB Output is correct
54 Correct 0 ms 348 KB Output is correct
55 Runtime error 452 ms 139080 KB Execution killed with signal 11
56 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 344 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 0 ms 348 KB Output is correct
4 Correct 0 ms 348 KB Output is correct
5 Correct 0 ms 348 KB Output is correct
6 Correct 0 ms 348 KB Output is correct
7 Correct 0 ms 348 KB Output is correct
8 Correct 0 ms 348 KB Output is correct
9 Correct 197 ms 51204 KB Output is correct
10 Correct 11 ms 5080 KB Output is correct
11 Correct 81 ms 27040 KB Output is correct
12 Correct 17 ms 7524 KB Output is correct
13 Correct 57 ms 22152 KB Output is correct
14 Correct 1 ms 860 KB Output is correct
15 Correct 2 ms 1116 KB Output is correct
16 Correct 201 ms 51144 KB Output is correct
17 Correct 0 ms 348 KB Output is correct
18 Correct 0 ms 436 KB Output is correct
19 Correct 0 ms 348 KB Output is correct
20 Correct 429 ms 78132 KB Output is correct
21 Correct 468 ms 78400 KB Output is correct
22 Correct 449 ms 78708 KB Output is correct
23 Correct 339 ms 79188 KB Output is correct
24 Correct 52 ms 23676 KB Output is correct
25 Correct 387 ms 96792 KB Output is correct
26 Correct 428 ms 96276 KB Output is correct
27 Correct 449 ms 101652 KB Output is correct
28 Correct 470 ms 101648 KB Output is correct
29 Correct 413 ms 101648 KB Output is correct
30 Correct 418 ms 102160 KB Output is correct
31 Correct 0 ms 348 KB Output is correct
32 Correct 22 ms 6000 KB Output is correct
33 Correct 26 ms 12308 KB Output is correct
34 Correct 463 ms 78664 KB Output is correct
35 Correct 13 ms 3976 KB Output is correct
36 Correct 71 ms 18768 KB Output is correct
37 Correct 182 ms 37228 KB Output is correct
38 Correct 128 ms 32368 KB Output is correct
39 Correct 187 ms 44044 KB Output is correct
40 Correct 258 ms 54316 KB Output is correct
41 Correct 353 ms 67924 KB Output is correct
42 Correct 460 ms 80448 KB Output is correct
43 Correct 0 ms 344 KB Output is correct
44 Correct 0 ms 344 KB Output is correct
45 Correct 0 ms 348 KB Output is correct
46 Correct 0 ms 344 KB Output is correct
47 Correct 0 ms 348 KB Output is correct
48 Correct 0 ms 348 KB Output is correct
49 Correct 0 ms 348 KB Output is correct
50 Correct 0 ms 344 KB Output is correct
51 Correct 0 ms 348 KB Output is correct
52 Correct 1 ms 344 KB Output is correct
53 Correct 0 ms 344 KB Output is correct
54 Correct 2 ms 1116 KB Output is correct
55 Correct 3 ms 1372 KB Output is correct
56 Correct 180 ms 41612 KB Output is correct
57 Correct 326 ms 61920 KB Output is correct
58 Correct 326 ms 62068 KB Output is correct
59 Correct 1 ms 348 KB Output is correct
60 Correct 0 ms 348 KB Output is correct
61 Correct 0 ms 348 KB Output is correct
62 Correct 538 ms 102736 KB Output is correct
63 Correct 532 ms 102092 KB Output is correct
64 Correct 562 ms 102296 KB Output is correct
65 Correct 4 ms 1884 KB Output is correct
66 Correct 8 ms 3520 KB Output is correct
67 Correct 182 ms 41132 KB Output is correct
68 Correct 336 ms 63128 KB Output is correct
69 Correct 520 ms 82784 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 344 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 0 ms 348 KB Output is correct
4 Correct 0 ms 348 KB Output is correct
5 Correct 0 ms 348 KB Output is correct
6 Correct 0 ms 348 KB Output is correct
7 Correct 0 ms 348 KB Output is correct
8 Correct 0 ms 348 KB Output is correct
9 Correct 197 ms 51204 KB Output is correct
10 Correct 11 ms 5080 KB Output is correct
11 Correct 81 ms 27040 KB Output is correct
12 Correct 17 ms 7524 KB Output is correct
13 Correct 57 ms 22152 KB Output is correct
14 Correct 1 ms 860 KB Output is correct
15 Correct 2 ms 1116 KB Output is correct
16 Correct 201 ms 51144 KB Output is correct
17 Correct 607 ms 102604 KB Output is correct
18 Correct 535 ms 102540 KB Output is correct
19 Correct 477 ms 77880 KB Output is correct
20 Correct 412 ms 78460 KB Output is correct
21 Correct 383 ms 79864 KB Output is correct
22 Correct 1 ms 344 KB Output is correct
23 Correct 44 ms 12688 KB Output is correct
24 Correct 25 ms 7892 KB Output is correct
25 Correct 104 ms 27536 KB Output is correct
26 Correct 206 ms 44156 KB Output is correct
27 Correct 171 ms 40736 KB Output is correct
28 Correct 223 ms 49588 KB Output is correct
29 Correct 364 ms 61844 KB Output is correct
30 Correct 491 ms 73636 KB Output is correct
31 Correct 559 ms 80988 KB Output is correct
32 Correct 790 ms 84076 KB Output is correct
33 Correct 900 ms 102188 KB Output is correct
34 Correct 8 ms 2140 KB Output is correct
35 Correct 14 ms 3784 KB Output is correct
36 Correct 302 ms 41120 KB Output is correct
37 Correct 530 ms 62920 KB Output is correct
38 Correct 914 ms 82128 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 344 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 0 ms 348 KB Output is correct
4 Correct 0 ms 348 KB Output is correct
5 Correct 0 ms 348 KB Output is correct
6 Correct 0 ms 348 KB Output is correct
7 Correct 0 ms 348 KB Output is correct
8 Correct 0 ms 348 KB Output is correct
9 Correct 197 ms 51204 KB Output is correct
10 Correct 11 ms 5080 KB Output is correct
11 Correct 81 ms 27040 KB Output is correct
12 Correct 17 ms 7524 KB Output is correct
13 Correct 57 ms 22152 KB Output is correct
14 Correct 1 ms 860 KB Output is correct
15 Correct 2 ms 1116 KB Output is correct
16 Correct 201 ms 51144 KB Output is correct
17 Correct 0 ms 348 KB Output is correct
18 Correct 0 ms 348 KB Output is correct
19 Correct 0 ms 348 KB Output is correct
20 Correct 0 ms 348 KB Output is correct
21 Correct 0 ms 348 KB Output is correct
22 Correct 0 ms 348 KB Output is correct
23 Correct 485 ms 78660 KB Output is correct
24 Correct 1 ms 344 KB Output is correct
25 Correct 1 ms 860 KB Output is correct
26 Correct 3 ms 1372 KB Output is correct
27 Correct 4 ms 1628 KB Output is correct
28 Correct 123 ms 31036 KB Output is correct
29 Correct 225 ms 45416 KB Output is correct
30 Correct 343 ms 63736 KB Output is correct
31 Correct 488 ms 79144 KB Output is correct
32 Correct 1 ms 348 KB Output is correct
33 Correct 0 ms 348 KB Output is correct
34 Correct 1 ms 348 KB Output is correct
35 Correct 0 ms 348 KB Output is correct
36 Correct 0 ms 348 KB Output is correct
37 Correct 0 ms 348 KB Output is correct
38 Correct 0 ms 348 KB Output is correct
39 Correct 0 ms 348 KB Output is correct
40 Correct 0 ms 344 KB Output is correct
41 Correct 0 ms 348 KB Output is correct
42 Correct 0 ms 348 KB Output is correct
43 Correct 2 ms 1116 KB Output is correct
44 Correct 3 ms 1372 KB Output is correct
45 Correct 178 ms 41580 KB Output is correct
46 Correct 330 ms 62940 KB Output is correct
47 Correct 313 ms 62052 KB Output is correct
48 Correct 0 ms 348 KB Output is correct
49 Correct 0 ms 348 KB Output is correct
50 Correct 0 ms 348 KB Output is correct
51 Correct 0 ms 348 KB Output is correct
52 Correct 0 ms 348 KB Output is correct
53 Correct 0 ms 348 KB Output is correct
54 Correct 0 ms 348 KB Output is correct
55 Runtime error 452 ms 139080 KB Execution killed with signal 11
56 Halted 0 ms 0 KB -