Submission #243531

#TimeUsernameProblemLanguageResultExecution timeMemory
243531AMO5Paths (BOI18_paths)C++17
100 / 100
584 ms97272 KiB
#include <bits/stdc++.h>

using namespace std;

#define fi first
#define se second
#define eb emplace_back
#define mt make_tuple
#define all(x) (x).begin(), (x).end() 
#define MOD 1000000007

typedef long long ll;
typedef pair <int, int> ii;
typedef pair <ll, ll> pll;
typedef vector<int> vi;
typedef vector<ll> vll;
typedef long double ld;

const ll INF=LLONG_MAX;
const int mxn=300111;
int n,m,k;
int color[mxn];
ll dp[mxn][32];
ll ans = 0ll;
vi adj[mxn];

ll solve(int u, int use){
	if(dp[u][use]!=-1ll)return dp[u][use];
	if((use>>color[u])&1){
		return dp[u][use]=0ll;
	}
	int bt = use;
	use |= 1<<color[u];
	ll res = 1ll;
	for(int v:adj[u]){
		res += solve(v,use);
	}
	if(bt==0)return dp[u][bt]=res-1;
	return dp[u][bt]=res;
}

int main()
{
    ios_base::sync_with_stdio(0); cin.tie(0);
    //freopen("input.txt","r",stdin); freopen("output.txt","w",stdout);
	memset(dp,-1ll,sizeof(dp));
	cin >> n >> m >> k;
	for(int i=0; i<n; i++){
		cin >> color[i];
		color[i]--;
	}
	int a,b;
	for(int i=0; i<m; i++){
		cin >> a >> b;
		a--; b--;
		adj[a].eb(b);
		adj[b].eb(a);
	}
	for(int i=0; i<n; i++){
		ans += solve(i,0);
	}
	cout << ans << '\n';
}
	
// READ & UNDERSTAND
// ll, int overflow, array bounds, memset(0)
// special cases (n=1?), n+1 (1-index)
// do smth instead of nothing & stay organized
// WRITE STUFF DOWN
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...