#include <iostream> using namespace std; const int M = 30, N = (1 << 20) + 5; int n, m, res = 0; int e[M]; bool vis[N]; void dfs(int v) { if (vis[v]) return; res++; vis[v] = 1; for (int i=0;i<m;i++) if ((v & e[i]) == 0 || (v & e[i]) == e[i]) dfs(v ^ e[i]); } int main() { cin.tie(0); cout.tie(0); ios_base::sync_with_stdio(0); cin >> n >> m; int cur = 0; for (int i = 0; i < n; i++) { int a; cin >> a; cur += (a << i); } for (int i = 0; i < m; i++) { int a, b; cin >> a >> b; e[i] = ((1 << (a - 1)) + (1 << (b - 1))); } dfs(cur); cout << res; }
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 | #include <iostream> using namespace std; const int M = 30, N = (1 << 20) + 5; int n, m, res = 0; int e[M]; bool vis[N]; void dfs(int v) { if (vis[v]) return; res++; vis[v] = 1; for (int i=0;i<m;i++) if ((v & e[i]) == 0 || (v & e[i]) == e[i]) dfs(v ^ e[i]); } int main() { cin.tie(0); cout.tie(0); ios_base::sync_with_stdio(0); cin >> n >> m; int cur = 0; for (int i = 0; i < n; i++) { int a; cin >> a; cur += (a << i); } for (int i = 0; i < m; i++) { int a, b; cin >> a >> b; e[i] = ((1 << (a - 1)) + (1 << (b - 1))); } dfs(cur); cout << res; } |