답안 #924079

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
924079 2024-02-08T11:41:16 Z Perl32 Railway (BOI17_railway) C++14
100 / 100
101 ms 22724 KB
//I wrote this code 4 u <3
#include <bits/stdc++.h>

using namespace std;
using ll = long long;

#ifdef LOCAL
#include "algo/debug.h"
#else
#define debug(...) 42
#endif

struct HLD {
    vector<int> par, siz, head, tin;
    vector<vector<int>> g;

    int dfs1(int u) {
        for (int &to : g[u]) {
            if (to == par[u]) {
                continue;
            }
            par[to] = u;
            siz[u] += dfs1(to);
            if (siz[to] > siz[g[u][0]] || g[u][0] == par[u]) swap(to, g[u][0]);
        }
        return siz[u];
    }

    void dfs2(int u, int &timer) {
        tin[u] = timer++;
        for (int to : g[u]) {
            if (to == par[u]) {
                continue;
            }
            head[to] = (timer == tin[u] + 1 ? head[u] : to);
            dfs2(to, timer);
        }
    }

    HLD(vector<vector<int>> &g, int r = 0) : par(g.size(), -1), siz(g.size(), 1), head(g.size(), r), tin(g.size()), g(g) {
        dfs1(r);
        int timer = 0;
        dfs2(r, timer);
    }

    vector<pair<int, int>> path(int a, int b) {
        vector<pair<int, int>> res;
        for (;; b = par[head[b]]) {
            if (tin[b] < tin[a]) swap(a, b);
            if (tin[head[b]] <= tin[a]) {
                res.emplace_back(tin[a] + 1, tin[b] + 1);
                return res;
            }
            res.emplace_back(tin[head[b]], tin[b] + 1);
        }
    }

    int lca(int a, int b) {
        for (;; b = par[head[b]]) {
            if (tin[b] < tin[a]) swap(a, b);
            if (tin[head[b]] <= tin[a]) return a;
        }
    }
};

template<typename T>
class Fenwick { //0-indexed, [l, r)
 public:
    vector<T> t;
    int n;

    Fenwick() = default;
    explicit Fenwick(int n) : t(n + 1), n(n) {}

    T get(int x) {
        T ans = 0;
        for (x; x > 0; x -= x & -x) {
            ans += t[x];
        }
        return ans;
    }

    T get(int l, int r) {
        return get(r) - get(l);
    }

    void upd(int x, T v) {
        for (++x; x <= n; x += x & -x) {
            t[x] += v;
        }
    }

    int kth(T k) {
        int x = 0;
        for (int i = 1 << __lg(n); i > 0; i >>= 1) {
            if (x + i <= n && t[x + i] < k) {
                k -= t[x += i];
            }
        }
        return x;
    }
};

signed main(int32_t argc, char *argv[]) {
    ios_base::sync_with_stdio(false);
    cin.tie(nullptr);

    int n, m, k;
    cin >> n >> m >> k;
    vector<vector<int>> g(n);
    vector<pair<int, int>> e;
    for (int i = 1; i < n; ++i) {
        int u, v;
        cin >> u >> v;
        --u, --v;
        e.emplace_back(u, v);
        g[u].push_back(v);
        g[v].push_back(u);
    }
    HLD tr(g);
    vector<int> par(n);
    for (int i = 0; i < n - 1; ++i) {
        auto [u, v] = e[i];
        if (tr.tin[u] > tr.tin[v]) swap(u, v);
        par[tr.tin[v]] = i;
    }
    Fenwick<int> bit(n + 1);
    for (int i = 0; i < m; ++i) {
        int K;
        cin >> K;
        vector<int> v(K);
        for (int j = 0; j < K; ++j) {
            cin >> v[j];
            --v[j];
        }
        int lca = v[0];
        for (int j = 1; j < K; ++j) {
            lca = tr.lca(lca, v[j]);
        }
        vector<pair<int, int>> ev;
        for (auto x : v) {
            for (auto [lx, rx] : tr.path(lca, x)) {
                if (lx == rx) continue;
                ev.emplace_back(lx, -1);
                ev.emplace_back(rx, 1);
            }
        }
        sort(ev.begin(), ev.end());
        vector<pair<int, int>> add;
        int last = 0, c = 0;
        for (auto [x, t] : ev) {
            if (t == 1 && c == 1) {
                bit.upd(last, 1);
                bit.upd(x, -1);
            }
            c -= t;
            if (t == -1 && c == 1) {
                last = x;
            }
        }
    }
    vector<int> ans;
    for (int i = 0; i < n; ++i) {
        if (bit.get(i + 1) >= k) {
            ans.push_back(par[i]);
        }
    }
    cout << ans.size() << '\n';
    sort(ans.begin(), ans.end());
    for (auto i : ans) {
        cout << i + 1 << ' ';
    }
}

