Submission #405644

# Submission time Handle Problem Language Result Execution time Memory
405644 2021-05-16T15:54:43 Z Falcon Amusement Park (CEOI19_amusementpark) C++17
100 / 100
2157 ms 7620 KB
#include <bits/stdc++.h>

#ifdef DEBUG
#include "debug.hpp"
#endif

using namespace std;

#define all(c)              (c).begin(), (c).end()
#define rall(c)             (c).rbegin(), (c).rend()
#define traverse(c, it)     for(auto it = (c).begin(); it != (c).end(); ++it)
#define rep(i, N)           for(int i = 0; i < (N); ++i)
#define rrep(i, N)          for(int i = (N) - 1; i >= 0; --i)
#define rep1(i, N)          for(int i = 1; i <= (N); ++i)
#define rep2(i, s, e)       for(int i = (s); i <= (e); ++i)

#ifdef DEBUG
#define debug(x...)         { \
                            ++dbg::depth; \
                            string dbg_vals = dbg::to_string(x); \
                            --dbg::depth; \
                            dbg::fprint(__func__, __LINE__, #x, dbg_vals); \
                            }

#define light_debug(x)      { \
                            dbg::light = true; \
                            dbg::dout << __func__ << ":" << __LINE__; \
                            dbg::dout << "  " << #x << " = " << x << endl; \
                            dbg::light = false; \
                            }

#else
#define debug(x...)         42
#define light_debug(x)      42
#endif


using ll = long long;

template<typename T>
inline T& ckmin(T& a, T b) { return a = a > b ? b : a; }

template<typename T>
inline T& ckmax(T& a, T b) { return a = a < b ? b : a; }


constexpr int mod{998244353};

array<int, 1 << 18> popcnt;
array<int, 1 << 18> lg;

pair<int, int> solve_for(const vector<int>& adj) {
    const int n{int(adj.size())};
    vector<bool> is_independent(1 << n, true);
    rep1(mask, (1 << n) - 1) {
        rep(i, n) 
            if((mask & (1 << i)) && (adj[i] & mask) != 0) {
                is_independent[mask] = false;
                break;
            }
    }
                
    vector<long long> dp_s(1 << n), dp_c(1 << n); dp_c[0] = 1;
    vector<int> partial_cost(1 << n);

    int j, sources;
    long long t_s, t_c;
    rep1(mask, (1 << n) - 1) {
        for(sources = mask & (mask - 1); ; sources = (sources - 1) & mask) {
            sources = mask - sources;

            if(is_independent[sources]) {
                j = lg[sources];
                partial_cost[sources] =
                    partial_cost[sources - (1 << j)] + popcnt[adj[j] & mask];

                t_c = dp_c[mask ^ sources];
                t_s = dp_s[mask ^ sources] +
                    dp_c[mask ^ sources] * partial_cost[sources] % mod;

                if(popcnt[sources] & 1)
                    dp_s[mask] += t_s, dp_c[mask] += t_c;
                else
                    dp_s[mask] -= t_s, dp_c[mask] -= t_c;
            }

            sources = mask - sources;
            if(sources == 0) break;
        }

        dp_c[mask] %= mod;
        dp_s[mask] %= mod;
    }

    if(dp_s.back() < 0) dp_s.back() += mod;
    if(dp_c.back() < 0) dp_c.back() += mod;

    return make_pair(int(dp_c.back()), int(dp_s.back()));
}

