답안 #985565

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
985565 2024-05-18T08:11:39 Z maomao90 Spy 3 (JOI24_spy3) C++17
100 / 100
53 ms 5892 KB
// Hallelujah, praise the one who set me free
// Hallelujah, death has lost its grip on me
// You have broken every chain, There's salvation in your name
// Jesus Christ, my living hope
#include <bits/stdc++.h> 
#include "Aoi.h"
using namespace std;

#define REP(i, s, e) for (int i = (s); i < (e); i++)
#define RREP(i, s, e) for (int i = (s); i >= (e); i--)
template <class T>
inline bool mnto(T& a, T b) {return a > b ? a = b, 1 : 0;}
template <class T>
inline bool mxto(T& a, T b) {return a < b ? a = b, 1: 0;}

typedef unsigned long long ull;
typedef long long ll;
typedef long double ld;
#define FI first
#define SE second
typedef pair<int, int> ii;
typedef pair<ll, ll> pll;
typedef tuple<int, int, int> iii;
#define ALL(_a) _a.begin(), _a.end()
#define SZ(_a) (int) _a.size()
#define pb push_back
typedef vector<int> vi;
typedef vector<ll> vll;
typedef vector<ii> vii;
typedef vector<iii> viii;

#ifndef DEBUG
#define cerr if (0) cerr
#endif

const int INF = 1000000005;
const ll LINF = 1000000000000000005ll;
const int MAXN = 10005;
const int MAXM = 20005;
const int MAXK = 305;
const int LOGK = 9;
const int MAXQ = 16;
const int LOGQ = 4;

namespace {
    int n, m, q, k;
    ll c[MAXM];
    vii adj[MAXN];
    int subt[MAXN], tid[MAXN], bid[MAXM];
    ii p[MAXN];
    vii tree[MAXN];

    ll d[MAXN];
    priority_queue<pll, vector<pll>, greater<pll>> pq;
    void dijkstra(int s) {
        REP (i, 0, n) {
            d[i] = LINF;
        }
        d[s] = 0;
        pq.push({0, s});
        while (!pq.empty()) {
            auto [ud, u] = pq.top(); pq.pop();
            if (ud != d[u]) {
                continue;
            }
            for (auto [v, i] : adj[u]) {
                if (mnto(d[v], d[u] + c[i])) {
                    p[v] = {u, i};
                    pq.push({d[v], v});
                }
            }
        }
    }

    void root(int u) {
        for (auto [v, i] : tree[u]) {
            root(v);
            subt[u] += subt[v];
        }
    }
    vi badj[MAXK];
    vi bt[MAXK];
    vi stk;
    void dfs(int u) {
        if (tid[u] < q) {
            bt[stk.back()].pb(tid[u]);
        }
        for (auto [v, i] : tree[u]) {
            if (!subt[v]) {
                continue;
            }
            if (bid[i] < k) {
                badj[stk.back()].pb(bid[i]);
                stk.pb(bid[i]);
            }
            dfs(v);
            if (bid[i] < k) {
                stk.pop_back();
            }
        }
    }
    int bmnt[MAXK];
    void broot(int u) {
        bmnt[u] = INF;
        if (!bt[u].empty()) {
            sort(ALL(bt[u]));
            bmnt[u] = bt[u][0];
        }
        for (int v : badj[u]) {
            broot(v);
            mnto(bmnt[u], bmnt[v]);
        }
        sort(ALL(badj[u]), [&] (int l, int r) {
                return bmnt[l] < bmnt[r];
                });
    }
    int lptr;
    int tlabel[MAXQ + 5], blabel[MAXK];
    int ids[MAXQ + 5];
    int bdfs(int u) {
        int ptr = 0;
        int nxt = -1;
        blabel[u] = lptr;
        for (int v : badj[u]) {
            while (ptr < SZ(bt[u]) && bt[u][ptr] < bmnt[v]) {
                tlabel[nxt] = u;
                ids[lptr] = bt[u][ptr];
                nxt = bt[u][ptr++];
                lptr++;
            }
            tlabel[nxt] = u;
            nxt = bdfs(v);
        }
        while (ptr < SZ(bt[u])) {
            tlabel[nxt] = u;
            ids[lptr] = bt[u][ptr];
            nxt = bt[u][ptr++];
            lptr++;
        }
        return nxt;
    }