/*

 */

Compilation message

railway.cpp: In function 'int main(int32_t, char**)':
railway.cpp:123:14: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
  123 |         auto [u, v] = e[i];
      |              ^
railway.cpp:142:23: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
  142 |             for (auto [lx, rx] : tr.path(lca, x)) {
      |                       ^
railway.cpp:151:19: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
  151 |         for (auto [x, t] : ev) {
      |                   ^
railway.cpp: In instantiation of 'T Fenwick<T>::get(int) [with T = int]':
railway.cpp:164:26:   required from here
railway.cpp:77:14: warning: statement has no effect [-Wunused-value]
   77 |         for (x; x > 0; x -= x & -x) {
      |              ^
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 344 KB Output is correct
2 Correct 4 ms 1884 KB Output is correct
3 Correct 4 ms 1884 KB Output is correct
4 Correct 0 ms 348 KB Output is correct
5 Correct 0 ms 348 KB Output is correct
6 Correct 4 ms 2136 KB Output is correct
7 Correct 4 ms 2140 KB Output is correct
8 Correct 3 ms 1884 KB Output is correct
9 Correct 4 ms 2392 KB Output is correct
10 Correct 0 ms 348 KB Output is correct
11 Correct 0 ms 456 KB Output is correct
12 Correct 0 ms 348 KB Output is correct
13 Correct 0 ms 348 KB Output is correct
14 Correct 0 ms 420 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 344 KB Output is correct
2 Correct 4 ms 1884 KB Output is correct
3 Correct 4 ms 1884 KB Output is correct
4 Correct 0 ms 348 KB Output is correct
5 Correct 0 ms 348 KB Output is correct
6 Correct 4 ms 2136 KB Output is correct
7 Correct 4 ms 2140 KB Output is correct
8 Correct 3 ms 1884 KB Output is correct
9 Correct 4 ms 2392 KB Output is correct
10 Correct 0 ms 348 KB Output is correct
11 Correct 0 ms 456 KB Output is correct
12 Correct 0 ms 348 KB Output is correct
13 Correct 0 ms 348 KB Output is correct
14 Correct 0 ms 420 KB Output is correct
15 Correct 41 ms 2140 KB Output is correct
16 Correct 43 ms 2384 KB Output is correct
17 Correct 47 ms 2292 KB Output is correct
18 Correct 4 ms 2256 KB Output is correct
19 Correct 4 ms 1884 KB Output is correct
20 Correct 18 ms 2396 KB Output is correct
21 Correct 27 ms 2392 KB Output is correct
22 Correct 0 ms 348 KB Output is correct
23 Correct 5 ms 1884 KB Output is correct
24 Correct 4 ms 1884 KB Output is correct
25 Correct 0 ms 348 KB Output is correct
26 Correct 1 ms 348 KB Output is correct
27 Correct 4 ms 2140 KB Output is correct
28 Correct 4 ms 2140 KB Output is correct
29 Correct 3 ms 1884 KB Output is correct
30 Correct 3 ms 1884 KB Output is correct
31 Correct 1 ms 348 KB Output is correct
32 Correct 0 ms 348 KB Output is correct
33 Correct 0 ms 456 KB Output is correct
34 Correct 0 ms 348 KB Output is correct
35 Correct 0 ms 348 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 52 ms 22224 KB Output is correct
2 Correct 0 ms 600 KB Output is correct
3 Correct 52 ms 21636 KB Output is correct
4 Correct 48 ms 20936 KB Output is correct
5 Correct 50 ms 22212 KB Output is correct
6 Correct 55 ms 22544 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 57 ms 18628 KB Output is correct
2 Correct 67 ms 16544 KB Output is correct
3 Correct 75 ms 16328 KB Output is correct
4 Correct 84 ms 16388 KB Output is correct
5 Correct 79 ms 16328 KB Output is correct
6 Correct 55 ms 18888 KB Output is correct
7 Correct 54 ms 18632 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 57 ms 18628 KB Output is correct
2 Correct 67 ms 16544 KB Output is correct
3 Correct 75 ms 16328 KB Output is correct
4 Correct 84 ms 16388 KB Output is correct
5 Correct 79 ms 16328 KB Output is correct
6 Correct 55 ms 18888 KB Output is correct
7 Correct 54 ms 18632 KB Output is correct
8 Correct 58 ms 18884 KB Output is correct
9 Correct 57 ms 18880 KB Output is correct
10 Correct 49 ms 22216 KB Output is correct
11 Correct 48 ms 22216 KB Output is correct
12 Correct 61 ms 16580 KB Output is correct
13 Correct 58 ms 16796 KB Output is correct
14 Correct 94 ms 16324 KB Output is correct
15 Correct 94 ms 16204 KB Output is correct
16 Correct 86 ms 16448 KB Output is correct
17 Correct 81 ms 16328 KB Output is correct
18 Correct 84 ms 16448 KB Output is correct
19 Correct 65 ms 16676 KB Output is correct
20 Correct 55 ms 18956 KB Output is correct
21 Correct 56 ms 19216 KB Output is correct
22 Correct 56 ms 18888 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 344 KB Output is correct
2 Correct 4 ms 1884 KB Output is correct
3 Correct 4 ms 1884 KB Output is correct
4 Correct 0 ms 348 KB Output is correct
5 Correct 0 ms 348 KB Output is correct
6 Correct 4 ms 2136 KB Output is correct
7 Correct 4 ms 2140 KB Output is correct
8 Correct 3 ms 1884 KB Output is correct
9 Correct 4 ms 2392 KB Output is correct
10 Correct 0 ms 348 KB Output is correct
11 Correct 0 ms 456 KB Output is correct
12 Correct 0 ms 348 KB Output is correct
13 Correct 0 ms 348 KB Output is correct
14 Correct 0 ms 420 KB Output is correct
15 Correct 41 ms 2140 KB Output is correct
16 Correct 43 ms 2384 KB Output is correct
17 Correct 47 ms 2292 KB Output is correct
18 Correct 4 ms 2256 KB Output is correct
19 Correct 4 ms 1884 KB Output is correct
20 Correct 18 ms 2396 KB Output is correct
21 Correct 27 ms 2392 KB Output is correct
22 Correct 0 ms 348 KB Output is correct
23 Correct 5 ms 1884 KB Output is correct
24 Correct 4 ms 1884 KB Output is correct
25 Correct 0 ms 348 KB Output is correct
26 Correct 1 ms 348 KB Output is correct
27 Correct 4 ms 2140 KB Output is correct
28 Correct 4 ms 2140 KB Output is correct
29 Correct 3 ms 1884 KB Output is correct
30 Correct 3 ms 1884 KB Output is correct
31 Correct 1 ms 348 KB Output is correct
32 Correct 0 ms 348 KB Output is correct
33 Correct 0 ms 456 KB Output is correct
34 Correct 0 ms 348 KB Output is correct
35 Correct 0 ms 348 KB Output is correct
36 Correct 52 ms 22224 KB Output is correct
37 Correct 0 ms 600 KB Output is correct
38 Correct 52 ms 21636 KB Output is correct
39 Correct 48 ms 20936 KB Output is correct
40 Correct 50 ms 22212 KB Output is correct
41 Correct 55 ms 22544 KB Output is correct
42 Correct 57 ms 18628 KB Output is correct
43 Correct 67 ms 16544 KB Output is correct
44 Correct 75 ms 16328 KB Output is correct
45 Correct 84 ms 16388 KB Output is correct
46 Correct 79 ms 16328 KB Output is correct
47 Correct 55 ms 18888 KB Output is correct
48 Correct 54 ms 18632 KB Output is correct
49 Correct 58 ms 18884 KB Output is correct
50 Correct 57 ms 18880 KB Output is correct
51 Correct 49 ms 22216 KB Output is correct
52 Correct 48 ms 22216 KB Output is correct
53 Correct 61 ms 16580 KB Output is correct
54 Correct 58 ms 16796 KB Output is correct
55 Correct 94 ms 16324 KB Output is correct
56 Correct 94 ms 16204 KB Output is correct
57 Correct 86 ms 16448 KB Output is correct
58 Correct 81 ms 16328 KB Output is correct
59 Correct 84 ms 16448 KB Output is correct
60 Correct 65 ms 16676 KB Output is correct
61 Correct 55 ms 18956 KB Output is correct
62 Correct 56 ms 19216 KB Output is correct
63 Correct 56 ms 18888 KB Output is correct
64 Correct 59 ms 18856 KB Output is correct
65 Correct 96 ms 16324 KB Output is correct
66 Correct 101 ms 16188 KB Output is correct
67 Correct 96 ms 16400 KB Output is correct
68 Correct 57 ms 16628 KB Output is correct
69 Correct 55 ms 16580 KB Output is correct
70 Correct 62 ms 19308 KB Output is correct
71 Correct 59 ms 18632 KB Output is correct
72 Correct 0 ms 348 KB Output is correct
73 Correct 5 ms 1880 KB Output is correct
74 Correct 4 ms 1880 KB Output is correct
75 Correct 0 ms 344 KB Output is correct
76 Correct 0 ms 348 KB Output is correct
77 Correct 4 ms 2140 KB Output is correct
78 Correct 4 ms 2140 KB Output is correct
79 Correct 3 ms 1884 KB Output is correct
80 Correct 4 ms 1884 KB Output is correct
81 Correct 0 ms 348 KB Output is correct
82 Correct 0 ms 348 KB Output is correct
83 Correct 0 ms 348 KB Output is correct
84 Correct 0 ms 460 KB Output is correct
85 Correct 0 ms 348 KB Output is correct
86 Correct 42 ms 2140 KB Output is correct
87 Correct 47 ms 2372 KB Output is correct
88 Correct 43 ms 2392 KB Output is correct
89 Correct 4 ms 2140 KB Output is correct
90 Correct 4 ms 1884 KB Output is correct
91 Correct 18 ms 2396 KB Output is correct
92 Correct 33 ms 2396 KB Output is correct
93 Correct 0 ms 348 KB Output is correct
94 Correct 53 ms 22308 KB Output is correct
95 Correct 51 ms 21700 KB Output is correct
96 Correct 54 ms 20932 KB Output is correct
97 Correct 52 ms 22216 KB Output is correct
98 Correct 55 ms 22724 KB Output is correct
99 Correct 85 ms 16344 KB Output is correct
100 Correct 84 ms 16328 KB Output is correct
101 Correct 91 ms 16600 KB Output is correct
102 Correct 67 ms 16584 KB Output is correct
103 Correct 54 ms 18624 KB Output is correct
104 Correct 56 ms 19144 KB Output is correct
105 Correct 55 ms 18648 KB Output is correct
106 Correct 65 ms 18968 KB Output is correct
107 Correct 56 ms 18628 KB Output is correct
108 Correct 50 ms 22216 KB Output is correct
109 Correct 48 ms 22216 KB Output is correct
110 Correct 61 ms 16584 KB Output is correct
111 Correct 69 ms 16680 KB Output is correct
112 Correct 99 ms 16324 KB Output is correct
113 Correct 99 ms 16200 KB Output is correct