Submission #1117961

# Submission time Handle Problem Language Result Execution time Memory
1117961 2024-11-24T13:38:35 Z kh0i Šarenlist (COCI22_sarenlist) C++17
110 / 110
80 ms 848 KB
#include "bits/stdc++.h"
using namespace std;

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

using ll = long long;
using pii = pair<int, int>;

#define F first
#define S second
#define sz(x) (int)((x).size())
#define all(x) (x).begin(), (x).end()

mt19937_64 rng(chrono::steady_clock::now().time_since_epoch().count());
ll get_rand(ll l, ll r) {
    assert(l <= r);
    return uniform_int_distribution<ll> (l, r)(rng);
}

const int N = 73;
const ll mod = 1e9 + 7;

ll qpow(ll a, ll b){
    ll res = 1;
    for(; b > 0; b >>= 1, a = a * a % mod)
        if(b & 1)
            res = res * a % mod;
    return res;
}

int n, m, k;
vector<int> g[N], gm[N];
vector<int> path[N][N];
bool in[20][N];
bool vis[N];
int x[20], y[20];
ll res;

struct DSU{
    vector<ll> p, sz;
    int comp;

    void init(int n){
        p.resize(n + 2, 0);
        sz.resize(n + 2, 0);
        comp = n;
        for(int i = 1; i <= n; ++i)
            p[i] = i, sz[i] = 1;
    }

    int find_set(int u){
        return u == p[u] ? u : p[u] = find_set(p[u]);
    }

    bool join_sets(int u, int v){
        u = find_set(u), v = find_set(v);
        if(u == v)
            return 0;
        --comp;
        if(sz[u] < sz[v])
            swap(u, v);
        p[v] = u;
        sz[u] += sz[v];
        return 1;
    }
};

void get_path(int src){
    queue<int> q;
    path[src][src].push_back(src);
    q.push(src);
    while(!q.empty()){
        int u = q.front();
        q.pop();
        for(int v : g[u]){
            if(path[src][v].empty()){
                path[src][v] = path[src][u];
                path[src][v].push_back(v);
                q.push(v);
            }
        }
    }
}

void solve(){
    cin >> n >> m >> k;
    for(int i = 1; i < n; ++i){
        int u, v;
        cin >> u >> v;
        g[u].push_back(v);
        g[v].push_back(u);
    }

    for(int i = 0; i < m; ++i)
        cin >> x[i] >> y[i];

    for(int i = 1; i <= n; ++i)
        get_path(i);

    for(int i = 0; i < m; ++i){
        if(sz(path[x[i]][y[i]]) <= 2){
            cout << 0;
            return;
        }
    }

    for(int i = 0; i < m; ++i)
        for(int x : path[x[i]][y[i]])
            in[i][x] = 1;

    for(int i = 0; i < m; ++i){
        for(int j = i + 1; j < m; ++j){
            int cnt = 0;
            for(int x = 1; x <= n; ++x){
                if(in[i][x] and in[j][x])
                    ++cnt;
            }
            if(cnt > 1){
                gm[i].push_back(j);
                gm[j].push_back(i);
            }
        }
    }

    for(int mask = 0; mask < (1 << m); ++mask){
        DSU dsu;
        dsu.init(m);
        for(int i = 0; i < m; ++i){
            if(mask & (1 << i)){
                for(int v : gm[i])
                    if(mask & (1 << v))
                        dsu.join_sets(i, v);
            }
        }
        set<pii> st;

        for(int i = 0; i < m; ++i){
            if(mask & (1 << i)){
                for(int j = 0; j < sz(path[x[i]][y[i]]) - 1; ++j){
                    int u = path[x[i]][y[i]][j], v = path[x[i]][y[i]][j + 1];
                    if(u > v)
                        swap(u, v);
                    st.insert({u, v});
                }
            }
        }

        // debug(st);

        int rem = n - 1 - sz(st);

        ll cur = qpow(k, rem);
        for(int i = 0; i < m; ++i){
            if(mask & (1 << i) and dsu.find_set(i) == i)
                cur = cur * k % mod;
        }

        // debug(bitset<5>(mask), cur);

        if(__builtin_popcount(mask) & 1)
            res += mod - cur;
        else
            res += cur;
        while(res >= mod)
            res -= mod;
    }

    cout << res;
}

