Submission #799722

# Submission time Handle Problem Language Result Execution time Memory
799722 2023-07-31T20:59:13 Z finn__ Keys (IOI21_keys) C++17
100 / 100
803 ms 157904 KB
#include <bits/stdc++.h>
using namespace std;

constexpr size_t N = 300000;

vector<uint32_t> g[N], scc[N], cycle;
unordered_map<uint32_t, vector<uint32_t>> edges[N];
set<uint32_t> keys[N];
bitset<N> visited, in_dfs_path, is_leaf;
uint32_t scc_index[N];

uint32_t dfs(size_t u)
{
    if (in_dfs_path[u]) // SCC found
        return u;
    visited[u] = 1;
    in_dfs_path[u] = 1;

    while (!g[u].empty())
    {
        uint32_t v = scc_index[g[u][0]];
        swap(g[u][0], g[u].back());
        g[u].pop_back();

        uint32_t x = dfs(v);

        if (x != UINT32_MAX)
        {
            cycle.push_back(u);
            if (x == u) // backtracked until the start of the cycle
            {
                uint32_t main = u;
                for (auto const &w : cycle)
                    if (scc[w].size() > scc[main].size())
                        main = w;

                in_dfs_path[u] = 0;
                in_dfs_path[main] = 1;
                u = main;
                cycle.erase(find(cycle.begin(), cycle.end(), main));

                for (auto const &w : cycle)
                {
                    scc[u].insert(scc[u].end(), scc[w].begin(), scc[w].end());
                    for (auto const &y : scc[w])
                        scc_index[y] = u;
                    scc[w].clear();
                    g[u].insert(g[u].end(), g[w].begin(), g[w].end());

                    is_leaf[u] = is_leaf[u] && is_leaf[w];
                    keys[u].insert(keys[w].begin(), keys[w].end());

                    for (auto const &c : keys[w])
                    {
                        auto it = edges[u].find(c);
                        if (it != edges[u].end())
                        {
                            g[u].insert(g[u].end(), it->second.begin(), it->second.end());
                            it->second.clear();
                        }
                    }
                }
                for (auto const &w : cycle)
                {
                    for (auto const &[c, e] : edges[w])
                        if (keys[u].find(c) != keys[u].end())
                            g[u].insert(g[u].end(), e.begin(), e.end());
                        else
                            edges[u][c].insert(edges[u][c].end(), e.begin(), e.end());
                }

                cycle.clear();
            }
            else
            {
                in_dfs_path[u] = 0;
                return x;
            }
        }
        else
        {
            is_leaf[u] = 0;
        }
    }

    in_dfs_path[u] = 0;
    return UINT32_MAX;
}

