Submission #764854

#TimeUsernameProblemLanguageResultExecution timeMemory
764854dxz05Šarenlist (COCI22_sarenlist)C++17
10 / 110
1 ms468 KiB
#pragma GCC optimize("Ofast,O3,unroll-loops") #pragma GCC target("avx2") #include <bits/stdc++.h> using namespace std; #define all(x) (x).begin(), (x).end() #define rall(x) (x).rbegin(), (x).rend() #define bpc(x) __builtin_popcount(x) #define bpcll(x) __builtin_popcountll(x) #define MP make_pair #define BIT(x, i) (((x) >> (i)) & 1) //#define endl '\n' mt19937 rng(chrono::high_resolution_clock::now().time_since_epoch().count()); typedef long long ll; const int MOD = 1e9 + 7; const int N = 61; long long binpow(long long n, long long k, long long mod = MOD){ // n^k % mod long long res = 1; while (k > 0){ if (k & 1) res = res * n % mod; n = n * n % mod; k >>= 1; } return res; } vector<int> operator+ (vector<int> vx, const vector<int>& vy){ for (int y : vy) vx.push_back(y); return vx; } int d[N][N]; vector<int> p[N][N]; void solve(){ int n, m, k; cin >> n >> m >> k; for (int i = 1; i <= n; i++){ fill(d[i] + 1, d[i] + n + 1, 1e9); d[i][i] = 0; } for (int i = 1; i < n; i++){ int u, v; cin >> u >> v; d[u][v] = d[v][u] = 1; p[u][v] = p[v][u] = {i}; } for (int t = 1; t <= n; t++){ for (int i = 1; i <= n; i++){ for (int j = 1; j <= n; j++){ if (d[i][j] > d[i][t] + d[t][j]){ d[i][j] = d[i][t] + d[t][j]; p[i][j] = p[i][t] + p[t][j]; } } } } vector<pair<int, int>> pairs(m); for (int i = 0; i < m; i++){ int x, y; cin >> x >> y; if (x > y) swap(x, y); pairs[i] = MP(x, y); assert(x != y); } sort(all(pairs)); pairs.erase(unique(all(pairs)), pairs.end()); m = (int) pairs.size(); ll ans = binpow(k, n - 1); for (int mask = 1; mask < (1 << m); mask++){ set<int> s; for (int i = 0; i < m; i++){ if (!BIT(mask, i)) continue; for (int j : p[pairs[i].first][pairs[i].second]){ s.insert(j); } } ll cur = k * binpow(k, n - 1 - (int) s.size()) % MOD; if (bpc(mask) & 1){ ans += MOD - cur; } else { ans += cur; } if (ans >= MOD) ans -= MOD; } cout << ans << endl; } int main(){ clock_t startTime = clock(); ios_base::sync_with_stdio(false); #ifdef LOCAL freopen("input.txt", "r", stdin); freopen("output.txt", "w", stdout); #endif int test_cases = 1; // cin >> test_cases; for (int test = 1; test <= test_cases; test++){ // cout << (solve() ? "YES" : "NO") << endl; solve(); } #ifdef LOCAL cerr << "Time: " << int((double) (clock() - startTime) / CLOCKS_PER_SEC * 1000) << " ms" << endl; #endif return 0; }

Compilation message (stderr)

Main.cpp: In function 'int main()':
Main.cpp:106:13: warning: unused variable 'startTime' [-Wunused-variable]
  106 |     clock_t startTime = clock();
      |             ^~~~~~~~~
#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...