Submission #951932

#TimeUsernameProblemLanguageResultExecution timeMemory
951932SaMuEl0516Paths (BOI18_paths)C++17
100 / 100
237 ms36948 KiB
#include<bits/stdc++.h> using namespace std; int color[(int)3e5+5],k4[(int)3e5+5][10],k5[(int)1e5+5][35]; vector<int>cnt[(int)3e5+5]; pair<int,int>e[(int)3e5+5]; int main(){ cin.tie(0)->sync_with_stdio(0); int n,m,k; cin>>n>>m>>k; for(int i=1;i<=n;i++)cin>>color[i]; for(int i=0;i<m;i++){ cin>>e[i].first>>e[i].second; cnt[e[i].first].push_back(e[i].second),cnt[e[i].second].push_back(e[i].first); if(k>=5)k5[e[i].first][color[e[i].first]*5+color[e[i].second]]++,k5[e[i].second][color[e[i].second]*5+color[e[i].first]]++; } long long int ans=0; if(k>=2){ for(int i=0;i<m;i++)if(color[e[i].first]!=color[e[i].second])ans+=2; } if(k>=3){ for(int i=1;i<=n;i++){ int c[10]; for(int j=1;j<=k;j++)c[j]=0; for(int j:cnt[i])c[color[j]]++; for(int x=1;x<=k;x++)for(int y=1;y<=k;y++){ if(x==y||x==color[i]||y==color[i])continue; ans+=1ll*c[x]*c[y]; } } } if(k>=4){ for(int i=1;i<=n;i++){ for(int j=1;j<=k;j++)k4[i][j]=0; } for(int i=1;i<=n;i++){ for(int j:cnt[i])k4[i][color[j]]++; } for(int i=0;i<m;i++){ if(color[e[i].first]==color[e[i].second])continue; for(int x=1;x<=k;x++)for(int y=1;y<=k;y++){ if(x==y||x==color[e[i].first]||x==color[e[i].second]||y==color[e[i].first]||y==color[e[i].second])continue; ans+=2ll*k4[e[i].first][x]*k4[e[i].second][y]; } } } if(k>=5){ for(int i=1;i<=n;i++){ int c[35]; for(int j=6;j<=30;j++)c[j]=0; for(int j:cnt[i]){ for(int x=6;x<=30;x++)c[x]+=k5[j][x]; } for(int x=1;x<=k;x++)for(int y=1;y<=k;y++)for(int u=1;u<=k;u++)for(int v=1;v<=k;v++){ if(color[i]==x||color[i]==y||color[i]==u||color[i]==v ||x==y||x==u||x==v||y==u||y==v||u==v)continue; ans+=1ll*c[x*5+y]*c[u*5+v]; } } } cout<<ans; }
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...