#include <iostream>
#include <vector>
#include <cstring>
using namespace std;
using ll = long long;
int n, m, k, c[300005];
vector<vector<int>> g;
ll solve() {
ll ans = 0;
if (k < 2) return ans;
ll dp_1[n][k];
memset(dp_1, 0, sizeof(dp_1));
for (int u = 0; u < n; u++) {
for (int v : g[u]) {
dp_1[u][c[v]] += 1;
}
for (int c1 = 0; c1 < k; c1++) {
if (c1 == c[u]) continue;
ans += dp_1[u][c1];
}
}
if (k < 3) return ans;
ll dp_2[n][k][k];
memset(dp_2, 0, sizeof(dp_2));
for (int u = 0; u < n; u++) {
for (int v : g[u]) {
for (int c1 = 0; c1 < k; c1++) {
dp_2[u][c[v]][c1] += dp_1[v][c1];
}
}
for (int c1 = 0; c1 < k; c1++) {
if (c1 == c[u]) continue;
for (int c2 = 0; c2 < k; c2++) {
if (c2 == c[u] or c2 == c1) continue;
ans += dp_2[u][c1][c2];
}
}
}
if (k < 4) return ans;
ll dp_3[n][k][k][k];
memset(dp_3, 0, sizeof(dp_3));
for (int u = 0; u < n; u++) {
for (int v : g[u]) {
for (int c1 = 0; c1 < k; c1++) {
for (int c2 = 0; c2 < k; c2++) {
dp_3[u][c[v]][c1][c2] += dp_2[v][c1][c2];
}
}
}
for (int c1 = 0; c1 < k; c1++) {
if (c1 == c[u]) continue;
for (int c2 = 0; c2 < k; c2++) {
if (c2 == c[u] or c2 == c1) continue;
for (int c3 = 0; c3 < k; c3++) {
if (c3 == c[u] or c3 == c1 or c3 == c2) continue;
ans += dp_3[u][c1][c2][c3];
}
}
}
}
if (k < 5) return ans;
ll dp_4[n][k][k][k][k];
memset(dp_4, 0, sizeof(dp_4));
for (int u = 0; u < n; u++) {
for (int v : g[u]) {
for (int c1 = 0; c1 < k; c1++) {
for (int c2 = 0; c2 < k; c2++) {
for (int c3 = 0; c3 < k; c3++) {
dp_4[u][c[v]][c1][c2][c3] += dp_3[v][c1][c2][c3];
}
}
}
}
for (int c1 = 0; c1 < k; c1++) {
if (c1 == c[u]) continue;
for (int c2 = 0; c2 < k; c2++) {
if (c2 == c[u] or c2 == c1) continue;
for (int c3 = 0; c3 < k; c3++) {
if (c3 == c[u] or c3 == c2 or c3 == c1) continue;
for (int c4 = 0; c4 < k; c4++) {
if (c4 == c[u] or c4 == c3 or c4 == c2 or c4 == c1) continue;
ans += dp_4[u][c1][c2][c3][c4];
}
}
}
}
}
return ans;
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cin >> n >> m >> k;
g.resize(n);
for (int i = 0; i < n; i++) {
cin >> c[i];
c[i]--;
}
while (m--) {
int u, v;
cin >> u >> v;
u--, v--;
g[u].push_back(v);
g[v].push_back(u);
}
cout << solve() << endl;
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
340 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 |
340 KB |
Output is correct |
7 |
Correct |
0 ms |
340 KB |
Output is correct |
8 |
Correct |
0 ms |
340 KB |
Output is correct |
9 |
Correct |
1 ms |
212 KB |
Output is correct |
10 |
Correct |
0 ms |
212 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
59 ms |
4572 KB |
Output is correct |
2 |
Correct |
46 ms |
3920 KB |
Output is correct |
3 |
Correct |
270 ms |
45060 KB |
Output is correct |
4 |
Correct |
79 ms |
5964 KB |
Output is correct |
5 |
Correct |
69 ms |
5564 KB |
Output is correct |
6 |
Correct |
171 ms |
31680 KB |
Output is correct |
7 |
Correct |
257 ms |
45044 KB |
Output is correct |
8 |
Correct |
258 ms |
45740 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
340 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 |
340 KB |
Output is correct |
7 |
Correct |
0 ms |
340 KB |
Output is correct |
8 |
Correct |
0 ms |
340 KB |
Output is correct |
9 |
Correct |
1 ms |
212 KB |
Output is correct |
10 |
Correct |
0 ms |
212 KB |
Output is correct |
11 |
Correct |
59 ms |
4572 KB |
Output is correct |
12 |
Correct |
46 ms |
3920 KB |
Output is correct |
13 |
Correct |
270 ms |
45060 KB |
Output is correct |
14 |
Correct |
79 ms |
5964 KB |
Output is correct |
15 |
Correct |
69 ms |
5564 KB |
Output is correct |
16 |
Correct |
171 ms |
31680 KB |
Output is correct |
17 |
Correct |
257 ms |
45044 KB |
Output is correct |
18 |
Correct |
258 ms |
45740 KB |
Output is correct |
19 |
Correct |
62 ms |
4472 KB |
Output is correct |
20 |
Correct |
48 ms |
3952 KB |
Output is correct |
21 |
Correct |
265 ms |
45080 KB |
Output is correct |
22 |
Correct |
94 ms |
5928 KB |
Output is correct |
23 |
Correct |
84 ms |
5568 KB |
Output is correct |
24 |
Correct |
171 ms |
31656 KB |
Output is correct |
25 |
Correct |
259 ms |
45032 KB |
Output is correct |
26 |
Correct |
278 ms |
45708 KB |
Output is correct |
27 |
Correct |
54 ms |
4472 KB |
Output is correct |
28 |
Correct |
84 ms |
10304 KB |
Output is correct |
29 |
Correct |
450 ms |
214156 KB |
Output is correct |
30 |
Correct |
258 ms |
108780 KB |
Output is correct |
31 |
Correct |
291 ms |
108940 KB |
Output is correct |
32 |
Correct |
444 ms |
214292 KB |
Output is correct |
33 |
Correct |
1 ms |
340 KB |
Output is correct |
34 |
Correct |
1 ms |
340 KB |
Output is correct |
35 |
Correct |
1 ms |
212 KB |
Output is correct |
36 |
Correct |
0 ms |
212 KB |
Output is correct |
37 |
Correct |
0 ms |
212 KB |
Output is correct |
38 |
Correct |
1 ms |
340 KB |
Output is correct |
39 |
Correct |
0 ms |
340 KB |
Output is correct |
40 |
Correct |
0 ms |
340 KB |
Output is correct |
41 |
Correct |
0 ms |
212 KB |
Output is correct |
42 |
Correct |
0 ms |
212 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
42 ms |
4300 KB |
Output is correct |
3 |
Correct |
17 ms |
1492 KB |
Output is correct |
4 |
Correct |
58 ms |
15240 KB |
Output is correct |
5 |
Correct |
50 ms |
15868 KB |
Output is correct |
6 |
Correct |
525 ms |
616520 KB |
Output is correct |
7 |
Correct |
18 ms |
1644 KB |
Output is correct |
8 |
Correct |
124 ms |
71572 KB |
Output is correct |
9 |
Correct |
103 ms |
72340 KB |
Output is correct |
10 |
Correct |
107 ms |
72204 KB |
Output is correct |
11 |
Correct |
282 ms |
308764 KB |
Output is correct |
12 |
Correct |
366 ms |
462932 KB |
Output is correct |
13 |
Correct |
307 ms |
308800 KB |
Output is correct |
14 |
Correct |
523 ms |
616512 KB |
Output is correct |
15 |
Correct |
533 ms |
616504 KB |
Output is correct |
16 |
Correct |
0 ms |
340 KB |
Output is correct |
17 |
Correct |
0 ms |
340 KB |
Output is correct |
18 |
Correct |
0 ms |
212 KB |
Output is correct |
19 |
Correct |
0 ms |
212 KB |
Output is correct |
20 |
Correct |
0 ms |
212 KB |
Output is correct |
21 |
Correct |
0 ms |
340 KB |
Output is correct |
22 |
Correct |
1 ms |
340 KB |
Output is correct |
23 |
Correct |
1 ms |
340 KB |
Output is correct |
24 |
Correct |
0 ms |
212 KB |
Output is correct |
25 |
Correct |
0 ms |
212 KB |
Output is correct |