Submission #559461

# Submission time Handle Problem Language Result Execution time Memory
559461 2022-05-09T21:38:51 Z four_specks Railway (BOI17_railway) C++17
100 / 100
201 ms 42692 KB
#include <bits/stdc++.h>

using namespace std;

inline namespace
{
    template <typename Fun>
    struct YCombinator
    {
        template <typename T>
        YCombinator(T &&_fun) : fun(forward<T>(_fun)) {}

        template <typename... Args>
        decltype(auto) operator()(Args &&...args) { return fun(ref(*this), forward<Args>(args)...); }

    private:
        Fun fun;
    };

    template <typename T>
    YCombinator(T &&) -> YCombinator<decay_t<T>>;

    template <typename T, typename Op>
    struct SparseTable
    {
        SparseTable(const vector<T> &vec, Op _op = Op())
            : op(_op), sz((int)vec.size())
        {
            table.push_back(vec);
            for (int k = 1; k <= __lg(sz); k++)
            {
                table.emplace_back(sz - (1 << k) + 1);
                for (int i = 0; i + (1 << k) <= sz; i++)
                    table[k][i] = op(table[k - 1][i], table[k - 1][i + (1 << (k - 1))]);
            }
        }

        T query(int l, int r) const
        {
            int k = __lg(r - l);
            return op(table[k][l], table[k][r - (1 << k)]);
        }

        T get(int l) const { return table[0][l]; }

        int size() const { return sz; }

    private:
        Op op;

        int sz;
        vector<vector<T>> table;
    };

    struct LCA
    {
        LCA(vector<vector<int>> &_adj, int _root = 0)
            : n((int)_adj.size()), root(_root), st(n), level(n), rmq(dfs(_adj), min)
        {
        }

        int query(int a, int b) const
        {
            if (a == b)
                return a;

            if (st[a] > st[b])
                swap(a, b);

            return path[rmq.query(st[a], st[b])];
        }

        int depth(int a) const { return level[a]; }

        int dist(int a, int b) const { return depth(a) + depth(b) - 2 * depth(query(a, b)); }

    private:
        int n;
        int root;

        vector<int> st, path, level;
        SparseTable<int, const int &(*)(const int &, const int &)> rmq;

        vector<int> dfs(vector<vector<int>> &adj)
        {
            vector<int> ret;
            path.reserve(n), ret.reserve(n);

            int timer = 0;

            auto yc = [&](auto self, int u, int p, int d) -> void
            {
                st[u] = timer++;
                level[u] = d;

                for (int v : adj[u])
                {
                    if (v != p)
                    {
                        path.push_back(u), ret.push_back(st[u]);
                        self(self, v, u, d + 1);
                    }
                }
            };

            yc(yc, root, -1, 0);

            return ret;
        };
    };

} // namespace

void solve()
{
    int n;
    int m, k;
    cin >> n >> m >> k;

    vector<vector<int>> adj(n);
    vector<vector<array<int, 2>>> gr(n);
    for (int i = 0; i < n - 1; i++)
    {
        int a, b;
        cin >> a >> b, --a, --b;

        adj[a].push_back(b),
        adj[b].push_back(a);
        gr[a].push_back({b, i}),
        gr[b].push_back({a, i});
    }

    LCA lca(adj);

    vector<vector<int>> change(n);
    for (int w = 0; w < m; w++)
    {
        int s;
        cin >> s;

        vector<int> c(s);
        for (int &a : c)
            cin >> a, --a;

        int l = lca.query(c[0], c[1]);
        for (int i = 2; i < s; i++)
            l = lca.query(l, c[i]);

        for (int a : c)
            change[a].push_back(w);
        change[l].push_back(~w);
    }

    vector<int> cnt(n - 1);

    YCombinator(
    [&](auto self, int u, int p) -> set<int>
    {
        set<int> st;
        for (auto [v, i] : gr[u])
        {
            if (v != p)
            {
                set<int> st1 = self(v, u);
                cnt[i] = (int)st1.size();
                if ((int)st.size() < (int)st1.size())
                    swap(st, st1);
                while (!st1.empty())
                {
                    auto it = st1.begin();
                    st.insert(*it);
                    st1.erase(it);
                }
            }
        }
        for (int w : change[u])
        {
            if (w >= 0)
                st.insert(w);
        }
        for (int w : change[u])
        {
            if (w < 0)
                st.erase(~w);
        }

        return st;
    })(0, -1);

    vector<int> tracks;
    for (int i = 0; i < n - 1; i++)
    {
        if (cnt[i] >= k)
            tracks.push_back(i);
    }

    cout << (int)tracks.size() << '\n';
    for (int track : tracks)
        cout << track + 1 << ' ';
    cout << '\n';
}

