이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include <bits/stdc++.h>
#define speed ios_base::sync_with_stdio(0); cin.tie(0)
#define all(x) (x).begin(),(x).end()
#define F first
#define S second
namespace{using namespace std;}
typedef long long ll;
typedef double db;
typedef long double ldb;
typedef pair<int,int> pii;
typedef pair<ll,ll> pll;
const ll MAX=3e5+10+10,P=1e9+7;
const ll INF=0x3f3f3f3f,oo=0x3f3f3f3f3f3f3f3f;
int n,m,k;
int clr[MAX];
vector<int> G[MAX];
ll dp[MAX][(1<<4)+10];
int main() {
speed;
cin>>n>>m>>k;
for (int i=1;i<=n;i++) {
cin>>clr[i];
clr[i]--;
}
for (int i=0;i<m;i++) {
int a,b;
cin>>a>>b;
G[a].push_back(b);
G[b].push_back(a);
}
memset(dp,0,sizeof(dp));
for (int i=1;i<=n;i++) {
for (int j:G[i]) {
if (clr[i]==clr[j]) continue;
dp[i][(1<<clr[j])]++;
}
}
for (int i=1;i<=n;i++) {
for (int j:G[i]) {
if (clr[i]==clr[j]) continue;
for (int c=0;c<k;c++) {
if (clr[i]==c) continue;
dp[i][(1<<c)|(1<<clr[j])]+=dp[j][(1<<c)];
}
}
}
// for (int i=1;i<=n;i++) {
// for (int s=0;s<(1<<k);s++) {
// cout<<i<<" -> ";
// for (int j=0;j<k;j++) {
// if (s&(1<<j)) cout<<j<<" ";
// }
// cout<<"-> "<<dp[i][s]<<"\n";
// }
// }
ll ans=0;
if (k>=2) {
for (int i=1;i<=n;i++) {
for (int c=0;c<k;c++) {
if (c==clr[i]) continue;
ans+=dp[i][(1<<c)];
}
}
}
// cout<<ans<<"\n";
if (k>=3) {
for (int i=1;i<=n;i++) {
for (int c1=0;c1<k;c1++) {
if (c1==clr[i]) continue;
for (int c2=c1+1;c2<k;c2++) {
if (c2==clr[i]) continue;
ans+=dp[i][(1<<c1)|(1<<c2)];
}
}
}
}
// cout<<ans<<"\n";
if (k>=4) {
for (int i=1;i<=n;i++) {
for (int s=7;s<(1<<k);s++) {
if (__builtin_popcount(s)!=3) continue;
if (s&(1<<clr[i])) continue;
for (int c1=0;c1<k;c1++) {
if (c1==clr[i]) continue;
if (!(s&(1<<c1))) continue;
ans+=dp[i][(1<<c1)]*dp[i][s^(1<<c1)];
}
}
}
}
// cout<<ans<<"\n";
if (k>=5) {
for (int i=1;i<=n;i++) {
int s=((1<<k)-1)^(1<<clr[i]);
for (int c1=0;c1<k;c1++) {
if (c1==clr[i]) continue;
for (int c2=c1+1;c2<k;c2++) {
if (c2==clr[i]) continue;
ans+=dp[i][s^(1<<c1)^(1<<c2)]*dp[i][(1<<c1)|(1<<c2)];
// cout<<i<<" "<<c1<<" "<<c2<<" "<<dp[i][s^(1<<c1)^(1<<c2)]<<" "<<dp[i][(1<<c1)|(1<<c2)]<<"\n";
}
}
}
}
cout<<ans<<"\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... |