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;
#define int long long
#define double long double
#define FOR(i, l, r, d) for(int i=(l); i<=(r); i+=(d))
#define szof(x) ((int)(x).size())
#define vi vector<int>
#define pii pair<int,int>
#define F first
#define S second
#define pb push_back
#define eb emplace_back
#define mkp make_pair
const int INF = 2147483647;
const int LNF = INF*INF;
const int MOD = 1000000007;
const int mod = 998244353;
//#pragma GCC optimize("Ofast")
//#pragma GCC optimize("unroll-loops")
const int MAX = 300010;
int n, m, k;
vi edge[MAX];
int color[MAX];
int cnt[MAX];
vector<vi> per;
void Enum(vi arr){
if(szof(arr)>=2) per.pb(arr);
if(szof(arr) == k){
return;
}
FOR(i, 1, k, 1){
bool flag = 1;
for(int j : arr){
if(j==i) flag=0;
}
if(flag){
arr.pb(i);
Enum(arr);
arr.pop_back();
}
}
}
int solve(vi arr){
FOR(i, 1, n, 1){
cnt[i] = (color[i]==arr[0]) ? 1 : 0;
}
FOR(i, 1, szof(arr)-1, 1){
int pc = arr[i-1]; /// previous color
int cc = arr[i]; /// current color
FOR(u, 1, n, 1){
for(int v : edge[u]){
if(color[u] == pc and color[v] == cc){
cnt[v] += cnt[u];
}
}
}
}
int ret = 0;
FOR(i, 1, n, 1){
if(color[i] == arr.back()){
ret += cnt[i];
}
}
/*
cerr<<"solve(";
for(int i : arr){
cerr<<i;
}
cerr<<") = "<<ret<<endl;
*/
return ret;
}
signed main(){
ios_base::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
/// input
cin>>n>>m>>k;
FOR(i, 1, n, 1){
cin>>color[i];
}
FOR(i, 1, m, 1){
int u, v;
cin>>u>>v;
edge[u].pb(v);
edge[v].pb(u);
}
/// permutations
FOR(i, 1, k, 1){
Enum({i});
}
/// solve
int res = 0;
for(vi arr : per){
res += solve(arr);
}
cout<<res<<'\n';
return 0;
}
# | 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... |