Submission #1107796

#TimeUsernameProblemLanguageResultExecution timeMemory
1107796WhisperŠarenlist (COCI22_sarenlist)C++17
110 / 110
13 ms504 KiB
#include <bits/stdc++.h> using namespace std; using ll = long long; #define int long long #define FOR(i, a, b) for (int i = (a); i <= (b); i++) #define FORD(i, a, b) for (int i = (b); i >= (a); i --) #define REP(i, a) for (int i = 0; i < (a); ++i) #define REPD(i, a) for (int i = (a) - 1; i >= 0; --i) #define MASK(i) (1LL << (i)) #define BIT(x, i) (((x) >> (i)) & 1) constexpr ll LINF = (1ll << 60); constexpr int INF = (1ll << 30); constexpr int Mod = 1e9 + 7; mt19937 rng(chrono::steady_clock::now().time_since_epoch().count()); /* Phu Trong from Nguyen Tat Thanh High School for gifted student */ template <class X, class Y> bool minimize(X &x, const Y &y){ X eps = 1e-9; if (x > y + eps) {x = y; return 1;} return 0; } template <class X, class Y> bool maximize(X &x, const Y &y){ X eps = 1e-9; if (x + eps < y) {x = y; return 1;} return 0; } #define MAX_NODE 75 #define MAX_REQ 16 int numNode, numEdge, numColor; vector<pair<int, int>> G[MAX_NODE]; int u[MAX_REQ], v[MAX_REQ]; void add(int &x, int y){ x += y; if(x >= Mod) x %= Mod; while(x < 0) x += Mod; } int mul(int a, int b){ return 1LL * a * b % Mod; } int pa[MAX_NODE], comp, sz[MAX_NODE]; void Init(void){ comp = numNode - 1; for (int i = 1; i < numNode; ++i){ pa[i] = i; sz[i] = 1; } } int find(int u){ return (u == pa[u] ? u : pa[u] = find(pa[u])); } bool joint (int u, int v){ u = find(u), v = find(v); if(u == v) return false; --comp; if(sz[u] < sz[v]) swap(u, v); sz[u] += sz[v]; pa[v] = u; return true; } int edge_ID[MAX_NODE]; int depth[MAX_NODE], up[MAX_NODE]; void dfs(int u, int p = -1){ for(pair<int, int>&x : G[u]) { int v = x.first, id = x.second; if(v ^ p){ depth[v] = depth[u] + 1; up[v] = u; edge_ID[v] = id; dfs(v, u); } } } void addEdge(int u, int v){ if(depth[u] < depth[v]) swap(u, v); while(depth[u] > depth[v]){ if(up[u] != v){ int ID_u = edge_ID[u]; int ID_v = edge_ID[up[u]]; joint(ID_u, ID_v); } u = up[u]; } while(u != v){ if(up[u] != up[v]){ int ID_u = edge_ID[u]; int ID_v = edge_ID[up[u]]; joint(ID_u, ID_v); ID_u = edge_ID[v]; ID_v = edge_ID[up[v]]; joint(ID_u, ID_v); } else{ joint(edge_ID[u], edge_ID[v]); } u = up[u], v = up[v]; } } void process(void){ cin >> numNode >> numEdge >> numColor; for (int i = 1; i < numNode; ++i){ int u, v; cin >> u >> v; G[u].emplace_back(v, i); G[v].emplace_back(u, i); } dfs(1); for (int i = 0; i < numEdge; ++i){ cin >> u[i] >> v[i]; } Init(); int ans = 0; for (int mask = 0; mask < MASK(numEdge); ++mask){ Init(); for (int i = 0; i < numEdge; ++i) if(BIT(mask, i)){ addEdge(u[i], v[i]); } int sign = (__builtin_popcount(mask) & 1 ? -1 : 1); int pw = 1; for (int i = 1; i <= comp; ++i){ pw = mul(pw, numColor); } add(ans, sign * pw); } cout << ans; } signed main(){ #define name "Whisper" cin.tie(nullptr) -> sync_with_stdio(false); //freopen(name".inp", "r", stdin); //freopen(name".out", "w", stdout); process(); return (0 ^ 0); }
#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...