답안 #582562

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
582562 2022-06-24T05:45:02 Z 박상훈(#8369) Amusement Park (CEOI19_amusementpark) C++17
100 / 100
2886 ms 5620 KB
#include <bits/stdc++.h>

typedef long long ll;
using namespace std;
const int MOD = 998244353;
int adj[20][20], dp1[(1<<18)], dp2[(1<<18)], p[(1<<18)], cnt[18];
ll cst[(1<<18)];

vector<pair<int, int>> E;
vector<int> adjj[20];
int visited[20], on[20], dc;

bool dfs(int s, int c){
    visited[s] = c;
    on[s] = 1;
    for (auto &v:adjj[s]){
        if (!visited[v]){
            if (!dfs(v, c)) return 0;
        }
        if (on[v]) return 0;
    }
    on[s] = 0;
    return 1;
}
void naive(int n, int m){
    int ans = 0;
    for (int z=0;z<(1<<m);z++){
        for (int i=0;i<n;i++) adjj[i].clear();

        for (int i=0;i<m;i++){
            if (z&(1<<i)) adjj[E[i].second].push_back(E[i].first);
            else adjj[E[i].first].push_back(E[i].second);
        }

        memset(visited, 0, sizeof(visited));
        bool flag = 1;
        int cnt =  0;
        for (int i=0;i<n;i++) if (!visited[i]){
            cnt++;
            flag &= dfs(i, cnt);
        }
        if (flag) ans = (ans+__builtin_popcount(z)) % MOD;

        //if (flag) printf("%d\n", z);
    }
    printf("%d\n", ans);
    exit(0);
}

int main(){
    //printf("%d\n", __builtin_ctz(10));
    int n, m;
    scanf("%d %d", &n, &m);
    for (int i=1;i<=m;i++){
        int x, y;
        scanf("%d %d", &x, &y);
        --x, --y;
        adj[x][y] = 1;
        adj[y][x] = -1;
        E.emplace_back(x, y);
    }

    if (m<=18) naive(n, m);

    for (int z=0;z<(1<<n);z++){
        p[z] = 1;
        for (int i=0;i<n;i++) if (z&(1<<i)){
            for (int j=i+1;j<n;j++) if (z&(1<<j)){
                if (adj[i][j]) p[z] = 0;
            }
        }
    }

    for (int z=0;z<(1<<n);z++) if (__builtin_popcount(z)<2) dp2[z] = 1;

    for (int z=0;z<(1<<n);z++) if (__builtin_popcount(z)>=2){
        memset(cnt, 0, sizeof(cnt));
        cst[z] = 0;
        for (int i=0;i<n;i++) if (z&(1<<i)){
            for (int j=0;j<n;j++) if (z&(1<<j) && adj[i][j]==-1) cnt[i]++, cst[z]++;
        }

        for (int z2=(z-1)&z;z2;z2=(z2-1)&z){
            cst[z2] = cst[z2 | (1<<(__builtin_ctz(z^z2)))] - cnt[__builtin_ctz(z^z2)];
        }


        for (int z2=z;z2;z2=(z2-1)&z) if (p[z2]){
            //ll cst = 0;
            //for (int i=0;i<n;i++) if (z2&(1<<i)) cst += cnt[i];

            if (__builtin_popcount(z2)&1){
                dp1[z] += (cst[z2] * dp2[z^z2] + dp1[z^z2]) % MOD;
                dp2[z] += dp2[z^z2];

                if (dp1[z]>=MOD) dp1[z] -= MOD;
                if (dp2[z]>=MOD) dp2[z] -= MOD;
            }
            else{
                dp1[z] -= (cst[z2] * dp2[z^z2] + dp1[z^z2]) % MOD;
                dp2[z] -= dp2[z^z2];

                if (dp1[z]<0) dp1[z] += MOD;
                if (dp2[z]<0) dp2[z] += MOD;
            }
        }
    }
    printf("%d\n", dp1[(1<<n)-1]);
    return 0;
}

Compilation message

amusementpark.cpp: In function 'int main()':
amusementpark.cpp:53:10: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
   53 |     scanf("%d %d", &n, &m);
      |     ~~~~~^~~~~~~~~~~~~~~~~
amusementpark.cpp:56:14: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
   56 |         scanf("%d %d", &x, &y);
      |         ~~~~~^~~~~~~~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 212 KB Output is correct
2 Correct 1 ms 212 KB Output is correct
3 Correct 0 ms 212 KB Output is correct
4 Correct 0 ms 212 KB Output is correct
5 Correct 0 ms 212 KB Output is correct
6 Correct 0 ms 212 KB Output is correct
7 Correct 0 ms 212 KB Output is correct
8 Correct 0 ms 212 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 212 KB Output is correct
2 Correct 1 ms 212 KB Output is correct
3 Correct 0 ms 212 KB Output is correct
4 Correct 0 ms 212 KB Output is correct
5 Correct 0 ms 212 KB Output is correct
6 Correct 0 ms 212 KB Output is correct
7 Correct 0 ms 212 KB Output is correct
8 Correct 0 ms 212 KB Output is correct
9 Correct 0 ms 212 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 0 ms 212 KB Output is correct
16 Correct 0 ms 212 KB Output is correct
17 Correct 0 ms 212 KB Output is correct
18 Correct 0 ms 212 KB Output is correct
19 Correct 1 ms 212 KB Output is correct
20 Correct 0 ms 212 KB Output is correct
21 Correct 0 ms 212 KB Output is correct
22 Correct 1 ms 212 KB Output is correct
23 Correct 2 ms 212 KB Output is correct
24 Correct 7 ms 304 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 212 KB Output is correct
2 Correct 1 ms 212 KB Output is correct
3 Correct 0 ms 212 KB Output is correct
4 Correct 0 ms 212 KB Output is correct
5 Correct 0 ms 212 KB Output is correct
6 Correct 0 ms 212 KB Output is correct
7 Correct 0 ms 212 KB Output is correct
8 Correct 0 ms 212 KB Output is correct
9 Correct 0 ms 212 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 0 ms 212 KB Output is correct
16 Correct 0 ms 212 KB Output is correct
17 Correct 0 ms 212 KB Output is correct
18 Correct 0 ms 212 KB Output is correct
19 Correct 1 ms 212 KB Output is correct
20 Correct 0 ms 212 KB Output is correct
21 Correct 0 ms 212 KB Output is correct
22 Correct 1 ms 212 KB Output is correct
23 Correct 2 ms 212 KB Output is correct
24 Correct 7 ms 304 KB Output is correct
25 Correct 0 ms 212 KB Output is correct
26 Correct 0 ms 212 KB Output is correct
27 Correct 1 ms 212 KB Output is correct
28 Correct 1 ms 212 KB Output is correct
29 Correct 7 ms 212 KB Output is correct
30 Correct 62 ms 300 KB Output is correct
31 Correct 0 ms 212 KB Output is correct
32 Correct 1 ms 212 KB Output is correct
33 Correct 1 ms 212 KB Output is correct
34 Correct 2 ms 212 KB Output is correct
35 Correct 17 ms 304 KB Output is correct
36 Correct 1 ms 340 KB Output is correct
37 Correct 1 ms 212 KB Output is correct
38 Correct 1 ms 212 KB Output is correct
39 Correct 0 ms 212 KB Output is correct
40 Correct 1 ms 212 KB Output is correct
41 Correct 2 ms 212 KB Output is correct
42 Correct 17 ms 212 KB Output is correct
43 Correct 1 ms 340 KB Output is correct
44 Correct 1 ms 340 KB Output is correct
45 Correct 1 ms 340 KB Output is correct
46 Correct 1 ms 340 KB Output is correct
47 Correct 1 ms 340 KB Output is correct
48 Correct 1 ms 212 KB Output is correct
49 Correct 7 ms 212 KB Output is correct
50 Correct 8 ms 212 KB Output is correct
51 Correct 9 ms 308 KB Output is correct
52 Correct 1 ms 212 KB Output is correct
53 Correct 1 ms 212 KB Output is correct
54 Correct 10 ms 212 KB Output is correct
55 Correct 1 ms 256 KB Output is correct
56 Correct 1 ms 340 KB Output is correct
57 Correct 1 ms 340 KB Output is correct
58 Correct 1 ms 340 KB Output is correct
59 Correct 1 ms 340 KB Output is correct
60 Correct 1 ms 340 KB Output is correct
61 Correct 1 ms 212 KB Output is correct
62 Correct 32 ms 212 KB Output is correct
63 Correct 35 ms 300 KB Output is correct
64 Correct 33 ms 212 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 212 KB Output is correct
2 Correct 1 ms 212 KB Output is correct
3 Correct 0 ms 212 KB Output is correct
4 Correct 0 ms 212 KB Output is correct
5 Correct 0 ms 212 KB Output is correct
6 Correct 0 ms 212 KB Output is correct
7 Correct 0 ms 212 KB Output is correct
8 Correct 0 ms 212 KB Output is correct
9 Correct 0 ms 212 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 0 ms 212 KB Output is correct
16 Correct 0 ms 212 KB Output is correct
17 Correct 0 ms 212 KB Output is correct
18 Correct 0 ms 212 KB Output is correct
19 Correct 1 ms 212 KB Output is correct
20 Correct 0 ms 212 KB Output is correct
21 Correct 0 ms 212 KB Output is correct
22 Correct 1 ms 212 KB Output is correct
23 Correct 2 ms 212 KB Output is correct
24 Correct 7 ms 304 KB Output is correct
25 Correct 0 ms 212 KB Output is correct
26 Correct 0 ms 212 KB Output is correct
27 Correct 1 ms 212 KB Output is correct
28 Correct 1 ms 212 KB Output is correct
29 Correct 7 ms 212 KB Output is correct
30 Correct 62 ms 300 KB Output is correct
31 Correct 0 ms 212 KB Output is correct
32 Correct 1 ms 212 KB Output is correct
33 Correct 1 ms 212 KB Output is correct
34 Correct 2 ms 212 KB Output is correct
35 Correct 17 ms 304 KB Output is correct
36 Correct 1 ms 340 KB Output is correct
37 Correct 1 ms 212 KB Output is correct
38 Correct 1 ms 212 KB Output is correct
39 Correct 0 ms 212 KB Output is correct
40 Correct 1 ms 212 KB Output is correct
41 Correct 2 ms 212 KB Output is correct
42 Correct 17 ms 212 KB Output is correct
43 Correct 1 ms 340 KB Output is correct
44 Correct 1 ms 340 KB Output is correct
45 Correct 1 ms 340 KB Output is correct
46 Correct 1 ms 340 KB Output is correct
47 Correct 1 ms 340 KB Output is correct
48 Correct 1 ms 212 KB Output is correct
49 Correct 7 ms 212 KB Output is correct
50 Correct 8 ms 212 KB Output is correct
51 Correct 9 ms 308 KB Output is correct
52 Correct 1 ms 212 KB Output is correct
53 Correct 1 ms 212 KB Output is correct
54 Correct 10 ms 212 KB Output is correct
55 Correct 1 ms 256 KB Output is correct
56 Correct 1 ms 340 KB Output is correct
57 Correct 1 ms 340 KB Output is correct
58 Correct 1 ms 340 KB Output is correct
59 Correct 1 ms 340 KB Output is correct
60 Correct 1 ms 340 KB Output is correct
61 Correct 1 ms 212 KB Output is correct
62 Correct 32 ms 212 KB Output is correct
63 Correct 35 ms 300 KB Output is correct
64 Correct 33 ms 212 KB Output is correct
65 Correct 30 ms 624 KB Output is correct
66 Correct 31 ms 628 KB Output is correct
67 Correct 33 ms 520 KB Output is correct
68 Correct 29 ms 596 KB Output is correct
69 Correct 3 ms 212 KB Output is correct
70 Correct 42 ms 644 KB Output is correct
71 Correct 38 ms 584 KB Output is correct
72 Correct 39 ms 548 KB Output is correct
73 Correct 36 ms 636 KB Output is correct
74 Correct 36 ms 616 KB Output is correct
75 Correct 35 ms 544 KB Output is correct
76 Correct 35 ms 596 KB Output is correct
77 Correct 87 ms 932 KB Output is correct
78 Correct 87 ms 1036 KB Output is correct
79 Correct 80 ms 856 KB Output is correct
80 Correct 78 ms 872 KB Output is correct
81 Correct 81 ms 908 KB Output is correct
82 Correct 76 ms 844 KB Output is correct
83 Correct 75 ms 928 KB Output is correct
84 Correct 71 ms 892 KB Output is correct
85 Correct 6 ms 212 KB Output is correct
86 Correct 112 ms 884 KB Output is correct
87 Correct 117 ms 1080 KB Output is correct
88 Correct 102 ms 904 KB Output is correct
89 Correct 97 ms 932 KB Output is correct
90 Correct 97 ms 916 KB Output is correct
91 Correct 93 ms 848 KB Output is correct
92 Correct 96 ms 920 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 212 KB Output is correct
2 Correct 1 ms 212 KB Output is correct
3 Correct 0 ms 212 KB Output is correct
4 Correct 0 ms 212 KB Output is correct
5 Correct 0 ms 212 KB Output is correct
6 Correct 0 ms 212 KB Output is correct
7 Correct 0 ms 212 KB Output is correct
8 Correct 0 ms 212 KB Output is correct
9 Correct 0 ms 212 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 0 ms 212 KB Output is correct
16 Correct 0 ms 212 KB Output is correct
17 Correct 0 ms 212 KB Output is correct
18 Correct 0 ms 212 KB Output is correct
19 Correct 1 ms 212 KB Output is correct
20 Correct 0 ms 212 KB Output is correct
21 Correct 0 ms 212 KB Output is correct
22 Correct 1 ms 212 KB Output is correct
23 Correct 2 ms 212 KB Output is correct
24 Correct 7 ms 304 KB Output is correct
25 Correct 0 ms 212 KB Output is correct
26 Correct 0 ms 212 KB Output is correct
27 Correct 1 ms 212 KB Output is correct
28 Correct 1 ms 212 KB Output is correct
29 Correct 7 ms 212 KB Output is correct
30 Correct 62 ms 300 KB Output is correct
31 Correct 0 ms 212 KB Output is correct
32 Correct 1 ms 212 KB Output is correct
33 Correct 1 ms 212 KB Output is correct
34 Correct 2 ms 212 KB Output is correct
35 Correct 17 ms 304 KB Output is correct
36 Correct 1 ms 340 KB Output is correct
37 Correct 1 ms 212 KB Output is correct
38 Correct 1 ms 212 KB Output is correct
39 Correct 0 ms 212 KB Output is correct
40 Correct 1 ms 212 KB Output is correct
41 Correct 2 ms 212 KB Output is correct
42 Correct 17 ms 212 KB Output is correct
43 Correct 1 ms 340 KB Output is correct
44 Correct 1 ms 340 KB Output is correct
45 Correct 1 ms 340 KB Output is correct
46 Correct 1 ms 340 KB Output is correct
47 Correct 1 ms 340 KB Output is correct
48 Correct 1 ms 212 KB Output is correct
49 Correct 7 ms 212 KB Output is correct
50 Correct 8 ms 212 KB Output is correct
51 Correct 9 ms 308 KB Output is correct
52 Correct 1 ms 212 KB Output is correct
53 Correct 1 ms 212 KB Output is correct
54 Correct 10 ms 212 KB Output is correct
55 Correct 1 ms 256 KB Output is correct
56 Correct 1 ms 340 KB Output is correct
57 Correct 1 ms 340 KB Output is correct
58 Correct 1 ms 340 KB Output is correct
59 Correct 1 ms 340 KB Output is correct
60 Correct 1 ms 340 KB Output is correct
61 Correct 1 ms 212 KB Output is correct
62 Correct 32 ms 212 KB Output is correct
63 Correct 35 ms 300 KB Output is correct
64 Correct 33 ms 212 KB Output is correct
65 Correct 30 ms 624 KB Output is correct
66 Correct 31 ms 628 KB Output is correct
67 Correct 33 ms 520 KB Output is correct
68 Correct 29 ms 596 KB Output is correct
69 Correct 3 ms 212 KB Output is correct
70 Correct 42 ms 644 KB Output is correct
71 Correct 38 ms 584 KB Output is correct
72 Correct 39 ms 548 KB Output is correct
73 Correct 36 ms 636 KB Output is correct
74 Correct 36 ms 616 KB Output is correct
75 Correct 35 ms 544 KB Output is correct
76 Correct 35 ms 596 KB Output is correct
77 Correct 87 ms 932 KB Output is correct
78 Correct 87 ms 1036 KB Output is correct
79 Correct 80 ms 856 KB Output is correct
80 Correct 78 ms 872 KB Output is correct
81 Correct 81 ms 908 KB Output is correct
82 Correct 76 ms 844 KB Output is correct
83 Correct 75 ms 928 KB Output is correct
84 Correct 71 ms 892 KB Output is correct
85 Correct 6 ms 212 KB Output is correct
86 Correct 112 ms 884 KB Output is correct
87 Correct 117 ms 1080 KB Output is correct
88 Correct 102 ms 904 KB Output is correct
89 Correct 97 ms 932 KB Output is correct
90 Correct 97 ms 916 KB Output is correct
91 Correct 93 ms 848 KB Output is correct
92 Correct 96 ms 920 KB Output is correct
93 Correct 2697 ms 5464 KB Output is correct
94 Correct 2886 ms 5520 KB Output is correct
95 Correct 2715 ms 5404 KB Output is correct
96 Correct 2342 ms 5348 KB Output is correct
97 Correct 2064 ms 5360 KB Output is correct
98 Correct 2406 ms 5588 KB Output is correct
99 Correct 244 ms 1492 KB Output is correct
100 Correct 244 ms 1572 KB Output is correct
101 Correct 224 ms 1472 KB Output is correct
102 Correct 215 ms 1584 KB Output is correct
103 Correct 1 ms 212 KB Output is correct
104 Correct 1 ms 212 KB Output is correct
105 Correct 24 ms 212 KB Output is correct
106 Correct 632 ms 2860 KB Output is correct
107 Correct 0 ms 212 KB Output is correct
108 Correct 53 ms 296 KB Output is correct
109 Correct 1793 ms 5420 KB Output is correct
110 Correct 1663 ms 5368 KB Output is correct
111 Correct 198 ms 1528 KB Output is correct
112 Correct 224 ms 1588 KB Output is correct
113 Correct 189 ms 1596 KB Output is correct
114 Correct 11 ms 308 KB Output is correct
115 Correct 305 ms 1588 KB Output is correct
116 Correct 329 ms 1656 KB Output is correct
117 Correct 296 ms 1560 KB Output is correct
118 Correct 292 ms 1636 KB Output is correct
119 Correct 255 ms 1600 KB Output is correct
120 Correct 286 ms 1688 KB Output is correct
121 Correct 252 ms 1600 KB Output is correct
122 Correct 617 ms 2840 KB Output is correct
123 Correct 538 ms 2768 KB Output is correct
124 Correct 561 ms 2880 KB Output is correct
125 Correct 579 ms 2868 KB Output is correct
126 Correct 528 ms 2764 KB Output is correct
127 Correct 531 ms 2860 KB Output is correct
128 Correct 511 ms 2932 KB Output is correct
129 Correct 524 ms 2796 KB Output is correct
130 Correct 502 ms 2952 KB Output is correct
131 Correct 24 ms 212 KB Output is correct
132 Correct 971 ms 2888 KB Output is correct
133 Correct 911 ms 2844 KB Output is correct
134 Correct 898 ms 2928 KB Output is correct
135 Correct 846 ms 2812 KB Output is correct
136 Correct 768 ms 2764 KB Output is correct
137 Correct 746 ms 2780 KB Output is correct
138 Correct 759 ms 2852 KB Output is correct
139 Correct 694 ms 2872 KB Output is correct
140 Correct 731 ms 2896 KB Output is correct
141 Correct 659 ms 2880 KB Output is correct
142 Correct 1552 ms 5524 KB Output is correct
143 Correct 1484 ms 5328 KB Output is correct
144 Correct 1542 ms 5388 KB Output is correct
145 Correct 1430 ms 5468 KB Output is correct
146 Correct 1449 ms 5364 KB Output is correct
147 Correct 1370 ms 5552 KB Output is correct
148 Correct 1585 ms 5612 KB Output is correct
149 Correct 1582 ms 5332 KB Output is correct
150 Correct 1551 ms 5428 KB Output is correct
151 Correct 1743 ms 5440 KB Output is correct
152 Correct 1732 ms 5448 KB Output is correct
153 Correct 2406 ms 5412 KB Output is correct
154 Correct 2248 ms 5436 KB Output is correct
155 Correct 2126 ms 5548 KB Output is correct
156 Correct 2178 ms 5504 KB Output is correct
157 Correct 2082 ms 5436 KB Output is correct
158 Correct 2078 ms 5508 KB Output is correct
159 Correct 1971 ms 5516 KB Output is correct
160 Correct 1837 ms 5620 KB Output is correct
161 Correct 1795 ms 5476 KB Output is correct
162 Correct 1735 ms 5420 KB Output is correct
163 Correct 1745 ms 5480 KB Output is correct
164 Correct 1662 ms 5460 KB Output is correct
165 Correct 1664 ms 5320 KB Output is correct
166 Correct 1739 ms 5420 KB Output is correct
167 Correct 1724 ms 5408 KB Output is correct