Submission #880904

# Submission time Handle Problem Language Result Execution time Memory
880904 2023-11-30T08:29:44 Z TAhmed33 Šarenlist (COCI22_sarenlist) C++
110 / 110
20 ms 604 KB
#include <bits/stdc++.h>
using namespace std;
const int MOD = 1e9 + 7;
int add (int a, int b) {
    a += b; if (a >= MOD) a -= MOD;
    return a;
}
int sub (int a, int b) {
    a -= b; if (a < 0) a += MOD;
    return a;
}
int mul (int a, int b) {
    return (a * 1ll * b) % MOD;
}
int power (int a, int b) {
    if (!b) return 1;
    int u = power(a, b >> 1);
    u = mul(u, u);
    if (b & 1) u = mul(u, a);
    return u;
}
int n, m, k;
vector <pair <int, int>> adj[61];
pair <int, int> val[15];
vector <int> edges[51];
vector <int> st;
bool flag = 0;
void dfs (int pos, int par, int dist) {
    if (pos == dist) {
        flag = 1;
        return;
    }
    for (auto j : adj[pos]) {
        if (j.first == par) continue;
        st.push_back(j.second);
        dfs(j.first, pos, dist);
        if (flag) return;
        st.pop_back();
    }
}
struct DSU {
    int par[61], sze[61];
    void init () {
        for (int i = 1; i <= n - 1; i++) {
            sze[i] = 1; par[i] = i;
        }
    }
    int find (int x) {
        return par[x] == x ? x : par[x] = find(par[x]);
    }
    void merge (int x, int y) {
        x = find(x); y = find(y);
        if (x == y) return;
        if (sze[x] > sze[y]) swap(x, y);
        sze[y] += sze[x]; par[x] = y; sze[x] = 0;
    }
} cur;
int solve (vector <int> x) {
    cur.init();
    for (int i = 0; i < (int)x.size(); i++) {
        for (int j = 1; j < (int)edges[x[i]].size(); j++) {
            cur.merge(edges[x[i]][j], edges[x[i]][j - 1]);
        }
    }
    int ans = 1;
    int l = 0;
    for (int i = 1; i <= n - 1; i++) {
        l += cur.sze[i];
        if (cur.sze[i]) ans = mul(ans, k);
    }
    return ans;
}
int main () {
    cin >> n >> m >> k;
    for (int i = 1; i < n; i++) {
        int a, b;
        cin >> a >> b;
        adj[a].push_back({b, i});
        adj[b].push_back({a, i});
    }
    for (int i = 0; i < m; i++) {
        cin >> val[i].first >> val[i].second;
        flag = 0;
        dfs(val[i].first, -1, val[i].second);
        edges[i] = st;
        st.clear();
    }
    int ans = power(k, n - 1);
    for (int i = 1; i < (1 << m); i++) {
        vector <int> s;
        for (int j = 0; j < m; j++) if ((i >> j) & 1) s.push_back(j);
        if (__builtin_popcount(i) & 1) ans = sub(ans, solve(s));
        else ans = add(ans, solve(s));
    }
    cout << ans << '\n';
}
# Verdict Execution time Memory Grader output
1 Correct 1 ms 600 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 0 ms 348 KB Output is correct
4 Correct 0 ms 348 KB Output is correct
5 Correct 0 ms 348 KB Output is correct
6 Correct 0 ms 348 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 344 KB Output is correct
2 Correct 1 ms 348 KB Output is correct
3 Correct 0 ms 348 KB Output is correct
4 Correct 0 ms 348 KB Output is correct
5 Correct 0 ms 348 KB Output is correct
6 Correct 0 ms 444 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 348 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 0 ms 348 KB Output is correct
4 Correct 0 ms 348 KB Output is correct
5 Correct 2 ms 348 KB Output is correct
6 Correct 1 ms 348 KB Output is correct
7 Correct 0 ms 348 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 344 KB Output is correct
2 Correct 0 ms 360 KB Output is correct
3 Correct 0 ms 348 KB Output is correct
4 Correct 0 ms 348 KB Output is correct
5 Correct 4 ms 604 KB Output is correct
6 Correct 3 ms 348 KB Output is correct
7 Correct 0 ms 348 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 600 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 0 ms 348 KB Output is correct
4 Correct 0 ms 348 KB Output is correct
5 Correct 0 ms 348 KB Output is correct
6 Correct 0 ms 348 KB Output is correct
7 Correct 1 ms 344 KB Output is correct
8 Correct 1 ms 348 KB Output is correct
9 Correct 0 ms 348 KB Output is correct
10 Correct 0 ms 348 KB Output is correct
11 Correct 0 ms 348 KB Output is correct
12 Correct 0 ms 444 KB Output is correct
13 Correct 0 ms 348 KB Output is correct
14 Correct 0 ms 348 KB Output is correct
15 Correct 0 ms 348 KB Output is correct
16 Correct 0 ms 348 KB Output is correct
17 Correct 2 ms 348 KB Output is correct
18 Correct 1 ms 348 KB Output is correct
19 Correct 0 ms 348 KB Output is correct
20 Correct 1 ms 344 KB Output is correct
21 Correct 0 ms 360 KB Output is correct
22 Correct 0 ms 348 KB Output is correct
23 Correct 0 ms 348 KB Output is correct
24 Correct 4 ms 604 KB Output is correct
25 Correct 3 ms 348 KB Output is correct
26 Correct 0 ms 348 KB Output is correct
27 Correct 10 ms 344 KB Output is correct
28 Correct 1 ms 348 KB Output is correct
29 Correct 0 ms 344 KB Output is correct
30 Correct 10 ms 448 KB Output is correct
31 Correct 3 ms 348 KB Output is correct
32 Correct 1 ms 344 KB Output is correct
33 Correct 0 ms 344 KB Output is correct
34 Correct 2 ms 348 KB Output is correct
35 Correct 5 ms 348 KB Output is correct
36 Correct 20 ms 348 KB Output is correct
37 Correct 10 ms 344 KB Output is correct