Submission #1107707

# Submission time Handle Problem Language Result Execution time Memory
1107707 2024-11-02T03:03:38 Z LTTrungCHL Šarenlist (COCI22_sarenlist) C++17
110 / 110
11 ms 504 KB
///***LTT***///
/// ->NHAT QUOC GIA<- ///
#include<bits/stdc++.h>
//#pragma GCC optimize ("O3")
//#pragma GCC optimize ("unroll-loops")
//#pragma GCC target("popcnt")
//#define int long long
#define endl "\n"
#define F first
#define S second
#define pb push_back
using namespace std;
vector <int> lg2;
//void MAKE_LOG_ARR(int n){
//    lg2.resize(n + 3);
//    for (int i = 1;i <= n;++i){
//        lg2[i] = __lg(i);
//    }
//}
const long long oo = 1e9+7;
const long long e = 1e9+7;
const int N =100;
int h[N], parent[N], idpa[N];
int sz[N];
int muk[N];
int m, k;
int last[N];
int pa[N];
int n;
vector <pair <int ,int>> adj[N];
bool dd[N];
vector <pair <int,int>> vec;
vector <int> dk[20];
void pre(int u, int p){
    h[u] = h[p] + 1;
    for (int i = 0;i < adj[u].size();++i){
        int v = adj[u][i].F;
        int id = adj[u][i].S;
        if (v == p) continue;
        pre(v, u);
        pa[v] = u;
        idpa[v] = id;
    }
}
int finds(int u){
    if (u == parent[u]) return u;
    int pu = finds(parent[u]);
    parent[u] = pu;
    return pu;
}
void unions(int u, int v){
    u = finds(u);
    v = finds(v);
    if (u != v){
        if (sz[u] < sz[v]) swap(u, v);
        parent[v] = u;
        sz[u] += sz[v];
    }
    return;
}
void solve(){
    cin >> n >> m >> k;
    for (int i = 1;i < n;++i){
        int u, v;
        cin >> u >> v;
        adj[u].pb({v, i});
        adj[v].pb({u, i});
    }
    pre(1,0);
    for (int i = 1;i <= m;++i){
        int x, y;
        cin >> x >> y;
        if (x == y) continue;
        vec.pb({min(x,y), max(x,y)});
    }
    sort(vec.begin(), vec.end());
    vec.erase(unique(vec.begin(), vec.end()), vec.end());
    for (int i = 0;i < vec.size();++i){
        int x = vec[i].F;
        int y = vec[i].S;
        while (x != y){
            if (h[x] > h[y]){
                dk[i + 1].pb(idpa[x]);
                x = pa[x];
            } else {
                dk[i + 1].pb(idpa[y]);
                y = pa[y];
            }
        }
    }
    m = vec.size();
    int ans = 1;
    muk[0] = 1;
    for (int i = 1;i <= n;++i){
        muk[i] = 1ll * muk[i - 1] * k % e;
    }
    for (int i = 1;i < n;++i){
        ans = 1ll * ans * k % e;
    }
    for (int mask = 1;mask < (1 << m);++mask){
        for (int i = 1;i < n;++i){
            last[i] = 0;
        }
        for (int i = 1;i <= m;++i){
            parent[i] = i;
            dd[i] = false;
            sz[i] = 1;
        }
        int cnt = 0, cntbit = 0;
        for (int i = 0;i < m;++i){
            if ((1 << i) & mask){
                cntbit++;
                for (int z : dk[i + 1]){
                    if (last[z]){
                        unions(i + 1, last[z]);
                    }
                    if (!last[z]){
                        ++cnt;
                    }
                    last[z] = i + 1;
                }
            }
        }
        int cnt2 = 0;
        for (int i = 1;i <= m;++i){
            if (!dd[finds(i)] and ((1 << (i - 1)) & mask) ){
                dd[finds(i)] = true;
                ++cnt2;
            }
        }
        if (cntbit & 1){
            ans += e;
            ans -= 1ll * muk[n - 1 - cnt] * muk[cnt2]%e;
            if (ans >= e) ans -= e;
        } else {
            ans += 1ll * muk[n - 1 - cnt] * muk[cnt2]%e;
            if (ans >= e) ans -= e;
        }
    }
    cout << ans;
    return;
}
int main(){
    ios_base::sync_with_stdio(NULL);
    cin.tie(NULL);
    cout.tie(NULL);
    if (fopen("ltt.inp", "r")){
        freopen("ltt.inp", "r", stdin);
        freopen("ltt.out", "w", stdout);
    }
//    int t;
//    cin >> t;
//    while(t--){
    solve();
//    }
    return 0;
}






