Advertisement
Josif_tepe

Untitled

Aug 27th, 2023
1,478
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.25 KB | None | 0 0
  1. #include <queue>
  2. #include <iostream>
  3. #include <vector>
  4. #include <cstring>
  5. using namespace std;
  6. typedef long long ll;
  7. const int maxn = 3e5 + 10;
  8. vector<int> graph[maxn];
  9. ll dp[maxn][1 << 5];
  10. int color[maxn];
  11. ll dfs(int node, int bitmask, int length) {
  12.     if(dp[node][bitmask] != -1) {
  13.         return dp[node][bitmask];
  14.     }
  15.     ll res = 0;
  16.     if(dp[node][bitmask] != -1) {
  17.         return dp[node][bitmask];
  18.     }
  19.     if(length >= 2) {
  20.         res++;
  21.     }
  22.     for(int neighbour : graph[node]) {
  23.         if(!(bitmask & (1 << color[neighbour]))) {
  24.             res += dfs(neighbour, bitmask | (1 << color[neighbour]), length + 1);
  25.         }
  26.     }
  27.     return dp[node][bitmask] = res;
  28. }
  29. int main(int argc, const char * argv[]) {
  30.     int n, m, k;
  31.     cin >> n >> m >> k;
  32.    
  33.     for(int i = 0; i < n; i++) {
  34.         cin >> color[i];
  35.         color[i]--;
  36.     }
  37.     for(int i = 0; i < m; i++) {
  38.         int a, b;
  39.         cin >> a >> b;
  40.         a--;
  41.         b--;
  42.         graph[a].push_back(b);
  43.         graph[b].push_back(a);
  44.     }
  45.     memset(dp, -1, sizeof dp);
  46.     ll res = 0;
  47.     for(int i = 0; i < n; i++) {
  48.         res += dfs(i, (1 << color[i]), 1);
  49.     }
  50.     cout << res << endl;
  51.     return 0;
  52. }
  53.  
  54. //4 3 3 1 2 1 3 1 2
  55. //2 3
  56. //4 2
  57.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement