69662023-12-22 00:10:48TuruTamasPac-Mancpp17Accepted 100/1001.072s80404 KiB
#include <bits/stdc++.h>
using namespace std;

#ifdef DEBUG
ifstream in_file("minta/be1.txt");
#define input in_file
#define INTHENAMEOFGOD
#else
#define input cin
#define INTHENAMEOFGOD \
    ios::sync_with_stdio(0); \
    cin.tie(0); \
    cout.tie(0);
#endif
typedef long long ll;
typedef vector<ll> vi;
typedef vector<vi> vvi;
typedef vector<bool> vb;
typedef pair<ll, ll> pii;
typedef array<ll, 3> pt;

ll N, x;
vector<pt> ps, points, sp;
set<pt> l;
map<pt, bool> m;
set<int> ax;

void dfs(pt x) {
    m[x] = true;
    for (int axis : ax) {
        pt next = x;
        next[axis]++;
        if (l.count(next) && !m[next]) {
            dfs(next);
        }
        next[axis] -= 2;
        if (l.count(next) && !m[next]) {
            dfs(next);
        }
    }
}

void validate_layer(int i) {
    ax = {0, 1, 2};
    ax.erase(i);
    m.clear();
    dfs(*l.begin());
    for (pt p : l) {
        if (!m[p]) {
            cout << "NO\n";
            exit(0);
        }
    }

    sp.clear();
    for (pt p : l) {
        sp.push_back(p);
    }

    int a = *ax.begin();
    int b = *++ax.begin();
    for (int i = 0; i < 2; i++) {
        sort(sp.begin(), sp.end(), [&](pt l, pt r) {
            if (l[a] == r[a])
                return l[b] < r[b];
            return l[a] < r[a];
        });

        ll row = -1;
        pt last;
        for (pt p : sp) {
            if (p[a] != row)
                row = p[a];
            else if (p[b] != last[b]+1) {
                cout << "NO\n";
                exit(0);
            }
            last = p;
        }

        int c = a;
        a = b;
        b = c;
    }
}

void dfs3d(pt x) {
    m[x] = true;
    for (int axis : ax) {
        pt next = x;
        next[axis]++;
        if (l.count(next) && !m[next])
            dfs3d(next);
        next[axis] -= 2;
        if (l.count(next) && !m[next])
            dfs3d(next);
    }
}

int main() {
    INTHENAMEOFGOD
    input >> N;
    points.resize(N);
    ps.resize(N);
    for (ll n = 0; n < N; n++) {
        input >> x;
        points[n][0] = x;
    }
    for (ll n = 0; n < N; n++) {
        input >> x;
        points[n][1] = x;
    }
    for (ll n = 0; n < N; n++){
        input >> x;
        points[n][2] = x;
    }
    for (pt p : points)
        l.insert(p);
    ax = {0, 1, 2};
    dfs3d(points[0]);
    for (pt p : points) {
        if (!m[p]) {
            cout << "NO\n";
            exit(0);
        }
    }
    l.clear();
    
    for (int i = 0; i < 3; i++) {
        copy_n(points.begin(), N, ps.begin());
        sort(ps.begin(), ps.end(), [&](pt a, pt b) {
            return a[i] < b[i];
        });
        ll layer = ps[0][i];
        l.clear();
        for (pt p : ps) {
            if (p[i] != layer) {
                validate_layer(i);
                layer = p[i];
                l.clear();
            }
            l.insert(p);
        }
        validate_layer(i);
    }
    cout << "YES\n";
}
SubtaskSumTestVerdictTimeMemory
subtask10/0
1Accepted3ms1952 KiB
2Accepted3ms2152 KiB
3Accepted3ms2372 KiB
subtask218/18
4Accepted3ms2460 KiB
5Accepted3ms2716 KiB
6Accepted3ms2832 KiB
7Accepted3ms3040 KiB
8Accepted3ms3128 KiB
9Accepted3ms3216 KiB
10Accepted3ms3220 KiB
11Accepted3ms3288 KiB
12Accepted3ms3216 KiB
13Accepted3ms3480 KiB
subtask319/19
14Accepted7ms4780 KiB
15Accepted3ms3456 KiB
16Accepted18ms6312 KiB
17Accepted35ms8084 KiB
18Accepted61ms8060 KiB
19Accepted61ms7936 KiB
20Accepted3ms3664 KiB
21Accepted16ms5900 KiB
22Accepted27ms8060 KiB
23Accepted28ms8076 KiB
24Accepted46ms7940 KiB
25Accepted26ms7944 KiB
26Accepted3ms3752 KiB
subtask424/24
27Accepted79ms25692 KiB
28Accepted10ms5380 KiB
29Accepted902ms69172 KiB
30Accepted945ms73748 KiB
31Accepted934ms69092 KiB
32Accepted930ms69264 KiB
33Accepted7ms4788 KiB
34Accepted507ms58452 KiB
35Accepted925ms65600 KiB
36Accepted620ms63544 KiB
37Accepted938ms63708 KiB
38Accepted356ms66536 KiB
39Accepted6ms5268 KiB
subtask522/22
40Accepted83ms26364 KiB
41Accepted4ms5108 KiB
42Accepted111ms22048 KiB
43Accepted486ms67532 KiB
44Accepted1.039s66496 KiB
45Accepted1.072s67504 KiB
46Accepted3ms4904 KiB
47Accepted3ms4908 KiB
48Accepted75ms16312 KiB
49Accepted501ms56020 KiB
50Accepted485ms65632 KiB
51Accepted758ms67460 KiB
52Accepted409ms68252 KiB
53Accepted3ms5212 KiB
subtask617/17
54Accepted85ms26900 KiB
55Accepted550ms70380 KiB
56Accepted451ms62872 KiB
57Accepted550ms63912 KiB
58Accepted723ms67000 KiB
59Accepted1.072s68296 KiB
60Accepted344ms71048 KiB
61Accepted456ms59492 KiB
62Accepted523ms68220 KiB
63Accepted713ms68580 KiB
64Accepted1.072s67344 KiB
65Accepted423ms67820 KiB
66Accepted541ms80404 KiB