#include <iostream> #include <vector> #include <set> using namespace std; const int MOD = 1e9 + 7; vector<vector<int>> adj; vector<bool> visited; set<int> current_component; void dfs(int v) { visited[v] = true; current_component.insert(v); for (int u : adj[v]) { if (!visited[u]) { dfs(u); } } } int main() { int n, m; cin >> n >> m; adj.resize(n); visited.resize(n, false); for (int i = 0; i < n; ++i) { int bulb_state; cin >> bulb_state; } for (int i = 0; i < m; ++i) { int a, b; cin >> a >> b; --a; --b; adj[a].push_back(b); adj[b].push_back(a); } int components = 0; for (int i = 0; i < n; ++i) { if (!visited[i]) { dfs(i); components++; current_component.clear(); } } long long configurations = 1; for (int i = 0; i < components; ++i) { configurations = (configurations * 2) % MOD; } cout << configurations << endl; return 0; }
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 | #include <iostream> #include <vector> #include <set> using namespace std; const int MOD = 1e9 + 7; vector<vector<int>> adj; vector<bool> visited; set<int> current_component; void dfs(int v) { visited[v] = true; current_component.insert(v); for (int u : adj[v]) { if (!visited[u]) { dfs(u); } } } int main() { int n, m; cin >> n >> m; adj.resize(n); visited.resize(n, false); for (int i = 0; i < n; ++i) { int bulb_state; cin >> bulb_state; } for (int i = 0; i < m; ++i) { int a, b; cin >> a >> b; --a; --b; adj[a].push_back(b); adj[b].push_back(a); } int components = 0; for (int i = 0; i < n; ++i) { if (!visited[i]) { dfs(i); components++; current_component.clear(); } } long long configurations = 1; for (int i = 0; i < components; ++i) { configurations = (configurations * 2) % MOD; } cout << configurations << endl; return 0; } |