This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#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;
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |