// PA 2024 5C - Zarowki #include <iostream> #include <string> #include <vector> #include <algorithm> #include <stack> #include <unordered_set> using namespace std; #define QWE 1000000007 string fullState; vector<vector<int>> edges; vector<int> colors; vector<vector<int>> verticesIncluded; vector<int> verticesInv; unordered_set<string> statesUsed; vector<string> viableStates; void colorSection(int v, int c) { colors[v] = c; verticesInv[v] = verticesIncluded[c].size(); verticesIncluded[c].push_back(v); for (unsigned i = 0; i < edges[v].size(); ++i) { if (colors[edges[v][i]] < 0) { colorSection(edges[v][i], c); } } } long long calcResult(vector<int> *vertices) { string state = ""; string state2; for (unsigned i = 0; i < vertices->size(); ++i) { state += fullState[(*vertices)[i]]; } viableStates.clear(); statesUsed.clear(); viableStates.push_back(state); statesUsed.insert(state); for (unsigned i = 0; i < viableStates.size(); ++i) { state = viableStates[i]; for (unsigned j = 0; j < vertices->size(); ++j) { for (unsigned k = 0; k < edges[(*vertices)[j]].size(); ++k) { if (state[j] == state[verticesInv[edges[(*vertices)[j]][k]]]) { state2 = state; state2[j] = state2[j] == '0' ? '1' : '0'; state2[verticesInv[edges[(*vertices)[j]][k]]] = state[verticesInv[edges[(*vertices)[j]][k]]] == '0' ? '1' : '0'; if (statesUsed.find(state2) == statesUsed.end()) { viableStates.push_back(state2); statesUsed.insert(state2); } } } } } return viableStates.size() % QWE; } int main() { ios_base::sync_with_stdio(0); int n, m, a, b; long long result = 1; cin >> n; cin >> m; for (int i = 0; i < n; ++i) { cin >> a; fullState += (a + '0'); colors.push_back(-1); edges.push_back(vector<int>()); verticesInv.push_back(-1); } for (int i = 0; i < m; ++i) { cin >> a; cin >> b; --a; --b; edges[a].push_back(b); edges[b].push_back(a); } a = 0; for (int i = 0; i < n; ++i) { if (colors[i] < 0) { verticesIncluded.push_back(vector<int>()); colorSection(i, a); ++a; } } for (int i = 0; i < a; ++i) { result = (result * calcResult(&verticesIncluded[i])) % QWE; } cout << result << 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 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 | // PA 2024 5C - Zarowki #include <iostream> #include <string> #include <vector> #include <algorithm> #include <stack> #include <unordered_set> using namespace std; #define QWE 1000000007 string fullState; vector<vector<int>> edges; vector<int> colors; vector<vector<int>> verticesIncluded; vector<int> verticesInv; unordered_set<string> statesUsed; vector<string> viableStates; void colorSection(int v, int c) { colors[v] = c; verticesInv[v] = verticesIncluded[c].size(); verticesIncluded[c].push_back(v); for (unsigned i = 0; i < edges[v].size(); ++i) { if (colors[edges[v][i]] < 0) { colorSection(edges[v][i], c); } } } long long calcResult(vector<int> *vertices) { string state = ""; string state2; for (unsigned i = 0; i < vertices->size(); ++i) { state += fullState[(*vertices)[i]]; } viableStates.clear(); statesUsed.clear(); viableStates.push_back(state); statesUsed.insert(state); for (unsigned i = 0; i < viableStates.size(); ++i) { state = viableStates[i]; for (unsigned j = 0; j < vertices->size(); ++j) { for (unsigned k = 0; k < edges[(*vertices)[j]].size(); ++k) { if (state[j] == state[verticesInv[edges[(*vertices)[j]][k]]]) { state2 = state; state2[j] = state2[j] == '0' ? '1' : '0'; state2[verticesInv[edges[(*vertices)[j]][k]]] = state[verticesInv[edges[(*vertices)[j]][k]]] == '0' ? '1' : '0'; if (statesUsed.find(state2) == statesUsed.end()) { viableStates.push_back(state2); statesUsed.insert(state2); } } } } } return viableStates.size() % QWE; } int main() { ios_base::sync_with_stdio(0); int n, m, a, b; long long result = 1; cin >> n; cin >> m; for (int i = 0; i < n; ++i) { cin >> a; fullState += (a + '0'); colors.push_back(-1); edges.push_back(vector<int>()); verticesInv.push_back(-1); } for (int i = 0; i < m; ++i) { cin >> a; cin >> b; --a; --b; edges[a].push_back(b); edges[b].push_back(a); } a = 0; for (int i = 0; i < n; ++i) { if (colors[i] < 0) { verticesIncluded.push_back(vector<int>()); colorSection(i, a); ++a; } } for (int i = 0; i < a; ++i) { result = (result * calcResult(&verticesIncluded[i])) % QWE; } cout << result << endl; return 0; } |