-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathE_By_the_Assignment.cpp
More file actions
116 lines (105 loc) · 2.54 KB
/
E_By_the_Assignment.cpp
File metadata and controls
116 lines (105 loc) · 2.54 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
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
#include <iostream>
#include <vector>
#include <numeric>
#include <algorithm>
#include<bits/stdc++.h>
using namespace std;
long long power(long long base, long long exp) {
long long res = 1;
long long mod = 998244353;
base %= mod;
while (exp > 0) {
if (exp % 2 == 1) res = (res * base) % mod;
base = (base * base) % mod;
exp /= 2;
}
return res;
}
void solve() {
int n;
long long m, V;
cin >> n >> m >> V;
vector<long long> a(n + 1);
int unknown_count = 0;
for (int i = 1; i <= n; ++i) {
cin >> a[i];
if (a[i] == -1) {
unknown_count++;
}
}
vector<vector<int>> adj(n + 1);
for (int i = 0; i < m; ++i) {
int u, v;
cin >> u >> v;
adj[u].push_back(v);
adj[v].push_back(u);
}
if (m == n - 1) {
cout << power(V, unknown_count) << endl;
return;
}
bool is_bipartite = true;
vector<int> color(n + 1, 0); // 0: uncolored, 1: color 1, 2: color 2
bool possible = true;
function<void(int, int)> bipartite_check_dfs = [&](int u, int c) {
color[u] = c;
for (int v : adj[u]) {
if (color[v] == 0) {
bipartite_check_dfs(v, 3 - c);
} else if (color[v] == color[u]) {
is_bipartite = false;
}
}
};
for (int i = 1; i <= n; ++i) {
if (color[i] == 0) {
bipartite_check_dfs(i, 1);
}
}
if (is_bipartite) {
long long c_val = -1;
bool conflict = false;
for (int i = 1; i <= n; ++i) {
if (a[i] != -1) {
if (c_val == -1) {
c_val = a[i];
} else if (c_val != a[i]) {
conflict = true;
break;
}
}
}
if (conflict) {
cout << 0 << endl;
} else {
if (c_val != -1) {
cout << 1 << endl;
} else {
cout << V % 998244353 << endl;
}
}
} else { // Not bipartite
bool conflict = false;
for (int i = 1; i <= n; ++i) {
if (a[i] != -1 && a[i] != 0) {
conflict = true;
break;
}
}
if (conflict) {
cout << 0 << endl;
} else {
cout << 1 << endl;
}
}
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
int t;
cin >> t;
while (t--) {
solve();
}
return 0;
}