#include <bits/stdc++.h>
using namespace std;
using ll = long long;
#define MAXN (300005)
ll N,M,K;
vector<ll> v[MAXN];
ll colour[MAXN], memo[MAXN][(1ll<<5) + 1];
ll dp(ll x,ll bitmask){
if(memo[x][bitmask] != -1) return memo[x][bitmask];
ll sum = 0;
if(__builtin_popcountll(bitmask) >= 2) sum = 1; //the problem statement defines that a path must consist of at least 2 nodes
for(auto u : v[x]){
if((bitmask & (1ll<<colour[u])) == 0){
sum += dp(u,bitmask | (1ll<<colour[u]));
}
}
memo[x][bitmask] = sum;
return memo[x][bitmask];
}
int main() {
ios_base::sync_with_stdio(false);cin.tie(0);
cin>>N>>M>>K;
for(ll i = 0;i < N;i++){
cin>>colour[i];
colour[i]--;
}
for(ll i = 0;i < M;i++){
ll a,b;
cin>>a>>b;
a--, b--;
v[a].push_back(b);
v[b].push_back(a);
}
memset(memo,-1,sizeof(memo));
ll ans = 0;
for(ll x = 0;x < N;x++){
ans += dp(x,1ll<<colour[x]);
}
cout<<ans<<'\n';
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
16 ms |
87132 KB |
Output is correct |
2 |
Correct |
15 ms |
87132 KB |
Output is correct |
3 |
Correct |
14 ms |
87284 KB |
Output is correct |
4 |
Correct |
16 ms |
87132 KB |
Output is correct |
5 |
Correct |
15 ms |
87132 KB |
Output is correct |
6 |
Correct |
15 ms |
87128 KB |
Output is correct |
7 |
Correct |
15 ms |
87132 KB |
Output is correct |
8 |
Correct |
16 ms |
87128 KB |
Output is correct |
9 |
Correct |
15 ms |
87340 KB |
Output is correct |
10 |
Correct |
15 ms |
87132 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
66 ms |
94288 KB |
Output is correct |
2 |
Correct |
57 ms |
94384 KB |
Output is correct |
3 |
Correct |
258 ms |
97852 KB |
Output is correct |
4 |
Correct |
84 ms |
95316 KB |
Output is correct |
5 |
Correct |
74 ms |
95316 KB |
Output is correct |
6 |
Correct |
180 ms |
96104 KB |
Output is correct |
7 |
Correct |
313 ms |
98000 KB |
Output is correct |
8 |
Correct |
341 ms |
98392 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
16 ms |
87132 KB |
Output is correct |
2 |
Correct |
15 ms |
87132 KB |
Output is correct |
3 |
Correct |
14 ms |
87284 KB |
Output is correct |
4 |
Correct |
16 ms |
87132 KB |
Output is correct |
5 |
Correct |
15 ms |
87132 KB |
Output is correct |
6 |
Correct |
15 ms |
87128 KB |
Output is correct |
7 |
Correct |
15 ms |
87132 KB |
Output is correct |
8 |
Correct |
16 ms |
87128 KB |
Output is correct |
9 |
Correct |
15 ms |
87340 KB |
Output is correct |
10 |
Correct |
15 ms |
87132 KB |
Output is correct |
11 |
Correct |
66 ms |
94288 KB |
Output is correct |
12 |
Correct |
57 ms |
94384 KB |
Output is correct |
13 |
Correct |
258 ms |
97852 KB |
Output is correct |
14 |
Correct |
84 ms |
95316 KB |
Output is correct |
15 |
Correct |
74 ms |
95316 KB |
Output is correct |
16 |
Correct |
180 ms |
96104 KB |
Output is correct |
17 |
Correct |
313 ms |
98000 KB |
Output is correct |
18 |
Correct |
341 ms |
98392 KB |
Output is correct |
19 |
Correct |
70 ms |
94292 KB |
Output is correct |
20 |
Correct |
58 ms |
94452 KB |
Output is correct |
21 |
Correct |
307 ms |
97856 KB |
Output is correct |
22 |
Correct |
82 ms |
95468 KB |
Output is correct |
23 |
Correct |
74 ms |
95312 KB |
Output is correct |
24 |
Correct |
184 ms |
96444 KB |
Output is correct |
25 |
Correct |
325 ms |
97616 KB |
Output is correct |
26 |
Correct |
253 ms |
98400 KB |
Output is correct |
27 |
Correct |
67 ms |
94292 KB |
Output is correct |
28 |
Correct |
85 ms |
95824 KB |
Output is correct |
29 |
Correct |
328 ms |
97620 KB |
Output is correct |
30 |
Correct |
273 ms |
95776 KB |
Output is correct |
31 |
Correct |
262 ms |
96036 KB |
Output is correct |
32 |
Correct |
334 ms |
97872 KB |
Output is correct |
33 |
Correct |
16 ms |
87316 KB |
Output is correct |
34 |
Correct |
14 ms |
87132 KB |
Output is correct |
35 |
Correct |
15 ms |
87132 KB |
Output is correct |
36 |
Correct |
15 ms |
87328 KB |
Output is correct |
37 |
Correct |
15 ms |
87132 KB |
Output is correct |
38 |
Correct |
15 ms |
87132 KB |
Output is correct |
39 |
Correct |
15 ms |
87236 KB |
Output is correct |
40 |
Correct |
16 ms |
87132 KB |
Output is correct |
41 |
Correct |
16 ms |
87132 KB |
Output is correct |
42 |
Correct |
15 ms |
87304 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
16 ms |
87124 KB |
Output is correct |
2 |
Correct |
41 ms |
89436 KB |
Output is correct |
3 |
Correct |
31 ms |
89424 KB |
Output is correct |
4 |
Correct |
72 ms |
90704 KB |
Output is correct |
5 |
Correct |
59 ms |
91076 KB |
Output is correct |
6 |
Correct |
110 ms |
90840 KB |
Output is correct |
7 |
Correct |
33 ms |
89436 KB |
Output is correct |
8 |
Correct |
86 ms |
91012 KB |
Output is correct |
9 |
Correct |
70 ms |
91076 KB |
Output is correct |
10 |
Correct |
75 ms |
90820 KB |
Output is correct |
11 |
Correct |
73 ms |
89804 KB |
Output is correct |
12 |
Correct |
67 ms |
90436 KB |
Output is correct |
13 |
Correct |
65 ms |
89924 KB |
Output is correct |
14 |
Correct |
92 ms |
90840 KB |
Output is correct |
15 |
Correct |
79 ms |
90712 KB |
Output is correct |
16 |
Correct |
15 ms |
87132 KB |
Output is correct |
17 |
Correct |
16 ms |
87316 KB |
Output is correct |
18 |
Correct |
15 ms |
87128 KB |
Output is correct |
19 |
Correct |
15 ms |
87128 KB |
Output is correct |
20 |
Correct |
15 ms |
87128 KB |
Output is correct |
21 |
Correct |
15 ms |
87128 KB |
Output is correct |
22 |
Correct |
15 ms |
87220 KB |
Output is correct |
23 |
Correct |
15 ms |
87132 KB |
Output is correct |
24 |
Correct |
15 ms |
87268 KB |
Output is correct |
25 |
Correct |
14 ms |
87132 KB |
Output is correct |