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 <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef vector<int> vi;
typedef vector<vi> vvi;
ll dp[350050][32];
int n, m, k, colours[350050];
vvi adj;
ll dfs(int u, int mask) {
if (dp[u][mask] != -1) return dp[u][mask];
//cerr << "// dfs(" << u << ", " << mask << ")\n";
mask |= (1 << colours[u]);
ll pathsCount = 0;
for (int v : adj[u]) {
if ((1 << colours[v]) & mask) continue;
pathsCount += 1 + dfs(v, mask);
}
mask ^= (1 << colours[u]);
dp[u][mask] = pathsCount;
return dp[u][mask];
}
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cin >> n >> m >> k;
adj.assign(n + 20, vi());
for (int i = 0; i < n; i++) {
cin >> colours[i];
colours[i]--;
}
for (int i = 0; i < m; i++) {
int a, b;
cin >> a >> b;
a--; b--;
adj[a].push_back(b);
adj[b].push_back(a);
}
memset(dp, -1, sizeof dp);
ll ans = 0;
for (int i = 0; i < n; i++) {
ans += dfs(i, 0);
}
cout << ans << '\n';
}
# | 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... |