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>
#define N 300005
#define K 5
using namespace std;
int n,m,k;
int arr[N];
long long dp[N][(1<<K)];
vector<int > adj[N];
long long f(int v,int mask){
if( (mask & (1<<arr[v])) == 0)return 0;
if(mask == (1<<arr[v]))return 1;
if(dp[v][mask] != -1)return dp[v][mask];
dp[v][mask] = 0;
for(auto u:adj[v]){
dp[v][mask] += f(u,mask^(1<<arr[v]));
}
return dp[v][mask];
}
void solve(){
for(int i=0;i<N;i++){
for(int j=0;j<(1<<K);j++){
dp[i][j] = -1;
}
}
cin >> n >> m >> k;
for(int i=1;i<=n;i++){
cin >> arr[i];
arr[i]--;
}
for(int i=0;i<m;i++){
int u,v;
cin >> u >> v;
adj[u].push_back(v);
adj[v].push_back(u);
}
long long ans = 0;
for(int i=1;i<=n;i++){
for(int j=1;j<(1<<k);j++){
ans += f(i,j);
}
}
cout << ans - n;
}
int32_t main(){
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
#ifdef Local
freopen("in.txt","r",stdin);
freopen("out.txt","w",stdout);
#endif
int t=1;
//cin>>t;
while(t--){
solve();
}
#ifdef Local
cout<<endl<<fixed<<setprecision(2)<<1000.0 * clock() / CLOCKS_PER_SEC<< " milliseconds ";
#endif
}
# | 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... |