vector<int> generate_adj(const vector<int>& adj, int mask) {
    vector<int> adj2(popcnt[mask]);
    vector<int> v;
    rep(i, int(adj.size())) if(mask & (1 << i)) v.push_back(i);
    
    rep(i, popcnt[mask]) rep(j, popcnt[mask])
        if(adj[v[i]] & (1 << v[j]))
            adj2[i] |= 1 << j;

    return adj2;
}

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

    rep(i, 1 << 18) popcnt[i] = __builtin_popcount(i);

    rep(i, 1 << 18) lg[i] = __lg(i);

    int n, m; cin >> n >> m;
    vector<int> adj(n);
    rep(i, m) {
        int u, v; cin >> u >> v; --u, --v;
        adj[u] |= 1 << v;
    }

    int vis{};
    long long p{1}, ans{};
    auto update_component = [&](int v) {
        int mask{};
        stack<int> s;
        s.push(v); mask |= 1 << v;
        while(!s.empty()) {
            int a = s.top(); s.pop();
            rep(u, n) {
                if(mask & (1 << u)) continue;
                if((adj[u] & (1 << a)) || (adj[a] & (1 << u)))
                    mask |= 1 << u, s.push(u);
            }
        }

        int cnt, sum;
        tie(cnt, sum) = solve_for(generate_adj(adj, mask));
        ans = (ans * cnt % mod + p * sum % mod) % mod;
        p = (p * cnt) % mod;

        vis |= mask;
    };

    rep(i, n)
        if(!(vis & (1 << i)))
            update_component(i);

    cout << ans << '\n';
    
    #ifdef DEBUG
    dbg::dout << "\nExecution time: "
              << clock() * 1000 / CLOCKS_PER_SEC 
              << "ms" << endl;
    #endif
    return 0;
}


