#include <iostream> #include <vector> #include <set> #include <map> #include <algorithm> #include <unordered_map> #include <unordered_set> #include <queue> #include <deque> #include <tuple> #include <bitset> using namespace std; #define pb push_back #define mp make_pair #define st first #define nd second #define x first #define y second typedef long long ll; typedef pair<int, int> pii; typedef pair<ll, ll> pll; typedef vector<int> vi; typedef vector<ll> vll; int n, m; vector<ll> start; unordered_set<ll> moves[200001]; unordered_set<ll> sklady[200001]; unordered_map<ll, ll> sklad_map; bool visited_sklady[200001]; ll cur_squad = 0; ll cur_s=-1; ll all_s=0; ll cur_cnt = 0; ll all_cnt = 1; ll MOD = 1e9 + 7; void sklad(int q){ if(visited_sklady[q]){ return; } visited_sklady[q] = true; sklady[cur_s].insert(q); sklad_map[q] = cur_squad; cur_squad++; for(auto mov: moves[q]){ sklad(mov); } } void bfs(ll pos){ cur_cnt = 1; deque<ll> kolejka; kolejka.pb(pos); unordered_map<ll, bool> visited; visited[pos] = true; while(kolejka.size()){ pos = kolejka.front(); kolejka.pop_front(); for(auto a: sklady[cur_s]){ for(auto b: moves[a]){ ll ai = ( 1LL << (sklad_map[a]) ); ll bi = ( 1LL << (sklad_map[b]) ); bool ac = pos & ai; bool bc = pos & bi; if(ac == bc){ ll new_pos = pos^ai^bi; if(!visited[new_pos]){ visited[new_pos] = true; kolejka.pb(new_pos); cur_cnt = (cur_cnt+1LL)%MOD; } } } } } } int main(){ ios_base::sync_with_stdio(false); cin.tie(nullptr); cin >> n >> m; for(int i=0; i<n; i++){ ll a; cin >> a; start.pb(a); } for(int i=0; i<m; i++){ int a, b; cin >> a >> b; moves[a].insert(b); moves[b].insert(a); } for(int i=1; i<=n; i++){ if(!visited_sklady[i]){ cur_squad=0; all_s++; cur_s++; sklad(i); } } for(cur_s=0; cur_s<all_s; cur_s++){ ll start_pos = 0; for(auto a: sklady[cur_s]){ start_pos += (start[a-1] << sklad_map[a]); } bfs(start_pos); all_cnt = (all_cnt * cur_cnt)%MOD; } cout << all_cnt <<"\n"; 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 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 | #include <iostream> #include <vector> #include <set> #include <map> #include <algorithm> #include <unordered_map> #include <unordered_set> #include <queue> #include <deque> #include <tuple> #include <bitset> using namespace std; #define pb push_back #define mp make_pair #define st first #define nd second #define x first #define y second typedef long long ll; typedef pair<int, int> pii; typedef pair<ll, ll> pll; typedef vector<int> vi; typedef vector<ll> vll; int n, m; vector<ll> start; unordered_set<ll> moves[200001]; unordered_set<ll> sklady[200001]; unordered_map<ll, ll> sklad_map; bool visited_sklady[200001]; ll cur_squad = 0; ll cur_s=-1; ll all_s=0; ll cur_cnt = 0; ll all_cnt = 1; ll MOD = 1e9 + 7; void sklad(int q){ if(visited_sklady[q]){ return; } visited_sklady[q] = true; sklady[cur_s].insert(q); sklad_map[q] = cur_squad; cur_squad++; for(auto mov: moves[q]){ sklad(mov); } } void bfs(ll pos){ cur_cnt = 1; deque<ll> kolejka; kolejka.pb(pos); unordered_map<ll, bool> visited; visited[pos] = true; while(kolejka.size()){ pos = kolejka.front(); kolejka.pop_front(); for(auto a: sklady[cur_s]){ for(auto b: moves[a]){ ll ai = ( 1LL << (sklad_map[a]) ); ll bi = ( 1LL << (sklad_map[b]) ); bool ac = pos & ai; bool bc = pos & bi; if(ac == bc){ ll new_pos = pos^ai^bi; if(!visited[new_pos]){ visited[new_pos] = true; kolejka.pb(new_pos); cur_cnt = (cur_cnt+1LL)%MOD; } } } } } } int main(){ ios_base::sync_with_stdio(false); cin.tie(nullptr); cin >> n >> m; for(int i=0; i<n; i++){ ll a; cin >> a; start.pb(a); } for(int i=0; i<m; i++){ int a, b; cin >> a >> b; moves[a].insert(b); moves[b].insert(a); } for(int i=1; i<=n; i++){ if(!visited_sklady[i]){ cur_squad=0; all_s++; cur_s++; sklad(i); } } for(cur_s=0; cur_s<all_s; cur_s++){ ll start_pos = 0; for(auto a: sklady[cur_s]){ start_pos += (start[a-1] << sklad_map[a]); } bfs(start_pos); all_cnt = (all_cnt * cur_cnt)%MOD; } cout << all_cnt <<"\n"; return 0; } |