# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
870656 | TheSahib | Paths (BOI18_paths) | C++14 | 0 ms | 0 KiB |
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;
#define ll long long
#define pii pair<int, int>
#define oo 1e18
const int MAX = 3e5 + 5;
vector<int> g[MAX];
ll dp[33][MAX];
int c[MAX];
int n, m, k;
ll rec(int node, int mask){
mask |= (1 << c[node]);
if(dp[mask][node] != -1) return dp[mask][node];
ll ans = 0;
if(__popcount(mask) >= 2) ans++;
for(int to : g[node]){
if(mask & (1 << c[to])){
continue;
}
ans += rec(to, mask);
}
return dp[mask][node] = ans;
}
int main(){
memset(dp, -1, sizeof(dp));
cin >> n >> m >> k;
for(int i = 1; i <= n; i++){
cin >> c[i];
c[i]--;
}
for(int i = 1; i <= m; i++){
int a, b; cin >> a >> b;
g[a].push_back(b);
g[b].push_back(a);
}
ll ans = 0;
for(int i = 1; i <= n; i++){
ans += rec(i, 0);
}
cout << ans << '\n';
}