# Verdict Execution time Memory Grader output
1 Correct 3 ms 2252 KB Output is correct
2 Correct 3 ms 2252 KB Output is correct
3 Correct 3 ms 2252 KB Output is correct
4 Correct 3 ms 2252 KB Output is correct
5 Correct 3 ms 2360 KB Output is correct
6 Correct 3 ms 2252 KB Output is correct
7 Correct 3 ms 2252 KB Output is correct
8 Correct 3 ms 2252 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 3 ms 2252 KB Output is correct
2 Correct 3 ms 2252 KB Output is correct
3 Correct 3 ms 2252 KB Output is correct
4 Correct 3 ms 2252 KB Output is correct
5 Correct 3 ms 2360 KB Output is correct
6 Correct 3 ms 2252 KB Output is correct
7 Correct 3 ms 2252 KB Output is correct
8 Correct 3 ms 2252 KB Output is correct
9 Correct 3 ms 2252 KB Output is correct
10 Correct 3 ms 2252 KB Output is correct
11 Correct 3 ms 2364 KB Output is correct
12 Correct 3 ms 2364 KB Output is correct
13 Correct 3 ms 2252 KB Output is correct
14 Correct 4 ms 2252 KB Output is correct
15 Correct 3 ms 2252 KB Output is correct
16 Correct 3 ms 2368 KB Output is correct
17 Correct 3 ms 2252 KB Output is correct
18 Correct 3 ms 2252 KB Output is correct
19 Correct 3 ms 2252 KB Output is correct
20 Correct 3 ms 2364 KB Output is correct
21 Correct 3 ms 2252 KB Output is correct
22 Correct 3 ms 2364 KB Output is correct
23 Correct 3 ms 2316 KB Output is correct
24 Correct 4 ms 2252 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 3 ms 2252 KB Output is correct
2 Correct 3 ms 2252 KB Output is correct
3 Correct 3 ms 2252 KB Output is correct
4 Correct 3 ms 2252 KB Output is correct
5 Correct 3 ms 2360 KB Output is correct
6 Correct 3 ms 2252 KB Output is correct
7 Correct 3 ms 2252 KB Output is correct
8 Correct 3 ms 2252 KB Output is correct
9 Correct 3 ms 2252 KB Output is correct
10 Correct 3 ms 2252 KB Output is correct
11 Correct 3 ms 2364 KB Output is correct
12 Correct 3 ms 2364 KB Output is correct
13 Correct 3 ms 2252 KB Output is correct
14 Correct 4 ms 2252 KB Output is correct
15 Correct 3 ms 2252 KB Output is correct
16 Correct 3 ms 2368 KB Output is correct
17 Correct 3 ms 2252 KB Output is correct
18 Correct 3 ms 2252 KB Output is correct
19 Correct 3 ms 2252 KB Output is correct
20 Correct 3 ms 2364 KB Output is correct
21 Correct 3 ms 2252 KB Output is correct
22 Correct 3 ms 2364 KB Output is correct
23 Correct 3 ms 2316 KB Output is correct
24 Correct 4 ms 2252 KB Output is correct
25 Correct 3 ms 2252 KB Output is correct
26 Correct 3 ms 2252 KB Output is correct
27 Correct 3 ms 2364 KB Output is correct
28 Correct 3 ms 2252 KB Output is correct
29 Correct 3 ms 2252 KB Output is correct
30 Correct 3 ms 2252 KB Output is correct
31 Correct 3 ms 2252 KB Output is correct
32 Correct 3 ms 2252 KB Output is correct
33 Correct 3 ms 2252 KB Output is correct
34 Correct 3 ms 2252 KB Output is correct
35 Correct 4 ms 2252 KB Output is correct
36 Correct 3 ms 2252 KB Output is correct
37 Correct 3 ms 2364 KB Output is correct
38 Correct 3 ms 2348 KB Output is correct
39 Correct 3 ms 2252 KB Output is correct
40 Correct 3 ms 2252 KB Output is correct
41 Correct 3 ms 2356 KB Output is correct
42 Correct 3 ms 2380 KB Output is correct
43 Correct 3 ms 2368 KB Output is correct
44 Correct 3 ms 2252 KB Output is correct
45 Correct 3 ms 2252 KB Output is correct
46 Correct 3 ms 2252 KB Output is correct
47 Correct 3 ms 2252 KB Output is correct
48 Correct 3 ms 2380 KB Output is correct
49 Correct 3 ms 2380 KB Output is correct
50 Correct 3 ms 2252 KB Output is correct
51 Correct 5 ms 2380 KB Output is correct
52 Correct 3 ms 2252 KB Output is correct
53 Correct 4 ms 2264 KB Output is correct
54 Correct 3 ms 2380 KB Output is correct
55 Correct 3 ms 2380 KB Output is correct
56 Correct 3 ms 2380 KB Output is correct
57 Correct 3 ms 2380 KB Output is correct
58 Correct 3 ms 2360 KB Output is correct
59 Correct 3 ms 2360 KB Output is correct
60 Correct 3 ms 2380 KB Output is correct
61 Correct 3 ms 2380 KB Output is correct
62 Correct 3 ms 2380 KB Output is correct
63 Correct 3 ms 2380 KB Output is correct
64 Correct 4 ms 2268 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 3 ms 2252 KB Output is correct
2 Correct 3 ms 2252 KB Output is correct
3 Correct 3 ms 2252 KB Output is correct
4 Correct 3 ms 2252 KB Output is correct
5 Correct 3 ms 2360 KB Output is correct
6 Correct 3 ms 2252 KB Output is correct
7 Correct 3 ms 2252 KB Output is correct
8 Correct 3 ms 2252 KB Output is correct
9 Correct 3 ms 2252 KB Output is correct
10 Correct 3 ms 2252 KB Output is correct
11 Correct 3 ms 2364 KB Output is correct
12 Correct 3 ms 2364 KB Output is correct
13 Correct 3 ms 2252 KB Output is correct
14 Correct 4 ms 2252 KB Output is correct
15 Correct 3 ms 2252 KB Output is correct
16 Correct 3 ms 2368 KB Output is correct
17 Correct 3 ms 2252 KB Output is correct
18 Correct 3 ms 2252 KB Output is correct
19 Correct 3 ms 2252 KB Output is correct
20 Correct 3 ms 2364 KB Output is correct
21 Correct 3 ms 2252 KB Output is correct
22 Correct 3 ms 2364 KB Output is correct
23 Correct 3 ms 2316 KB Output is correct
24 Correct 4 ms 2252 KB Output is correct
25 Correct 3 ms 2252 KB Output is correct
26 Correct 3 ms 2252 KB Output is correct
27 Correct 3 ms 2364 KB Output is correct
28 Correct 3 ms 2252 KB Output is correct
29 Correct 3 ms 2252 KB Output is correct
30 Correct 3 ms 2252 KB Output is correct
31 Correct 3 ms 2252 KB Output is correct
32 Correct 3 ms 2252 KB Output is correct
33 Correct 3 ms 2252 KB Output is correct
34 Correct 3 ms 2252 KB Output is correct
35 Correct 4 ms 2252 KB Output is correct
36 Correct 3 ms 2252 KB Output is correct
37 Correct 3 ms 2364 KB Output is correct
38 Correct 3 ms 2348 KB Output is correct
39 Correct 3 ms 2252 KB Output is correct
40 Correct 3 ms 2252 KB Output is correct
41 Correct 3 ms 2356 KB Output is correct
42 Correct 3 ms 2380 KB Output is correct
43 Correct 3 ms 2368 KB Output is correct
44 Correct 3 ms 2252 KB Output is correct
45 Correct 3 ms 2252 KB Output is correct
46 Correct 3 ms 2252 KB Output is correct
47 Correct 3 ms 2252 KB Output is correct
48 Correct 3 ms 2380 KB Output is correct
49 Correct 3 ms 2380 KB Output is correct
50 Correct 3 ms 2252 KB Output is correct
51 Correct 5 ms 2380 KB Output is correct
52 Correct 3 ms 2252 KB Output is correct
53 Correct 4 ms 2264 KB Output is correct
54 Correct 3 ms 2380 KB Output is correct
55 Correct 3 ms 2380 KB Output is correct
56 Correct 3 ms 2380 KB Output is correct
57 Correct 3 ms 2380 KB Output is correct
58 Correct 3 ms 2360 KB Output is correct
59 Correct 3 ms 2360 KB Output is correct
60 Correct 3 ms 2380 KB Output is correct
61 Correct 3 ms 2380 KB Output is correct
62 Correct 3 ms 2380 KB Output is correct
63 Correct 3 ms 2380 KB Output is correct
64 Correct 4 ms 2268 KB Output is correct
65 Correct 17 ms 2708 KB Output is correct
66 Correct 16 ms 2708 KB Output is correct
67 Correct 16 ms 2616 KB Output is correct
68 Correct 14 ms 2708 KB Output is correct
69 Correct 33 ms 2692 KB Output is correct
70 Correct 26 ms 2600 KB Output is correct
71 Correct 25 ms 2688 KB Output is correct
72 Correct 25 ms 2636 KB Output is correct
73 Correct 22 ms 2712 KB Output is correct
74 Correct 21 ms 2636 KB Output is correct
75 Correct 20 ms 2636 KB Output is correct
76 Correct 20 ms 2704 KB Output is correct
77 Correct 50 ms 3024 KB Output is correct
78 Correct 44 ms 3020 KB Output is correct
79 Correct 42 ms 3020 KB Output is correct
80 Correct 39 ms 3020 KB Output is correct
81 Correct 41 ms 3020 KB Output is correct
82 Correct 34 ms 3036 KB Output is correct
83 Correct 35 ms 2980 KB Output is correct
84 Correct 32 ms 3148 KB Output is correct
85 Correct 92 ms 3020 KB Output is correct
86 Correct 75 ms 3020 KB Output is correct
87 Correct 69 ms 3020 KB Output is correct
88 Correct 67 ms 3004 KB Output is correct
89 Correct 60 ms 3004 KB Output is correct
90 Correct 58 ms 3028 KB Output is correct
91 Correct 56 ms 3020 KB Output is correct
92 Correct 53 ms 3020 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 3 ms 2252 KB Output is correct
2 Correct 3 ms 2252 KB Output is correct
3 Correct 3 ms 2252 KB Output is correct
4 Correct 3 ms 2252 KB Output is correct
5 Correct 3 ms 2360 KB Output is correct
6 Correct 3 ms 2252 KB Output is correct
7 Correct 3 ms 2252 KB Output is correct
8 Correct 3 ms 2252 KB Output is correct
9 Correct 3 ms 2252 KB Output is correct
10 Correct 3 ms 2252 KB Output is correct
11 Correct 3 ms 2364 KB Output is correct
12 Correct 3 ms 2364 KB Output is correct
13 Correct 3 ms 2252 KB Output is correct
14 Correct 4 ms 2252 KB Output is correct
15 Correct 3 ms 2252 KB Output is correct
16 Correct 3 ms 2368 KB Output is correct
17 Correct 3 ms 2252 KB Output is correct
18 Correct 3 ms 2252 KB Output is correct
19 Correct 3 ms 2252 KB Output is correct
20 Correct 3 ms 2364 KB Output is correct
21 Correct 3 ms 2252 KB Output is correct
22 Correct 3 ms 2364 KB Output is correct
23 Correct 3 ms 2316 KB Output is correct
24 Correct 4 ms 2252 KB Output is correct
25 Correct 3 ms 2252 KB Output is correct
26 Correct 3 ms 2252 KB Output is correct
27 Correct 3 ms 2364 KB Output is correct
28 Correct 3 ms 2252 KB Output is correct
29 Correct 3 ms 2252 KB Output is correct
30 Correct 3 ms 2252 KB Output is correct
31 Correct 3 ms 2252 KB Output is correct
32 Correct 3 ms 2252 KB Output is correct
33 Correct 3 ms 2252 KB Output is correct
34 Correct 3 ms 2252 KB Output is correct
35 Correct 4 ms 2252 KB Output is correct
36 Correct 3 ms 2252 KB Output is correct
37 Correct 3 ms 2364 KB Output is correct
38 Correct 3 ms 2348 KB Output is correct
39 Correct 3 ms 2252 KB Output is correct
40 Correct 3 ms 2252 KB Output is correct
41 Correct 3 ms 2356 KB Output is correct
42 Correct 3 ms 2380 KB Output is correct
43 Correct 3 ms 2368 KB Output is correct
44 Correct 3 ms 2252 KB Output is correct
45 Correct 3 ms 2252 KB Output is correct
46 Correct 3 ms 2252 KB Output is correct
47 Correct 3 ms 2252 KB Output is correct
48 Correct 3 ms 2380 KB Output is correct
49 Correct 3 ms 2380 KB Output is correct
50 Correct 3 ms 2252 KB Output is correct
51 Correct 5 ms 2380 KB Output is correct
52 Correct 3 ms 2252 KB Output is correct
53 Correct 4 ms 2264 KB Output is correct
54 Correct 3 ms 2380 KB Output is correct
55 Correct 3 ms 2380 KB Output is correct
56 Correct 3 ms 2380 KB Output is correct
57 Correct 3 ms 2380 KB Output is correct
58 Correct 3 ms 2360 KB Output is correct
59 Correct 3 ms 2360 KB Output is correct
60 Correct 3 ms 2380 KB Output is correct
61 Correct 3 ms 2380 KB Output is correct
62 Correct 3 ms 2380 KB Output is correct
63 Correct 3 ms 2380 KB Output is correct
64 Correct 4 ms 2268 KB Output is correct
65 Correct 17 ms 2708 KB Output is correct
66 Correct 16 ms 2708 KB Output is correct
67 Correct 16 ms 2616 KB Output is correct
68 Correct 14 ms 2708 KB Output is correct
69 Correct 33 ms 2692 KB Output is correct
70 Correct 26 ms 2600 KB Output is correct
71 Correct 25 ms 2688 KB Output is correct
72 Correct 25 ms 2636 KB Output is correct
73 Correct 22 ms 2712 KB Output is correct
74 Correct 21 ms 2636 KB Output is correct
75 Correct 20 ms 2636 KB Output is correct
76 Correct 20 ms 2704 KB Output is correct
77 Correct 50 ms 3024 KB Output is correct
78 Correct 44 ms 3020 KB Output is correct
79 Correct 42 ms 3020 KB Output is correct
80 Correct 39 ms 3020 KB Output is correct
81 Correct 41 ms 3020 KB Output is correct
82 Correct 34 ms 3036 KB Output is correct
83 Correct 35 ms 2980 KB Output is correct
84 Correct 32 ms 3148 KB Output is correct
85 Correct 92 ms 3020 KB Output is correct
86 Correct 75 ms 3020 KB Output is correct
87 Correct 69 ms 3020 KB Output is correct
88 Correct 67 ms 3004 KB Output is correct
89 Correct 60 ms 3004 KB Output is correct
90 Correct 58 ms 3028 KB Output is correct
91 Correct 56 ms 3020 KB Output is correct
92 Correct 53 ms 3020 KB Output is correct
93 Correct 2157 ms 7528 KB Output is correct
94 Correct 230 ms 3660 KB Output is correct
95 Correct 26 ms 2620 KB Output is correct
96 Correct 1766 ms 7528 KB Output is correct
97 Correct 1633 ms 7532 KB Output is correct
98 Correct 1829 ms 7524 KB Output is correct
99 Correct 159 ms 3660 KB Output is correct
100 Correct 135 ms 3660 KB Output is correct
101 Correct 118 ms 3644 KB Output is correct
102 Correct 108 ms 3660 KB Output is correct
103 Correct 3 ms 2364 KB Output is correct
104 Correct 3 ms 2360 KB Output is correct
105 Correct 24 ms 2636 KB Output is correct
106 Correct 135 ms 3672 KB Output is correct
107 Correct 3 ms 2356 KB Output is correct
108 Correct 170 ms 3660 KB Output is correct
109 Correct 1069 ms 7500 KB Output is correct
110 Correct 891 ms 7532 KB Output is correct
111 Correct 106 ms 3660 KB Output is correct
112 Correct 94 ms 3676 KB Output is correct
113 Correct 89 ms 3648 KB Output is correct
114 Correct 285 ms 3660 KB Output is correct
115 Correct 219 ms 3660 KB Output is correct
116 Correct 204 ms 3660 KB Output is correct
117 Correct 198 ms 3656 KB Output is correct
118 Correct 184 ms 3660 KB Output is correct
119 Correct 169 ms 3660 KB Output is correct
120 Correct 160 ms 3660 KB Output is correct
121 Correct 153 ms 3660 KB Output is correct
122 Correct 332 ms 4964 KB Output is correct
123 Correct 284 ms 4940 KB Output is correct
124 Correct 261 ms 4944 KB Output is correct
125 Correct 255 ms 5064 KB Output is correct
126 Correct 245 ms 4956 KB Output is correct
127 Correct 228 ms 5060 KB Output is correct
128 Correct 215 ms 4916 KB Output is correct
129 Correct 207 ms 4940 KB Output is correct
130 Correct 232 ms 4940 KB Output is correct
131 Correct 866 ms 4948 KB Output is correct
132 Correct 696 ms 4928 KB Output is correct
133 Correct 619 ms 4940 KB Output is correct
134 Correct 606 ms 4948 KB Output is correct
135 Correct 559 ms 5048 KB Output is correct
136 Correct 500 ms 5060 KB Output is correct
137 Correct 471 ms 5060 KB Output is correct
138 Correct 451 ms 4948 KB Output is correct
139 Correct 477 ms 5056 KB Output is correct
140 Correct 420 ms 4948 KB Output is correct
141 Correct 371 ms 4940 KB Output is correct
142 Correct 773 ms 7544 KB Output is correct
143 Correct 712 ms 7620 KB Output is correct
144 Correct 698 ms 7532 KB Output is correct
145 Correct 668 ms 7528 KB Output is correct
146 Correct 628 ms 7536 KB Output is correct
147 Correct 589 ms 7544 KB Output is correct
148 Correct 793 ms 7528 KB Output is correct
149 Correct 769 ms 7532 KB Output is correct
150 Correct 760 ms 7528 KB Output is correct
151 Correct 1008 ms 7532 KB Output is correct
152 Correct 990 ms 7620 KB Output is correct
153 Correct 1772 ms 7528 KB Output is correct
154 Correct 1779 ms 7532 KB Output is correct
155 Correct 1487 ms 7532 KB Output is correct
156 Correct 1380 ms 7524 KB Output is correct
157 Correct 1565 ms 7528 KB Output is correct
158 Correct 1418 ms 7528 KB Output is correct
159 Correct 1538 ms 7528 KB Output is correct
160 Correct 1268 ms 7528 KB Output is correct
161 Correct 1092 ms 7500 KB Output is correct
162 Correct 1321 ms 7536 KB Output is correct
163 Correct 1143 ms 7524 KB Output is correct
164 Correct 1032 ms 7532 KB Output is correct
165 Correct 964 ms 7528 KB Output is correct
166 Correct 924 ms 7532 KB Output is correct
167 Correct 915 ms 7500 KB Output is correct