#include <bits/stdc++.h> // Tomasz Nowak using namespace std; // XIII LO Szczecin using LL = long long; // Poland #define FOR(i, l, r) for(int i = (l); i <= (r); ++i) #define REP(i, n) FOR(i, 0, (n) - 1) int main() { ios_base::sync_with_stdio(0); cin.tie(0); int n, m, a, b; cin >> n >> m >> a >> b; assert(a + b <= n); vector<vector<int>> graph(n); while(m --> 0) { int v, u; string mid; cin >> v >> mid >> u; --v, --u; if(mid == "--") { graph[v].emplace_back(u); graph[u].emplace_back(v); } else if(mid == "->") graph[v].emplace_back(u); } vector<int> vis(a); REP(i, a) { vector<bool> visited(n, false); vis[i] = true; deque<int> que = {i}; while(not que.empty()) { int v = que.front(); que.pop_front(); for(int u : graph[v]) if(not visited[u]) { visited[u] = true; que.emplace_back(u); } } int mask = 0; FOR(v, a, a + b - 1) if(visited[v]) mask |= 1 << (v - a); vis[i] = mask; } int answer = 0; REP(mask, (1 << b)) { bool good = true; REP(v, a) if((vis[v] & mask) == 0) { good = false; break; } answer += good; } cout << answer << '\n'; }
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 | #include <bits/stdc++.h> // Tomasz Nowak using namespace std; // XIII LO Szczecin using LL = long long; // Poland #define FOR(i, l, r) for(int i = (l); i <= (r); ++i) #define REP(i, n) FOR(i, 0, (n) - 1) int main() { ios_base::sync_with_stdio(0); cin.tie(0); int n, m, a, b; cin >> n >> m >> a >> b; assert(a + b <= n); vector<vector<int>> graph(n); while(m --> 0) { int v, u; string mid; cin >> v >> mid >> u; --v, --u; if(mid == "--") { graph[v].emplace_back(u); graph[u].emplace_back(v); } else if(mid == "->") graph[v].emplace_back(u); } vector<int> vis(a); REP(i, a) { vector<bool> visited(n, false); vis[i] = true; deque<int> que = {i}; while(not que.empty()) { int v = que.front(); que.pop_front(); for(int u : graph[v]) if(not visited[u]) { visited[u] = true; que.emplace_back(u); } } int mask = 0; FOR(v, a, a + b - 1) if(visited[v]) mask |= 1 << (v - a); vis[i] = mask; } int answer = 0; REP(mask, (1 << b)) { bool good = true; REP(v, a) if((vis[v] & mask) == 0) { good = false; break; } answer += good; } cout << answer << '\n'; } |