답안 #1107706

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
1107706 2024-11-02T02:59:35 Z LTTrungCHL Šarenlist (COCI22_sarenlist) C++17
110 / 110
12 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 <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;
        while (x != y){
            if (h[x] > h[y]){
                dk[i].pb(idpa[x]);
                x = pa[x];
            } else {
                dk[i].pb(idpa[y]);
                y = pa[y];
            }
        }
    }
    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;
//        ans %= 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]){
//                    cout << z <<"\n";
                    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;
//            cout << mask <<" th1 "<< 1ll * muk[n - 1 - cnt] * muk[cnt2]%e <<"\n";
            ans -= 1ll * muk[n - 1 - cnt] * muk[cnt2]%e;
            if (ans >= e) ans -= e;
        } else {
//            cout << mask <<" th2 "<< 1ll * muk[n - 1 - cnt] * muk[cnt2]%e <<"\n";
            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:35: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]
   35 |     for (int i = 0;i < adj[u].size();++i){
      |                    ~~^~~~~~~~~~~~~~~
Main.cpp: In function 'int main()':
Main.cpp:142:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
  142 |         freopen("ltt.inp", "r", stdin);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~
Main.cpp:143:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
  143 |         freopen("ltt.out", "w", stdout);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
# 결과 실행 시간 메모리 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 348 KB Output is correct
# 결과 실행 시간 메모리 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
# 결과 실행 시간 메모리 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 2 ms 336 KB Output is correct
6 Correct 1 ms 336 KB Output is correct
7 Correct 1 ms 348 KB Output is correct
# 결과 실행 시간 메모리 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 2 ms 336 KB Output is correct
6 Correct 2 ms 336 KB Output is correct
7 Correct 1 ms 336 KB Output is correct
# 결과 실행 시간 메모리 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 348 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 336 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 504 KB Output is correct
16 Correct 1 ms 336 KB Output is correct
17 Correct 2 ms 336 KB Output is correct
18 Correct 1 ms 336 KB Output is correct
19 Correct 1 ms 348 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 504 KB Output is correct
24 Correct 2 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 336 KB Output is correct
29 Correct 1 ms 336 KB Output is correct
30 Correct 4 ms 472 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 12 ms 472 KB Output is correct
37 Correct 6 ms 336 KB Output is correct