-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathFill The Matrix.cpp
52 lines (49 loc) · 1.13 KB
/
Fill The Matrix.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
// https://www.codechef.com/problems/FILLMTR
// Concept used : Disjoint Set union
#include<bits/stdc++.h>
#define int long long int
#define nl cout<<"\n";
using namespace std;
vector<int> par(100009),dsuarr(100009);
int dsufind(int a){
if(a==dsuarr[a]) return a;
int tmp = dsuarr[a];
dsuarr[a] = dsufind(tmp);
par[a] = par[a]^par[tmp];
return dsuarr[a];
}
bool dsuunion(int a,int b,int val){
int tmpa = dsufind(a);
int tmpb = dsufind(b);
if(tmpa==tmpb){
if(val!=(par[a]^par[b])) return false;
else return true;
}
dsuarr[tmpa] = tmpb;
par[tmpa] = par[a]^par[b]^val;
return true;
}
int32_t main(){
int test = 0;
cin>>test;
while(test--){
int n=0,q=0;
cin>>n>>q;
for(int i=0;i<n;i++){
dsuarr[i] = i;
par[i] = 0;
}
bool ans = true;
while(q--){
int a=0,b=0,c=0;
cin>>a>>b>>c;
a--,b--;
if(!dsuunion(a,b,c)) ans = false;
if(a==b and c==1) ans = false;
}
if(ans) cout<<"yes";
else cout<<"no";
nl
}
return 0;
}