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 <queue>
#include <iostream>
#include <vector>
#include <cstring>
using namespace std;
typedef long long ll;
const int maxn = 3e5 + 10;
vector<int> graph[maxn];
ll dp[maxn][1 << 5];
int color[maxn];
ll dfs(int node, int bitmask, int length) {
if(dp[node][bitmask] != -1) {
return dp[node][bitmask];
}
ll res = 0;
if(dp[node][bitmask] != -1) {
return dp[node][bitmask];
}
if(length >= 2) {
res++;
}
for(int neighbour : graph[node]) {
if(!(bitmask & (1 << color[neighbour]))) {
res += dfs(neighbour, bitmask | (1 << color[neighbour]), length + 1);
}
}
return dp[node][bitmask] = res;
}
int main(int argc, const char * argv[]) {
int n, m, k;
cin >> n >> m >> k;
for(int i = 0; i < n; i++) {
cin >> color[i];
color[i]--;
}
for(int i = 0; i < m; i++) {
int a, b;
cin >> a >> b;
a--;
b--;
graph[a].push_back(b);
graph[b].push_back(a);
}
memset(dp, -1, sizeof dp);
ll res = 0;
for(int i = 0; i < n; i++) {
res += dfs(i, (1 << color[i]), 1);
}
cout << res << endl;
return 0;
}
//4 3 3 1 2 1 3 1 2
//2 3
//4 2
# | 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... |