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("Ofast,unroll-loops")
#pragma GCC target("popcnt")
typedef long long ll;
#define pb push_back
#define F first
#define S second
#define all(x) x.begin(),x.end()
#define sz(x) int(x.size())
const int N = 3e5, N2 = 1e5;
int n, m, k, col[N];
ll dp[N][(1 << 4)], dp2[N2][(1 << 5)], ans = 0;
vector<int> adj[N];
ll sol(int u, int msk) {
ll &ret = dp[u][msk];
if (~ret)return ret;
ret = 1;
msk |= (1 << col[u]);
for (auto &v : adj[u]) {
if ((msk & (1 << col[v])))continue;
ret = ret + sol(v, msk);
}
return ret;
}
ll sol2(int u, int msk) {
ll &ret = dp2[u][msk];
if (~ret)return ret;
ret = 1;
msk |= (1 << col[u]);
for (auto &v : adj[u]) {
if ((msk & (1 << col[v])))continue;
ret = ret + sol2(v, msk);
}
return ret;
}
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
cin >> n >> m >> k;
for (int i = 0; i < n; i++) {
cin >> col[i];
--col[i];
}
for (int i = 0; i < m; i++) {
int u, v;
cin >> u >> v;
--u, --v;
adj[u].pb(v);
adj[v].pb(u);
}
memset(dp, -1, sizeof dp);
memset(dp2, -1, sizeof dp2);
if (n > int(1e5)) {
for (int i = 0; i < n; i++) {
ans = ans + sol(i, 0);
}
} else {
for (int i = 0; i < n; i++) {
ans = ans + sol2(i, 0);
}
}
cout << ans - ll(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... |