Submission #1107707

#TimeUsernameProblemLanguageResultExecution timeMemory
1107707LTTrungCHLŠarenlist (COCI22_sarenlist)C++17
110 / 110
11 ms504 KiB
///***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 (stderr)

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 timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...