제출 #104788

#제출 시각아이디문제언어결과실행 시간메모리
104788dailiPaths (BOI18_paths)C++14
23 / 100
563 ms47588 KiB
#include <bits/stdc++.h>

using namespace std;

long long solve(vector<vector<int>> &adj, int current, int colsUsed, int K, vector<int> &colors, vector<vector<long long>> &dp)
{
    int cnt = 1;

    if (dp[current][colsUsed])
    {
      return dp[current][colsUsed];
    }

    for (auto u : adj[current])
    {
        if ((colsUsed & (1 << colors[u])) == 0)
        {
            cnt += solve(adj, u, colsUsed ^ (1 << colors[u]), K, colors, dp);
        }
    }
    dp[current][colsUsed] = cnt;
    return cnt;
}

int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(0);

    int n, m, K;
    cin >> n >> m >> K;

    vector<int> cols;
    for (int i = 0; i < n; i++)
    {
        int x;
        cin >> x;
        x--;

        cols.push_back(x);
    }

    vector<vector<int>> graph(n);
    for (int i = 0; i < m; i++)
    {
      int a, b;
      cin >> a >> b;

      a--; b--;
      graph[a].push_back(b);
      graph[b].push_back(a);
    }

    vector<vector<long long>> dp(n+1, vector<long long> (1 << K));

    int res = 0;
    for (int i = 0; i < n; i++)
    {
      res += solve(graph, i, (1 << cols[i]), K, cols, dp);
    }
    cout << res - n;


    return 0;
}
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...