    vector<int> encode(int b, vector<int> k, vector<vector<int>> a) {
        assert(k.size() == a.size());
        int n = k.size();
        ld bits = 0;
        for (int i = 0; i < n; i++) {
            bits += a[i].size() * (log(k[i]) / log(b));
        }
        int m = ceil(bits);
        vector<int> x(m);
        for (int p = 0; p < m; p++) {
            int rem = 0;
            for (int i = n - 1; i >= 0; i--) {
                for (int j = a[i].size() - 1; j >= 0; j--) {
                    rem = rem * k[i] + a[i][j];
                    a[i][j] = rem / b;
                    rem %= b;
                }
            }
            x[p] = rem;
        }
        return x;
    }
}

string aoi(int N, int M, int Q, int K, vi A, vi B, vll C, vi T, vi X) {
    n = N; m = M; q = Q; k = K;
    REP (i, 0, m) {
        adj[A[i]].pb({B[i], i});
        adj[B[i]].pb({A[i], i});
        c[i] = C[i];
    }
    REP (i, 0, n) {
        tid[i] = INF;
    }
    REP (i, 0, q) {
        subt[T[i]] = 1;
        tid[T[i]] = i;
    }
    REP (i, 0, m) {
        bid[i] = INF;
    }
    REP (i, 0, k) {
        bid[X[i]] = i;
        blabel[i] = q;
    }
    dijkstra(0);
    REP (i, 1, n) {
        tree[p[i].FI].pb({i, p[i].SE});
    }
    root(0);
    stk.pb(k);
    dfs(0);
    broot(k);
    tlabel[bdfs(k)] = k;
    string s;
    assert(lptr <= q);
    vi tmp = encode(2, vi({q + 1, k + 1, q}), vector<vi>({vi(blabel, blabel + k), vi(tlabel, tlabel + q), vi(ids, ids + q)}));
    for (int i : tmp) {
        char c = i + '0';
        s += c;
    }
    return s;
}
// Hallelujah, praise the one who set me free
// Hallelujah, death has lost its grip on me
// You have broken every chain, There's salvation in your name
// Jesus Christ, my living hope
#include <bits/stdc++.h> 
#include "Bitaro.h"
using namespace std;

#define REP(i, s, e) for (int i = (s); i < (e); i++)
#define RREP(i, s, e) for (int i = (s); i >= (e); i--)
template <class T>
inline bool mnto(T& a, T b) {return a > b ? a = b, 1 : 0;}
template <class T>
inline bool mxto(T& a, T b) {return a < b ? a = b, 1: 0;}

typedef unsigned long long ull;
typedef long long ll;
typedef long double ld;
#define FI first
#define SE second
typedef pair<int, int> ii;
typedef pair<ll, ll> pll;
typedef tuple<int, int, int> iii;
#define ALL(_a) _a.begin(), _a.end()
#define SZ(_a) (int) _a.size()
#define pb push_back
typedef vector<int> vi;
typedef vector<ll> vll;
typedef vector<ii> vii;
typedef vector<iii> viii;

#ifndef DEBUG
#define cerr if (0) cerr
#endif

const int INF = 1000000005;
const ll LINF = 1000000000000000005ll;
const int MAXN = 10005;
const int MAXM = 20005;
const int MAXK = 305;
const int LOGK = 9;
const int MAXQ = 16;
const int LOGQ = 4;

namespace {
    int n, m, q, k;
    string s;
    ll c[MAXM];
    vii adj[MAXN];
    int ids[MAXQ + 5];
    int blabel[MAXK], bid[MAXM];
    bool bvis[MAXK];
    int bl[MAXK];
    int tlabel[MAXQ + 5];
    bool tdone[MAXQ + 5];

    int qptr;
    ii p[MAXN], tp[MAXN];
    ll d[MAXN];
    priority_queue<pll, vector<pll>, greater<pll>> pq;

    vector<vector<int>> decode(int b, vector<int> k, vector<int> l, vector<int> x) {
        assert(k.size() == l.size());
        int n = k.size(), m = x.size();
        vector<vector<int>> a(n);
        for (int i = 0; i < n; i++) {
            a[i].resize(l[i]);
            for (int j = 0; j < l[i]; j++) {
                int rem = 0;
                for (int p = m - 1; p >= 0; p--) {
                    rem = rem * b + x[p];
                    x[p] = rem / k[i];
                    rem %= k[i];
                }
                a[i][j] = rem;
            }
        }
        return a;
    }
}

void bitaro(int N, int M, int Q, int K, vi A, vi B, vll C, vi T, vi X, string S) {
    s = S;
    n = N; m = M; q = Q; k = K;
    REP (i, 0, m) {
        adj[A[i]].pb({B[i], i});
        adj[B[i]].pb({A[i], i});
        c[i] = C[i];
    }
    REP (i, 0, k) {
        c[X[i]] = -1;
        bid[X[i]] = i;
    }
    vi vs;
    for (char c : s) {
        vs.pb(c - '0');
    }
    vector<vi> tmp = decode(2, vi({q + 1, k + 1, q}), vi({k, q, q}), vs);
    REP (i, 0, k) {
        blabel[i] = tmp[0][i];
    }
    REP (i, 0, q) {
        tlabel[i] = tmp[1][i];
    }
    REP (i, 0, q) {
        ids[i] = tmp[2][i];
    }
    REP (i, 0, n) {
        p[i] = {-1, -1};
    }
    vi bstk;
    bstk.pb(k);
    bvis[k] = 1;
    REP (l, 0, q) {
        int bu = bstk.back();
        int u = 0;
        if (bu < k) {
            u = p[A[X[bu]]].SE == X[bu] ? A[X[bu]] : B[X[bu]];
        }
        while (!pq.empty()) {
            pq.pop();
        }
        REP (i, 0, n) {
            d[i] = LINF;
        }
        d[u] = 0;
        pq.push({0, u});
        while (!pq.empty()) {
            auto [ud, u] = pq.top(); pq.pop();
            if (ud != d[u]) {
                continue;
            }
            for (auto [v, i] : adj[u]) {
                if (p[v].FI != -1 && p[v].FI != u) {
                    continue;
                }
                if (p[u].FI != -1 && p[u].FI == v) {
                    continue;
                }
                ll w = c[i];
                if (c[i] == -1) {
                    if (blabel[bid[i]] != l) {
                        continue;
                    }
                    w = 1;
                }
                if (mnto(d[v], ud + w)) {
                    if (c[i] == -1) {
                        bvis[bid[i]] = 1;
                        bl[bid[i]] = bl[bstk.back()] + 1;
                        bstk.pb(bid[i]);
                    }
                    tp[v] = {u, i};
                    pq.push({d[v], v});
                }
            }
        }
        int tid = ids[l];
        int tu = T[tid];
        while (tu != u) {
            p[tu] = tp[tu];
            tu = tp[tu].FI;
        }
        while (!bstk.empty() && bstk.back() != tlabel[tid]) {
            bvis[bstk.back()] = 0;
            bstk.pop_back();
        }
        assert(!bstk.empty());
    }
    REP (i, 0, q) {
        int u = T[i];
        vi e;
        while (u) {
            e.pb(p[u].SE);
            u = p[u].FI;
        }
        reverse(ALL(e));
        answer(e);
    }
}

Compilation message

Bitaro.cpp:58:9: warning: '{anonymous}::qptr' defined but not used [-Wunused-variable]
   58 |     int qptr;
      |         ^~~~
Bitaro.cpp:56:10: warning: '{anonymous}::tdone' defined but not used [-Wunused-variable]
   56 |     bool tdone[MAXQ + 5];
      |          ^~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 20 ms 4160 KB Output is correct
