-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathD_Beautiful_Graph.cpp
More file actions
86 lines (75 loc) · 1.55 KB
/
D_Beautiful_Graph.cpp
File metadata and controls
86 lines (75 loc) · 1.55 KB
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
#include <bits/stdc++.h>
using namespace std;
const int N = 3e5 + 5;
const int mod = 998244353;
vector<int> adj[N];
int color[N];
int modpwr(long long a, int b) {
long long res = 1;
while(b) {
if(b & 1) {
res = (res * a) % mod;
}
a = (a * a) % mod;
b >>= 1;
}
return res;
}
int c1, c2;
bool dfs(int node) {
for(auto &j : adj[node]) {
if(color[j] == -1) {
color[j] = (1 ^ color[node]);
if(color[j] == 0) c1++;
else c2++;
if(!dfs(j)) return false;
}
else if(color[j] == color[node]) {
return false;
}
}
return true;
}
void solve() {
int n, m;
cin >> n >> m;
for(int i=0;i<=n;i++) {
color[i] = -1;
adj[i].clear();
}
if(m == 0) {
cout << modpwr(3, n) << "\n";
return;
}
for(int i=0;i<m;i++) {
int u, v;
cin >> u >> v;
adj[u].push_back(v);
adj[v].push_back(u);
}
long long ans = 1;
for(int i=1;i<=n;i++) {
if(color[i] == -1) {
color[i] = 0;
c1 = 1, c2 = 0;
if(!dfs(i)) {
cout << "0\n";
return;
}
int ways = (modpwr(2, c1) + modpwr(2, c2)) % mod;
ans = (ans * ways) % mod;
}
}
cout << ans << "\n";
}
int32_t main(){
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
cout.tie(nullptr);
int _;
cin >> _;
while (_-->0) {
solve();
}
return 0;
}