#include <bits/stdc++.h>
using namespace std;
constexpr int maxn = 2e5+2;
int n,m, a[maxn*2], b[maxn*2], rep[maxn];
vector <int> ls, conn[maxn];
long long res=1, act;
bool arr[maxn], viss[maxn];
vector <pair<int,int>> conls;
map <vector<bool>, bool> emptymap;
vector <bool> vls;
struct component
{
vector <pair<int,int>> con;
map <vector<bool>, bool> vis;
vector <bool> v;
void dfs()
{
vis[v] = 1; act++;
for(auto e : con)
{
if(v[e.first] == v[e.second])
{
v[e.first] = !v[e.first];
v[e.second] = !v[e.second];
if(!vis[v]) dfs();
v[e.first] = !v[e.first];
v[e.second] = !v[e.second];
}
}
}
};
vector <component> comps;
void zbierz(int v)
{
viss[v] = 1; ls.push_back(v);
for(auto u : conn[v])
if(!viss[u]) zbierz(u);
}
int main()
{
ios_base::sync_with_stdio(0);
cin.tie(0); cout.tie(0);
cin >> n >> m;
for(int i=1; i<=n; i++)
cin >> arr[i];
for(int i=1; i<=m; i++)
{
cin >> a[i] >> b[i];
conn[a[i]].push_back(b[i]);
conn[b[i]].push_back(a[i]);
}
for(int i=1; i<=n; i++)
{
if(!viss[i])
{
ls.clear(); conls.clear(); vls.clear();
zbierz(i);
for(int i=0; i<ls.size(); i++)
{
rep[ls[i]] = i;
vls.push_back(arr[ls[i]]);
}
for(int j=1; j<=m; j++)
if(count(ls.begin(), ls.end(), a[j]))
conls.push_back({rep[a[j]], rep[b[j]]});
comps.push_back({conls, emptymap, vls});
}
}
for(auto c : comps)
{
act = 0;
c.dfs();
res *= act;
}
cout << res << '\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 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 | #include <bits/stdc++.h> using namespace std; constexpr int maxn = 2e5+2; int n,m, a[maxn*2], b[maxn*2], rep[maxn]; vector <int> ls, conn[maxn]; long long res=1, act; bool arr[maxn], viss[maxn]; vector <pair<int,int>> conls; map <vector<bool>, bool> emptymap; vector <bool> vls; struct component { vector <pair<int,int>> con; map <vector<bool>, bool> vis; vector <bool> v; void dfs() { vis[v] = 1; act++; for(auto e : con) { if(v[e.first] == v[e.second]) { v[e.first] = !v[e.first]; v[e.second] = !v[e.second]; if(!vis[v]) dfs(); v[e.first] = !v[e.first]; v[e.second] = !v[e.second]; } } } }; vector <component> comps; void zbierz(int v) { viss[v] = 1; ls.push_back(v); for(auto u : conn[v]) if(!viss[u]) zbierz(u); } int main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); cin >> n >> m; for(int i=1; i<=n; i++) cin >> arr[i]; for(int i=1; i<=m; i++) { cin >> a[i] >> b[i]; conn[a[i]].push_back(b[i]); conn[b[i]].push_back(a[i]); } for(int i=1; i<=n; i++) { if(!viss[i]) { ls.clear(); conls.clear(); vls.clear(); zbierz(i); for(int i=0; i<ls.size(); i++) { rep[ls[i]] = i; vls.push_back(arr[ls[i]]); } for(int j=1; j<=m; j++) if(count(ls.begin(), ls.end(), a[j])) conls.push_back({rep[a[j]], rep[b[j]]}); comps.push_back({conls, emptymap, vls}); } } for(auto c : comps) { act = 0; c.dfs(); res *= act; } cout << res << '\n'; } |
English