Submission #696667

# Submission time Handle Problem Language Result Execution time Memory
696667 2023-02-07T03:24:24 Z uylulu Šarenlist (COCI22_sarenlist) C++17
110 / 110
22 ms 796 KB
#include<bits/stdc++.h>
using namespace std;

#define int long long
#define endl "\n"

const int N = 60,mod = 1e9 + 7;

vector<int> adj[N + 1];
vector<int> edge[N + 1][N + 1];

map<pair<int,int>,int> mp;

int pa[N + 1],cnt[N + 1];

void init() {
    for(int i = 1;i <= N;i++) {
        pa[i] = i;
        cnt[i] = 1;
    }
}

int find(int a) {
    if(a == pa[a]) return a;
    return pa[a] = find(pa[a]);
}

void unite(int a,int b) {
    a = find(a);
    b = find(b);
    if(a == b) return;
    if(cnt[a] < cnt[b]) swap(a,b);

    pa[b] = a;
    cnt[a] += cnt[b];
}

int cha[N + 1][N + 1],root;

void dfs(int s,int pa) {
    cha[root][s] = pa;

    for(auto u : adj[s]) {
        if(u == pa) continue;

        dfs(u,s);
    }
}

int binpow(int x,int y) {
    int res = 1,curr = x;
    while(y > 0) {
        if(y%2) {
            res = (res * curr) % mod;
        }
        y /= 2;
        curr = (curr * curr) % mod;
    }
    return res;
}

vector<pair<int,int>> st;

signed main() {
    ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);

    // freopen("in.txt","r",stdin);
    // freopen("out.txt","w",stdout);

    int n,m,k;
    cin>>n>>m>>k;

    for(int i = 1;i < n;i++) {
        int u,v;
        cin>>u>>v;
        adj[u].push_back(v);
        adj[v].push_back(u);
    
        mp[{u,v}] = i;
        mp[{v,u}] = i;
    }
    for(int i = 1;i <= n;i++) {
        root = i;
        dfs(i,-1);
    }
    for(int i = 1;i <= n;i++) {

        for(int j = 1;j <= n;j++) {
            if(j == i) continue;

            int tmp = j;
            while(tmp != i) {
                edge[i][j].push_back(mp[{tmp,cha[i][tmp]}]);
                tmp = cha[i][tmp];
            }
        }
    }
    for(int i = 0;i < m;i++) {
        int a,b;
        cin>>a>>b;

        st.push_back({a,b});
    }
    int sum = 0;

    for(int i = 0;i < 1<<m;i++) {
        int cnt = __builtin_popcountll(i);

        init();
        for(int j = 0;j < m;j++) {
            if((i >> j)%2 == 0) continue;

            int a = st[j].first,b = st[j].second;

            for(auto u : edge[a][b]) {
                unite(u,edge[a][b][0]);
            }
        }
        int tmp = 0;
        for(int i = 1;i <= n - 1;i++) {
            tmp += (i == find(i));
        }
        if(cnt%2 == 1) {
            sum = (mod + sum - binpow(k,tmp)) % mod;
        } else {
            sum = (sum + binpow(k,tmp)) % mod;
        }
    }
    cout<<sum<<endl;
}           
# Verdict Execution time Memory Grader output
1 Correct 1 ms 340 KB Output is correct
2 Correct 1 ms 340 KB Output is correct
3 Correct 1 ms 468 KB Output is correct
4 Correct 1 ms 468 KB Output is correct
5 Correct 1 ms 544 KB Output is correct
6 Correct 2 ms 724 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 420 KB Output is correct
2 Correct 1 ms 340 KB Output is correct
3 Correct 1 ms 416 KB Output is correct
4 Correct 1 ms 468 KB Output is correct
5 Correct 1 ms 596 KB Output is correct
6 Correct 2 ms 596 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 468 KB Output is correct
2 Correct 1 ms 468 KB Output is correct
3 Correct 2 ms 596 KB Output is correct
4 Correct 2 ms 596 KB Output is correct
5 Correct 3 ms 596 KB Output is correct
6 Correct 2 ms 796 KB Output is correct
7 Correct 2 ms 672 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 340 KB Output is correct
2 Correct 1 ms 420 KB Output is correct
3 Correct 1 ms 340 KB Output is correct
4 Correct 1 ms 340 KB Output is correct
5 Correct 3 ms 340 KB Output is correct
6 Correct 3 ms 340 KB Output is correct
7 Correct 1 ms 340 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 340 KB Output is correct
2 Correct 1 ms 340 KB Output is correct
3 Correct 1 ms 468 KB Output is correct
4 Correct 1 ms 468 KB Output is correct
5 Correct 1 ms 544 KB Output is correct
6 Correct 2 ms 724 KB Output is correct
7 Correct 1 ms 420 KB Output is correct
8 Correct 1 ms 340 KB Output is correct
9 Correct 1 ms 416 KB Output is correct
10 Correct 1 ms 468 KB Output is correct
11 Correct 1 ms 596 KB Output is correct
12 Correct 2 ms 596 KB Output is correct
13 Correct 1 ms 468 KB Output is correct
14 Correct 1 ms 468 KB Output is correct
15 Correct 2 ms 596 KB Output is correct
16 Correct 2 ms 596 KB Output is correct
17 Correct 3 ms 596 KB Output is correct
18 Correct 2 ms 796 KB Output is correct
19 Correct 2 ms 672 KB Output is correct
20 Correct 1 ms 340 KB Output is correct
21 Correct 1 ms 420 KB Output is correct
22 Correct 1 ms 340 KB Output is correct
23 Correct 1 ms 340 KB Output is correct
24 Correct 3 ms 340 KB Output is correct
25 Correct 3 ms 340 KB Output is correct
26 Correct 1 ms 340 KB Output is correct
27 Correct 10 ms 628 KB Output is correct
28 Correct 1 ms 596 KB Output is correct
29 Correct 2 ms 596 KB Output is correct
30 Correct 11 ms 672 KB Output is correct
31 Correct 4 ms 596 KB Output is correct
32 Correct 3 ms 596 KB Output is correct
33 Correct 2 ms 596 KB Output is correct
34 Correct 3 ms 596 KB Output is correct
35 Correct 6 ms 596 KB Output is correct
36 Correct 22 ms 504 KB Output is correct
37 Correct 10 ms 468 KB Output is correct