int main()
{
    ios_base::sync_with_stdio(false), cin.tie(NULL);

    solve();

    return 0;
}
# Verdict Execution time Memory Grader output
1 Correct 0 ms 212 KB Output is correct
2 Correct 5 ms 2320 KB Output is correct
3 Correct 6 ms 2352 KB Output is correct
4 Correct 0 ms 212 KB Output is correct
5 Correct 0 ms 212 KB Output is correct
6 Correct 6 ms 3284 KB Output is correct
7 Correct 6 ms 2516 KB Output is correct
8 Correct 5 ms 2516 KB Output is correct
9 Correct 5 ms 2516 KB Output is correct
10 Correct 0 ms 212 KB Output is correct
11 Correct 0 ms 212 KB Output is correct
12 Correct 0 ms 212 KB Output is correct
13 Correct 0 ms 212 KB Output is correct
14 Correct 0 ms 212 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 212 KB Output is correct
2 Correct 5 ms 2320 KB Output is correct
3 Correct 6 ms 2352 KB Output is correct
4 Correct 0 ms 212 KB Output is correct
5 Correct 0 ms 212 KB Output is correct
6 Correct 6 ms 3284 KB Output is correct
7 Correct 6 ms 2516 KB Output is correct
8 Correct 5 ms 2516 KB Output is correct
9 Correct 5 ms 2516 KB Output is correct
10 Correct 0 ms 212 KB Output is correct
11 Correct 0 ms 212 KB Output is correct
12 Correct 0 ms 212 KB Output is correct
13 Correct 0 ms 212 KB Output is correct
14 Correct 0 ms 212 KB Output is correct
15 Correct 25 ms 3076 KB Output is correct
16 Correct 25 ms 3084 KB Output is correct
17 Correct 26 ms 3088 KB Output is correct
18 Correct 8 ms 3296 KB Output is correct
19 Correct 7 ms 2516 KB Output is correct
20 Correct 30 ms 3288 KB Output is correct
21 Correct 28 ms 3260 KB Output is correct
22 Correct 0 ms 212 KB Output is correct
23 Correct 6 ms 2388 KB Output is correct
24 Correct 6 ms 2336 KB Output is correct
25 Correct 0 ms 212 KB Output is correct
26 Correct 0 ms 212 KB Output is correct
27 Correct 6 ms 3284 KB Output is correct
28 Correct 6 ms 2548 KB Output is correct
29 Correct 5 ms 2516 KB Output is correct
30 Correct 5 ms 2516 KB Output is correct
31 Correct 0 ms 212 KB Output is correct
32 Correct 0 ms 212 KB Output is correct
33 Correct 0 ms 212 KB Output is correct
34 Correct 0 ms 212 KB Output is correct
35 Correct 0 ms 212 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 133 ms 42492 KB Output is correct
2 Correct 0 ms 212 KB Output is correct
3 Correct 109 ms 42052 KB Output is correct
4 Correct 107 ms 41724 KB Output is correct
5 Correct 94 ms 41972 KB Output is correct
6 Correct 95 ms 41924 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 100 ms 33664 KB Output is correct
2 Correct 124 ms 26040 KB Output is correct
3 Correct 141 ms 27204 KB Output is correct
4 Correct 131 ms 27524 KB Output is correct
5 Correct 133 ms 26884 KB Output is correct
6 Correct 102 ms 33704 KB Output is correct
7 Correct 100 ms 33696 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 100 ms 33664 KB Output is correct
2 Correct 124 ms 26040 KB Output is correct
3 Correct 141 ms 27204 KB Output is correct
4 Correct 131 ms 27524 KB Output is correct
5 Correct 133 ms 26884 KB Output is correct
6 Correct 102 ms 33704 KB Output is correct
7 Correct 100 ms 33696 KB Output is correct
8 Correct 99 ms 33460 KB Output is correct
9 Correct 100 ms 33476 KB Output is correct
10 Correct 89 ms 41880 KB Output is correct
11 Correct 93 ms 41796 KB Output is correct
12 Correct 73 ms 24528 KB Output is correct
13 Correct 69 ms 24556 KB Output is correct
14 Correct 119 ms 23512 KB Output is correct
15 Correct 111 ms 23492 KB Output is correct
16 Correct 138 ms 27516 KB Output is correct
17 Correct 141 ms 27560 KB Output is correct
18 Correct 131 ms 27636 KB Output is correct
19 Correct 122 ms 25972 KB Output is correct
20 Correct 103 ms 33740 KB Output is correct
21 Correct 103 ms 33648 KB Output is correct
22 Correct 101 ms 33776 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 212 KB Output is correct
2 Correct 5 ms 2320 KB Output is correct
3 Correct 6 ms 2352 KB Output is correct
4 Correct 0 ms 212 KB Output is correct
5 Correct 0 ms 212 KB Output is correct
6 Correct 6 ms 3284 KB Output is correct
7 Correct 6 ms 2516 KB Output is correct
8 Correct 5 ms 2516 KB Output is correct
9 Correct 5 ms 2516 KB Output is correct
10 Correct 0 ms 212 KB Output is correct
11 Correct 0 ms 212 KB Output is correct
12 Correct 0 ms 212 KB Output is correct
13 Correct 0 ms 212 KB Output is correct
14 Correct 0 ms 212 KB Output is correct
15 Correct 25 ms 3076 KB Output is correct
16 Correct 25 ms 3084 KB Output is correct
17 Correct 26 ms 3088 KB Output is correct
18 Correct 8 ms 3296 KB Output is correct
19 Correct 7 ms 2516 KB Output is correct
20 Correct 30 ms 3288 KB Output is correct
21 Correct 28 ms 3260 KB Output is correct
22 Correct 0 ms 212 KB Output is correct
23 Correct 6 ms 2388 KB Output is correct
24 Correct 6 ms 2336 KB Output is correct
25 Correct 0 ms 212 KB Output is correct
26 Correct 0 ms 212 KB Output is correct
27 Correct 6 ms 3284 KB Output is correct
28 Correct 6 ms 2548 KB Output is correct
29 Correct 5 ms 2516 KB Output is correct
30 Correct 5 ms 2516 KB Output is correct
31 Correct 0 ms 212 KB Output is correct
32 Correct 0 ms 212 KB Output is correct
33 Correct 0 ms 212 KB Output is correct
34 Correct 0 ms 212 KB Output is correct
35 Correct 0 ms 212 KB Output is correct
36 Correct 133 ms 42492 KB Output is correct
37 Correct 0 ms 212 KB Output is correct
38 Correct 109 ms 42052 KB Output is correct
39 Correct 107 ms 41724 KB Output is correct
40 Correct 94 ms 41972 KB Output is correct
41 Correct 95 ms 41924 KB Output is correct
42 Correct 100 ms 33664 KB Output is correct
43 Correct 124 ms 26040 KB Output is correct
44 Correct 141 ms 27204 KB Output is correct
45 Correct 131 ms 27524 KB Output is correct
46 Correct 133 ms 26884 KB Output is correct
47 Correct 102 ms 33704 KB Output is correct
48 Correct 100 ms 33696 KB Output is correct
49 Correct 99 ms 33460 KB Output is correct
50 Correct 100 ms 33476 KB Output is correct
51 Correct 89 ms 41880 KB Output is correct
52 Correct 93 ms 41796 KB Output is correct
53 Correct 73 ms 24528 KB Output is correct
54 Correct 69 ms 24556 KB Output is correct
55 Correct 119 ms 23512 KB Output is correct
56 Correct 111 ms 23492 KB Output is correct
57 Correct 138 ms 27516 KB Output is correct
58 Correct 141 ms 27560 KB Output is correct
59 Correct 131 ms 27636 KB Output is correct
60 Correct 122 ms 25972 KB Output is correct
61 Correct 103 ms 33740 KB Output is correct
62 Correct 103 ms 33648 KB Output is correct
63 Correct 101 ms 33776 KB Output is correct
64 Correct 109 ms 33660 KB Output is correct
65 Correct 188 ms 26308 KB Output is correct
66 Correct 177 ms 24788 KB Output is correct
67 Correct 168 ms 24536 KB Output is correct
68 Correct 76 ms 24208 KB Output is correct
69 Correct 75 ms 24228 KB Output is correct
70 Correct 115 ms 34512 KB Output is correct
71 Correct 108 ms 33436 KB Output is correct
72 Correct 1 ms 212 KB Output is correct
73 Correct 6 ms 2388 KB Output is correct
74 Correct 9 ms 2444 KB Output is correct
75 Correct 2 ms 212 KB Output is correct
76 Correct 1 ms 320 KB Output is correct
77 Correct 7 ms 3412 KB Output is correct
78 Correct 6 ms 2636 KB Output is correct
79 Correct 6 ms 2572 KB Output is correct
80 Correct 6 ms 2516 KB Output is correct
81 Correct 1 ms 212 KB Output is correct
82 Correct 1 ms 212 KB Output is correct
83 Correct 1 ms 212 KB Output is correct
84 Correct 1 ms 324 KB Output is correct
85 Correct 1 ms 212 KB Output is correct
86 Correct 28 ms 3244 KB Output is correct
87 Correct 29 ms 3172 KB Output is correct
88 Correct 33 ms 3276 KB Output is correct
89 Correct 7 ms 3412 KB Output is correct
90 Correct 8 ms 2636 KB Output is correct
91 Correct 31 ms 3388 KB Output is correct
92 Correct 30 ms 3344 KB Output is correct
93 Correct 1 ms 212 KB Output is correct
94 Correct 149 ms 42692 KB Output is correct
95 Correct 120 ms 42188 KB Output is correct
96 Correct 124 ms 41952 KB Output is correct
97 Correct 123 ms 42092 KB Output is correct
98 Correct 109 ms 42112 KB Output is correct
99 Correct 201 ms 27824 KB Output is correct
100 Correct 189 ms 27780 KB Output is correct
101 Correct 170 ms 27652 KB Output is correct
102 Correct 154 ms 26176 KB Output is correct
103 Correct 128 ms 33896 KB Output is correct
104 Correct 126 ms 33836 KB Output is correct
105 Correct 124 ms 33828 KB Output is correct
106 Correct 126 ms 33560 KB Output is correct
107 Correct 124 ms 33440 KB Output is correct
108 Correct 113 ms 41976 KB Output is correct
109 Correct 100 ms 41968 KB Output is correct
110 Correct 89 ms 24788 KB Output is correct
111 Correct 102 ms 24764 KB Output is correct
112 Correct 152 ms 23408 KB Output is correct
113 Correct 147 ms 23896 KB Output is correct