Compilation message

Main.cpp: In function 'void pre(int, int)':
Main.cpp:36:22: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   36 |     for (int i = 0;i < adj[u].size();++i){
      |                    ~~^~~~~~~~~~~~~~~
Main.cpp: In function 'void solve()':
Main.cpp:78:22: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   78 |     for (int i = 0;i < vec.size();++i){
      |                    ~~^~~~~~~~~~~~
Main.cpp: In function 'int main()':
Main.cpp:148:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
  148 |         freopen("ltt.inp", "r", stdin);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~
Main.cpp:149:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
  149 |         freopen("ltt.out", "w", stdout);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 1 ms 336 KB Output is correct
2 Correct 1 ms 336 KB Output is correct
3 Correct 1 ms 504 KB Output is correct
4 Correct 1 ms 336 KB Output is correct
5 Correct 1 ms 336 KB Output is correct
6 Correct 1 ms 336 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 336 KB Output is correct
2 Correct 1 ms 336 KB Output is correct
3 Correct 1 ms 336 KB Output is correct
4 Correct 1 ms 504 KB Output is correct
5 Correct 1 ms 336 KB Output is correct
6 Correct 1 ms 336 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 336 KB Output is correct
2 Correct 1 ms 336 KB Output is correct
3 Correct 1 ms 336 KB Output is correct
4 Correct 1 ms 336 KB Output is correct
5 Correct 1 ms 336 KB Output is correct
6 Correct 1 ms 336 KB Output is correct
7 Correct 1 ms 336 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 336 KB Output is correct
2 Correct 1 ms 336 KB Output is correct
3 Correct 1 ms 336 KB Output is correct
4 Correct 1 ms 336 KB Output is correct
5 Correct 1 ms 336 KB Output is correct
6 Correct 2 ms 336 KB Output is correct
7 Correct 1 ms 336 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 336 KB Output is correct
2 Correct 1 ms 336 KB Output is correct
3 Correct 1 ms 504 KB Output is correct
4 Correct 1 ms 336 KB Output is correct
5 Correct 1 ms 336 KB Output is correct
6 Correct 1 ms 336 KB Output is correct
7 Correct 1 ms 336 KB Output is correct
8 Correct 1 ms 336 KB Output is correct
9 Correct 1 ms 336 KB Output is correct
10 Correct 1 ms 504 KB Output is correct
11 Correct 1 ms 336 KB Output is correct
12 Correct 1 ms 336 KB Output is correct
13 Correct 1 ms 336 KB Output is correct
14 Correct 1 ms 336 KB Output is correct
15 Correct 1 ms 336 KB Output is correct
16 Correct 1 ms 336 KB Output is correct
17 Correct 1 ms 336 KB Output is correct
18 Correct 1 ms 336 KB Output is correct
19 Correct 1 ms 336 KB Output is correct
20 Correct 1 ms 336 KB Output is correct
21 Correct 1 ms 336 KB Output is correct
22 Correct 1 ms 336 KB Output is correct
23 Correct 1 ms 336 KB Output is correct
24 Correct 1 ms 336 KB Output is correct
25 Correct 2 ms 336 KB Output is correct
26 Correct 1 ms 336 KB Output is correct
27 Correct 4 ms 336 KB Output is correct
28 Correct 1 ms 500 KB Output is correct
29 Correct 1 ms 336 KB Output is correct
30 Correct 4 ms 336 KB Output is correct
31 Correct 2 ms 336 KB Output is correct
32 Correct 1 ms 336 KB Output is correct
33 Correct 1 ms 336 KB Output is correct
34 Correct 1 ms 336 KB Output is correct
35 Correct 3 ms 336 KB Output is correct
36 Correct 11 ms 504 KB Output is correct
37 Correct 5 ms 464 KB Output is correct