2 Correct 1 ms 1548 KB Output is correct
3 Correct 34 ms 5172 KB Output is correct
4 Correct 28 ms 5444 KB Output is correct
5 Correct 45 ms 5028 KB Output is correct
6 Correct 31 ms 5008 KB Output is correct
7 Correct 37 ms 4820 KB Output is correct
8 Correct 36 ms 5284 KB Output is correct
9 Correct 27 ms 5300 KB Output is correct
10 Correct 22 ms 5092 KB Output is correct
11 Correct 37 ms 5032 KB Output is correct
12 Correct 27 ms 4924 KB Output is correct
13 Correct 41 ms 5024 KB Output is correct
14 Correct 34 ms 5040 KB Output is correct
15 Correct 22 ms 5156 KB Output is correct
16 Correct 16 ms 5144 KB Output is correct
17 Correct 32 ms 5816 KB Output is correct
18 Correct 32 ms 5892 KB Output is correct
19 Correct 45 ms 5664 KB Output is correct
20 Correct 34 ms 5580 KB Output is correct
21 Correct 42 ms 5632 KB Output is correct
22 Correct 39 ms 5584 KB Output is correct
23 Correct 31 ms 5580 KB Output is correct
24 Correct 38 ms 5640 KB Output is correct
25 Correct 27 ms 5300 KB Output is correct
26 Correct 28 ms 5452 KB Output is correct
27 Correct 2 ms 1548 KB Output is correct
28 Correct 26 ms 5072 KB Output is correct
29 Correct 19 ms 4360 KB Output is correct
30 Correct 23 ms 5352 KB Output is correct
31 Correct 28 ms 5052 KB Output is correct
32 Correct 41 ms 5144 KB Output is correct
33 Correct 41 ms 4760 KB Output is correct
34 Correct 39 ms 5356 KB Output is correct
35 Correct 20 ms 5300 KB Output is correct
36 Correct 25 ms 5320 KB Output is correct
37 Correct 13 ms 4072 KB Output is correct
38 Correct 23 ms 5256 KB Output is correct
39 Correct 19 ms 4972 KB Output is correct
40 Correct 10 ms 3992 KB Output is correct
41 Correct 53 ms 5044 KB Output is correct
42 Correct 35 ms 5604 KB Output is correct
43 Correct 51 ms 5732 KB Output is correct
44 Correct 20 ms 5448 KB Output is correct
45 Correct 13 ms 4112 KB Output is correct
46 Correct 19 ms 4280 KB Output is correct
47 Correct 13 ms 4372 KB Output is correct
48 Correct 1 ms 1552 KB Output is correct
49 Correct 2 ms 1548 KB Output is correct
50 Correct 16 ms 4092 KB Output is correct
51 Correct 7 ms 1548 KB Output is correct
52 Correct 3 ms 1552 KB Output is correct
53 Correct 20 ms 4096 KB Output is correct
54 Correct 17 ms 3504 KB Output is correct
55 Correct 32 ms 4024 KB Output is correct
56 Correct 30 ms 4936 KB Output is correct
57 Correct 40 ms 5120 KB Output is correct
58 Correct 41 ms 4536 KB Output is correct
59 Correct 47 ms 5308 KB Output is correct
60 Correct 40 ms 5048 KB Output is correct
61 Correct 48 ms 5300 KB Output is correct
62 Correct 47 ms 5044 KB Output is correct
63 Correct 48 ms 5044 KB Output is correct
64 Correct 16 ms 5044 KB Output is correct
65 Correct 27 ms 4400 KB Output is correct
66 Correct 26 ms 5608 KB Output is correct
67 Correct 28 ms 4492 KB Output is correct
68 Correct 32 ms 5580 KB Output is correct
69 Correct 1 ms 1548 KB Output is correct
70 Correct 1 ms 1548 KB Output is correct
71 Correct 3 ms 1548 KB Output is correct
72 Correct 13 ms 3340 KB Output is correct
73 Correct 27 ms 3764 KB Output is correct
74 Correct 28 ms 3856 KB Output is correct
75 Correct 11 ms 3856 KB Output is correct
76 Correct 1 ms 1552 KB Output is correct
77 Correct 31 ms 4636 KB Output is correct
78 Correct 30 ms 4788 KB Output is correct
79 Correct 40 ms 4664 KB Output is correct
80 Correct 1 ms 1548 KB Output is correct
81 Correct 39 ms 5028 KB Output is correct
82 Correct 31 ms 5044 KB Output is correct
83 Correct 42 ms 4788 KB Output is correct