int32_t main() {
    cin.tie(nullptr)->sync_with_stdio(0);
    #define task "coloring"
    if(fopen(task".inp", "r")){
        freopen(task".inp", "r", stdin);
        freopen(task".out", "w", stdout);
    }
    int test = 1;
//    cin >> test;
    for(int i = 1; i <= test; ++i){
//        cout << "Case #" << i << ": ";
        solve();
    }
    #ifdef LOCAL
        cerr << "\n[Time]: " << 1000.0 * clock() / CLOCKS_PER_SEC << " ms.\n";
    #endif
    return 0;
}

Compilation message

Main.cpp: In function 'int32_t main()':
Main.cpp:179:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
  179 |         freopen(task".inp", "r", stdin);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
Main.cpp:180:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
  180 |         freopen(task".out", "w", stdout);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 1 ms 592 KB Output is correct
2 Correct 1 ms 592 KB Output is correct
3 Correct 1 ms 592 KB Output is correct
4 Correct 1 ms 592 KB Output is correct
5 Correct 1 ms 592 KB Output is correct
6 Correct 2 ms 592 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 592 KB Output is correct
2 Correct 1 ms 592 KB Output is correct
3 Correct 1 ms 592 KB Output is correct
4 Correct 2 ms 592 KB Output is correct
5 Correct 2 ms 592 KB Output is correct
6 Correct 1 ms 592 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 592 KB Output is correct
2 Correct 1 ms 592 KB Output is correct
3 Correct 1 ms 592 KB Output is correct
4 Correct 2 ms 592 KB Output is correct
5 Correct 5 ms 764 KB Output is correct
6 Correct 1 ms 848 KB Output is correct
7 Correct 1 ms 592 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 592 KB Output is correct
2 Correct 1 ms 592 KB Output is correct
3 Correct 1 ms 764 KB Output is correct
4 Correct 1 ms 764 KB Output is correct
5 Correct 9 ms 612 KB Output is correct
6 Correct 9 ms 604 KB Output is correct
7 Correct 1 ms 592 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 592 KB Output is correct
2 Correct 1 ms 592 KB Output is correct
3 Correct 1 ms 592 KB Output is correct
4 Correct 1 ms 592 KB Output is correct
5 Correct 1 ms 592 KB Output is correct
6 Correct 2 ms 592 KB Output is correct
7 Correct 1 ms 592 KB Output is correct
8 Correct 1 ms 592 KB Output is correct
9 Correct 1 ms 592 KB Output is correct
10 Correct 2 ms 592 KB Output is correct
11 Correct 2 ms 592 KB Output is correct
12 Correct 1 ms 592 KB Output is correct
13 Correct 1 ms 592 KB Output is correct
14 Correct 1 ms 592 KB Output is correct
15 Correct 1 ms 592 KB Output is correct
16 Correct 2 ms 592 KB Output is correct
17 Correct 5 ms 764 KB Output is correct
18 Correct 1 ms 848 KB Output is correct
19 Correct 1 ms 592 KB Output is correct
20 Correct 1 ms 592 KB Output is correct
21 Correct 1 ms 592 KB Output is correct
22 Correct 1 ms 764 KB Output is correct
23 Correct 1 ms 764 KB Output is correct
24 Correct 9 ms 612 KB Output is correct
25 Correct 9 ms 604 KB Output is correct
26 Correct 1 ms 592 KB Output is correct
27 Correct 31 ms 592 KB Output is correct
28 Correct 2 ms 848 KB Output is correct
29 Correct 1 ms 592 KB Output is correct
30 Correct 33 ms 760 KB Output is correct
31 Correct 9 ms 784 KB Output is correct
32 Correct 4 ms 600 KB Output is correct
33 Correct 3 ms 592 KB Output is correct
34 Correct 5 ms 848 KB Output is correct
35 Correct 18 ms 592 KB Output is correct
36 Correct 80 ms 592 KB Output is correct
37 Correct 37 ms 592 KB Output is correct