제출 #1183974

#제출 시각아이디문제언어결과실행 시간메모리
1183974alterioPaths (BOI18_paths)C++20
70 / 100
172 ms92744 KiB
#include <bits/stdc++.h>

#pragma GCC target("avx2")
#pragma GCC optimize("O3")
#pragma GCC optimize("Ofast,unroll-loops")

using namespace std;

#define endl '\n'
#define ll long long
#define all(x) (x).begin(), (x).end()

const int mxn = 3e5 + 10, mxk = 5;

vector<int> g[mxn], val(mxn), masks[5];

ll dp[mxn][1 << mxk];

int main() {
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    int n, m, k;
    cin >> n >> m >> k;
    for (int i = 1; i <= n; i++) cin >> val[i], val[i]--;
    for (int i = 0; i < m; i++) {
        int f, t;
        cin >> f >> t;
        g[f].push_back(t);
        g[t].push_back(f);
    }
    for (int i = 1; i < (1 << k); i++) masks[__builtin_popcount(i)].push_back(i);
    for (int i = 1; i <= n; i++) dp[i][1 << val[i]] = 1;
    for (int i = 1; i < k; i++) {
        for (int j = 1; j <= n; j++) {
            for (auto mask : masks[i]) {
                if (!dp[j][mask]) continue;
                for (auto to : g[j]) {
                    if (!((1 << val[to]) & mask)) {
                        int newMask = mask | (1 << val[to]);
                        dp[to][newMask] += dp[j][mask];
                    }
                }
            }
        }
    }
    ll ans = 0;
    for (int i = 1; i <= n; i++) {
        for (int j = 2; j <= k; j++) {
            for (auto mask : masks[j]) {
                ans += dp[i][mask];
            }
        }
    }
    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...