Submission #878647

# Submission time Handle Problem Language Result Execution time Memory
878647 2023-11-25T03:06:42 Z noiaint Šarenlist (COCI22_sarenlist) C++17
110 / 110
43 ms 460 KB
#include <bits/stdc++.h>

using namespace std;

#ifdef LOCAL
#include "debug.h"
#else
#define debug(...) 42
#endif

#define file ""

#define mp make_pair
#define fi first
#define se second
#define all(x) x.begin(), x.end()

#define bit(x) (1LL << (x))
#define getbit(x, i) (((x) >> (i)) & 1)
#define popcount __builtin_popcountll

mt19937_64 rd(chrono::steady_clock::now().time_since_epoch().count());
int rand(int l, int r) {
    return l + rd() % (r - l + 1);
}

const int N = 100 + 5;
const int mod = (int)1e9 + 7; // 998244353;
const int lg = 25; // lg + 1
const int oo = 1e9;
const long long ooo = 1e18;

template<class X, class Y> bool mini(X &a, Y b) {
    return a > b ? (a = b, true) : false;
}
template<class X, class Y> bool maxi(X &a, Y b) {
    return a < b ? (a = b, true) : false;
}
void add(int &a, int b) {
    a += b;
    if (a >= mod) a -= mod;
    if (a < 0) a += mod;
}

int n, m, k;
vector<pair<int, int> > adj[N];
pair<int, int> path[N];
int par[N], par_id[N];
int e[N];
int cnt = 0;

int getpar(int x) {
    return e[x] < 0 ? x : e[x] = getpar(e[x]);
}
bool unite(int x, int y) {
    x = getpar(x);
    y = getpar(y);
    if (x == y) return false;
    if (-e[x] < -e[y]) swap(x, y);
    e[x] += e[y];
    e[y] = x;
    --cnt;
    return true;
}

void dfs(int u, int p) {
    for (auto [v, id] : adj[u]) if (v != p) {
        par[v] = u;
        par_id[v] = id;
        dfs(v, u);
    }
}

void update(int x, int y) {
    par[x] = x;
    dfs(x, 0);
    for (int u = y; par[u] != x; u = par[u]) {
        unite(par_id[u], par_id[par[u]]);
    }
}

void process() {
    cin >> n >> m >> k;
    for (int i = 1; i < n; ++i) {
        int u, v;
        cin >> u >> v;
        adj[u].emplace_back(v, i);
        adj[v].emplace_back(u, i);
    }
    for (int i = 0; i < m; ++i) cin >> path[i].fi >> path[i].se;
        
    int ans = 0;
    for (int mask = 0; mask < bit(m); ++mask) {
        memset(e, -1, sizeof e);
        cnt = n - 1;

        for (int i = 0; i < m; ++i) if (getbit(mask, i)) {
            update(path[i].fi, path[i].se);
        }

        int cur = 1;
        for (int i = 0; i < cnt; ++i) cur = 1LL * cur * k % mod;

        debug(cur, cnt);
        int sgn = popcount(mask) & 1 ? -1 : 1;
        add(ans, sgn * cur);
    }

    cout << ans;
}

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

    #ifdef LOCAL
    freopen("input.txt", "r", stdin);
    // freopen("output.txt", "w", stdout);
    #endif

    int tc = 1;
    // cin >> tc;
    
    while (tc--) {
        process();
    }

    return 0;
}

/*

*/  

Compilation message

Main.cpp: In function 'void process()':
Main.cpp:8:20: warning: statement has no effect [-Wunused-value]
    8 | #define debug(...) 42
      |                    ^~
Main.cpp:104:9: note: in expansion of macro 'debug'
  104 |         debug(cur, cnt);
      |         ^~~~~
# Verdict Execution time Memory Grader output
1 Correct 1 ms 348 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 348 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 344 KB Output is correct
4 Correct 0 ms 348 KB Output is correct
5 Correct 0 ms 348 KB Output is correct
6 Correct 1 ms 348 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 1 ms 348 KB Output is correct
5 Correct 7 ms 348 KB Output is correct
6 Correct 0 ms 348 KB Output is correct
7 Correct 0 ms 348 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 5 ms 348 KB Output is correct
6 Correct 5 ms 348 KB Output is correct
7 Correct 0 ms 460 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 348 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 348 KB Output is correct
7 Correct 0 ms 348 KB Output is correct
8 Correct 0 ms 348 KB Output is correct
9 Correct 0 ms 344 KB Output is correct
10 Correct 0 ms 348 KB Output is correct
11 Correct 0 ms 348 KB Output is correct
12 Correct 1 ms 348 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 1 ms 348 KB Output is correct
17 Correct 7 ms 348 KB Output is correct
18 Correct 0 ms 348 KB Output is correct
19 Correct 0 ms 348 KB Output is correct
20 Correct 0 ms 348 KB Output is correct
21 Correct 0 ms 348 KB Output is correct
22 Correct 0 ms 348 KB Output is correct
23 Correct 0 ms 348 KB Output is correct
24 Correct 5 ms 348 KB Output is correct
25 Correct 5 ms 348 KB Output is correct
26 Correct 0 ms 460 KB Output is correct
27 Correct 29 ms 348 KB Output is correct
28 Correct 1 ms 348 KB Output is correct
29 Correct 0 ms 348 KB Output is correct
30 Correct 33 ms 444 KB Output is correct
31 Correct 8 ms 344 KB Output is correct
32 Correct 4 ms 348 KB Output is correct
33 Correct 1 ms 348 KB Output is correct
34 Correct 4 ms 348 KB Output is correct
35 Correct 16 ms 348 KB Output is correct
36 Correct 43 ms 348 KB Output is correct
37 Correct 20 ms 344 KB Output is correct