Submission #518381

#TimeUsernameProblemLanguageResultExecution timeMemory
518381IerusPaths (BOI18_paths)C++17
100 / 100
658 ms331388 KiB
#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 timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...