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;
#pragma GCC optimize ("unroll-loops,Ofast,O3")
#pragma GCC target("avx,avx2,fma")
#define F first
#define S second
#define sz(x) (int)x.size()
#define pb push_back
#define eb emplace_back
#define rall(x) (x).rbegin(),(x).rend()
#define NFS ios_base::sync_with_stdio(0) , cin.tie(0) , cout.tie(0) ;
#define file(s) if (fopen(s".in", "r")) freopen(s".in", "r", stdin), freopen(s".out", "w", stdout)
typedef long long ll;
const int E = 1e6+777;
const long long inf = 1e18+777;
const int N = 3e5+324;
const int MOD = 1e9+7;
long long res;
int n, m, k, c[N];
vector<int> g[N];
long long cnt[7][N][33];
void Count(int v, int x){
for(auto to : g[v]){
if(c[to] == c[v]) continue;
// cerr << "to: " << to << "\n";
for(int mask = 0; mask < (1 << k); ++mask){
if(((mask >> c[v]) & 1)) continue;
// cerr << mask << " <-> "<< (mask|(1<<c[v])) << " -> " << cnt[x-1][to][mask] << "\n";
cnt[x][v][mask|(1<<c[v])] += cnt[x-1][to][mask];
};
};
}
void go(int x){
for(int i = 1; i <= n; ++i){
// cerr << "count(" << i << "," << x << ")\n";
Count(i, x);
}
}
int main(){
cin >> n >> m >> k;
for(int i = 1; i <= n; ++i){
cin >> c[i];
--c[i];
cnt[1][i][(1<<c[i])] = 1;
// cerr << cnt[1][i][(1<<c[i])] << '\n';
}
for(int i = 1; i <= m; ++i){
int x, y;
cin >> x >> y;
g[x].pb(y);
g[y].pb(x);
}
// cerr << "1<<k: " << (1 << k) << '\n';
for(int i = 2; i <= k; ++i){
// cerr << "k -> " << i << "\n";
go(i);
}
long long sum = 0;
for(int kk = 1; kk <= k; ++kk){
// cerr << "K: " << kk << "\n";
for(int i = 1; i <= n; ++i){
long long sm = 0;
for(int j = 0; j < (1 << k); ++j){
sm += cnt[kk][i][j];
}
sum += sm;
// cerr << i << " -> " << sm << "\n";
}
}
cout << sum - 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... |