vector<int> find_reachable(vector<int> r, vector<int> u, vector<int> v, vector<int> c)
{
    size_t const n = r.size();

    for (size_t i = 0; i < u.size(); ++i)
    {
        visited[u[i]] = visited[u[i]] || c[i] == r[u[i]];
        visited[v[i]] = visited[v[i]] || c[i] == r[v[i]];
    }
    if (visited.count() < n)
    {
        vector<int> ans(n);
        for (size_t i = 0; i < n; ++i)
            ans[i] = !visited[i];
        return ans;
    }
    visited.reset();

    for (size_t i = 0; i < n; ++i)
        keys[i].insert(r[i]), scc[i].push_back(i);

    for (size_t i = 0; i < u.size(); ++i)
    {
        if (c[i] == r[u[i]])
            g[u[i]].push_back(v[i]);
        else
            edges[u[i]][c[i]].push_back(v[i]);

        if (c[i] == r[v[i]])
            g[v[i]].push_back(u[i]);
        else
            edges[v[i]][c[i]].push_back(u[i]);
    }

    iota(scc_index, scc_index + N, 0);
    for (size_t i = 0; i < n; ++i)
        is_leaf[i] = 1;
    for (size_t i = 0; i < n; ++i)
        if (!visited[i])
            dfs(i);

    size_t smallest = SIZE_MAX;
    for (size_t i = 0; i < n; ++i)
        if (is_leaf[i] && scc[i].size())
            smallest = min(smallest, scc[i].size());
    vector<int> ans(n);
    for (size_t i = 0; i < n; ++i)
        if (is_leaf[i] && scc[i].size() == smallest)
            for (auto const &u : scc[i])
                ans[u] = 1;
    return ans;
}
# Verdict Execution time Memory Grader output
1 Correct 24 ms 46072 KB Output is correct
2 Correct 22 ms 44928 KB Output is correct
3 Correct 24 ms 44884 KB Output is correct
4 Correct 27 ms 44844 KB Output is correct
5 Correct 24 ms 46036 KB Output is correct
6 Correct 23 ms 46036 KB Output is correct
7 Correct 22 ms 46076 KB Output is correct
8 Correct 25 ms 46104 KB Output is correct
9 Correct 23 ms 44928 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 24 ms 46072 KB Output is correct
2 Correct 22 ms 44928 KB Output is correct
3 Correct 24 ms 44884 KB Output is correct
4 Correct 27 ms 44844 KB Output is correct
5 Correct 24 ms 46036 KB Output is correct
6 Correct 23 ms 46036 KB Output is correct
7 Correct 22 ms 46076 KB Output is correct
8 Correct 25 ms 46104 KB Output is correct
9 Correct 23 ms 44928 KB Output is correct
10 Correct 22 ms 44940 KB Output is correct
11 Correct 22 ms 44936 KB Output is correct
12 Correct 22 ms 44856 KB Output is correct
13 Correct 24 ms 44884 KB Output is correct
14 Correct 24 ms 44904 KB Output is correct
15 Correct 24 ms 46164 KB Output is correct
16 Correct 22 ms 46036 KB Output is correct
17 Correct 23 ms 46140 KB Output is correct
18 Correct 22 ms 46056 KB Output is correct
19 Correct 26 ms 46132 KB Output is correct
20 Correct 28 ms 46060 KB Output is correct
21 Correct 25 ms 46200 KB Output is correct
22 Correct 23 ms 44884 KB Output is correct
23 Correct 23 ms 44904 KB Output is correct
24 Correct 24 ms 44832 KB Output is correct
25 Correct 23 ms 44888 KB Output is correct
26 Correct 23 ms 44920 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 24 ms 46072 KB Output is correct
2 Correct 22 ms 44928 KB Output is correct
3 Correct 24 ms 44884 KB Output is correct
4 Correct 27 ms 44844 KB Output is correct
5 Correct 24 ms 46036 KB Output is correct
6 Correct 23 ms 46036 KB Output is correct
7 Correct 22 ms 46076 KB Output is correct
8 Correct 25 ms 46104 KB Output is correct
9 Correct 23 ms 44928 KB Output is correct
10 Correct 22 ms 44940 KB Output is correct
11 Correct 22 ms 44936 KB Output is correct
12 Correct 22 ms 44856 KB Output is correct
13 Correct 24 ms 44884 KB Output is correct
14 Correct 24 ms 44904 KB Output is correct
15 Correct 24 ms 46164 KB Output is correct
16 Correct 22 ms 46036 KB Output is correct
17 Correct 23 ms 46140 KB Output is correct
18 Correct 22 ms 46056 KB Output is correct
19 Correct 26 ms 46132 KB Output is correct
20 Correct 28 ms 46060 KB Output is correct
21 Correct 25 ms 46200 KB Output is correct
22 Correct 23 ms 44884 KB Output is correct
23 Correct 23 ms 44904 KB Output is correct
24 Correct 24 ms 44832 KB Output is correct
25 Correct 23 ms 44888 KB Output is correct
26 Correct 23 ms 44920 KB Output is correct
27 Correct 23 ms 44936 KB Output is correct
28 Correct 30 ms 44936 KB Output is correct
29 Correct 23 ms 44932 KB Output is correct
30 Correct 25 ms 46440 KB Output is correct
31 Correct 23 ms 44932 KB Output is correct
32 Correct 24 ms 46272 KB Output is correct
33 Correct 23 ms 46452 KB Output is correct
34 Correct 28 ms 46412 KB Output is correct
35 Correct 22 ms 44928 KB Output is correct
36 Correct 22 ms 44920 KB Output is correct
37 Correct 23 ms 45012 KB Output is correct
38 Correct 23 ms 45008 KB Output is correct
39 Correct 23 ms 44996 KB Output is correct
40 Correct 28 ms 46572 KB Output is correct
41 Correct 25 ms 46668 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 24 ms 46072 KB Output is correct
2 Correct 22 ms 44928 KB Output is correct
3 Correct 24 ms 44884 KB Output is correct
4 Correct 27 ms 44844 KB Output is correct
5 Correct 24 ms 46036 KB Output is correct
6 Correct 23 ms 46036 KB Output is correct
7 Correct 22 ms 46076 KB Output is correct
8 Correct 25 ms 46104 KB Output is correct
9 Correct 23 ms 44928 KB Output is correct
10 Correct 96 ms 57648 KB Output is correct
11 Correct 303 ms 97632 KB Output is correct
12 Correct 112 ms 67696 KB Output is correct
13 Correct 501 ms 150372 KB Output is correct
14 Correct 170 ms 97544 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 24 ms 46072 KB Output is correct
2 Correct 22 ms 44928 KB Output is correct
3 Correct 24 ms 44884 KB Output is correct
4 Correct 27 ms 44844 KB Output is correct
5 Correct 24 ms 46036 KB Output is correct
6 Correct 23 ms 46036 KB Output is correct
7 Correct 22 ms 46076 KB Output is correct
8 Correct 25 ms 46104 KB Output is correct
9 Correct 23 ms 44928 KB Output is correct
10 Correct 22 ms 44940 KB Output is correct
11 Correct 22 ms 44936 KB Output is correct
12 Correct 22 ms 44856 KB Output is correct
13 Correct 24 ms 44884 KB Output is correct
14 Correct 24 ms 44904 KB Output is correct
15 Correct 24 ms 46164 KB Output is correct
16 Correct 22 ms 46036 KB Output is correct
17 Correct 23 ms 46140 KB Output is correct
18 Correct 22 ms 46056 KB Output is correct
19 Correct 26 ms 46132 KB Output is correct
20 Correct 28 ms 46060 KB Output is correct
21 Correct 25 ms 46200 KB Output is correct
22 Correct 23 ms 44884 KB Output is correct
23 Correct 23 ms 44904 KB Output is correct
24 Correct 24 ms 44832 KB Output is correct
25 Correct 23 ms 44888 KB Output is correct
26 Correct 23 ms 44920 KB Output is correct
27 Correct 23 ms 44936 KB Output is correct
28 Correct 30 ms 44936 KB Output is correct
29 Correct 23 ms 44932 KB Output is correct
30 Correct 25 ms 46440 KB Output is correct
31 Correct 23 ms 44932 KB Output is correct
32 Correct 24 ms 46272 KB Output is correct
33 Correct 23 ms 46452 KB Output is correct
34 Correct 28 ms 46412 KB Output is correct
35 Correct 22 ms 44928 KB Output is correct
36 Correct 22 ms 44920 KB Output is correct
37 Correct 23 ms 45012 KB Output is correct
38 Correct 23 ms 45008 KB Output is correct
39 Correct 23 ms 44996 KB Output is correct
40 Correct 28 ms 46572 KB Output is correct
41 Correct 25 ms 46668 KB Output is correct
42 Correct 96 ms 57648 KB Output is correct
43 Correct 303 ms 97632 KB Output is correct
44 Correct 112 ms 67696 KB Output is correct
45 Correct 501 ms 150372 KB Output is correct
46 Correct 170 ms 97544 KB Output is correct
47 Correct 24 ms 46096 KB Output is correct
48 Correct 25 ms 46120 KB Output is correct
49 Correct 24 ms 44936 KB Output is correct
50 Correct 161 ms 95948 KB Output is correct
51 Correct 128 ms 63044 KB Output is correct
52 Correct 100 ms 57008 KB Output is correct
53 Correct 90 ms 57012 KB Output is correct
54 Correct 90 ms 57032 KB Output is correct
55 Correct 100 ms 58808 KB Output is correct
56 Correct 124 ms 63296 KB Output is correct
57 Correct 121 ms 63432 KB Output is correct
58 Correct 123 ms 63296 KB Output is correct
59 Correct 772 ms 157840 KB Output is correct
60 Correct 123 ms 61020 KB Output is correct
61 Correct 116 ms 61276 KB Output is correct
62 Correct 109 ms 59692 KB Output is correct
63 Correct 295 ms 143512 KB Output is correct
64 Correct 27 ms 47060 KB Output is correct
65 Correct 28 ms 47036 KB Output is correct
66 Correct 122 ms 59708 KB Output is correct
67 Correct 40 ms 51424 KB Output is correct
68 Correct 51 ms 55004 KB Output is correct
69 Correct 803 ms 154520 KB Output is correct
70 Correct 103 ms 63720 KB Output is correct
71 Correct 180 ms 98172 KB Output is correct
72 Correct 748 ms 157904 KB Output is correct
73 Correct 111 ms 59704 KB Output is correct