-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathf.cpp
61 lines (57 loc) · 1.44 KB
/
f.cpp
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
#include <bits/stdc++.h>
#define endl '\n'
#define eat cin
#define moo cout
#define int long long
using namespace std;
int T, N, M, v[200000], t[200000], alt[200000];
vector<int> graph[200000];
bool bipartite;
void dfs(int id, int par){
if(alt[id] == -1) alt[id] = (id ? !alt[par] : 0);
else{
if(alt[id] == alt[par]) bipartite = false;
return;
}
for(int i : graph[id]){
dfs(i, id);
}
}
int32_t main(){
eat.tie(0) -> sync_with_stdio(0);
eat >> T;
while(T--){
eat >> N >> M;
for(int i = 0; i < N; i++) graph[i].clear();
for(int i = 0; i < N; i++){
eat >> v[i];
v[i] += 1e9;
}
for(int i = 0; i < N; i++){
eat >> t[i];
t[i] += 1e9;
}
for(int i = 0; i < M; i++){
int u, v; eat >> u >> v;
graph[u-1].push_back(v-1);
graph[v-1].push_back(u-1);
}
if(accumulate(v, v+N, 0LL) % 2 != accumulate(t, t+N, 0LL) % 2){
moo << "NO" << endl;
continue;
}
bipartite = true;
fill(alt, alt+N, -1);
dfs(0, -1);
if(!bipartite){
moo << "YES" << endl;
continue;
}
int diff0 = 0, diff1 = 0;
for(int i = 0; i < N; i++){
if(alt[i]) diff1 += v[i] - t[i];
else diff0 += v[i] - t[i];
}
moo << (diff0 == diff1 ? "YES" : "NO") << endl;
}
}