Skip to content

Commit aa2eba0

Browse files
committed
Added new stuff
1 parent ad2cbd9 commit aa2eba0

File tree

1 file changed

+57
-0
lines changed

1 file changed

+57
-0
lines changed

Advanced Graphs/Matrix.cpp

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
/*
2+
3+
Name: Mehul Chaturvedi
4+
IIT-Guwahati
5+
6+
*/
7+
8+
/*
9+
PROBLEM STATEMENT
10+
11+
*/
12+
13+
#include <bits/stdc++.h>
14+
using namespace std;
15+
const int N = 1e5+5;
16+
bool error; int col[N];
17+
vector<pair<int,int> > adj[N];
18+
void dfs(int start){
19+
for(auto edge: adj[start]){
20+
int next = edge.first;
21+
if(col[next] == -1){
22+
col[next] = col[start]^edge.second;
23+
dfs(next);
24+
}
25+
else if(col[next] != col[start]^edge.second){
26+
error = true;
27+
}
28+
}
29+
}
30+
int main(){
31+
ios_base::sync_with_stdio(false);
32+
cin.tie(0); cout.tie(0);
33+
int t; cin>>t;
34+
while(t--){
35+
int n,m;
36+
cin>>n>>m;
37+
error = false;
38+
for(int i=1;i<=n;i++){
39+
adj[i].clear();
40+
col[i] = -1;
41+
}
42+
while(m--){
43+
int a,b,c;
44+
cin>>a>>b>>c;
45+
adj[a].push_back({b,c});
46+
adj[b].push_back({a,c});
47+
}
48+
for(int i=1;i<=n;i++){
49+
if(col[i] == -1){
50+
col[i] = 0;
51+
dfs(i);
52+
}
53+
}
54+
if(error) cout<<"no"<<endl;
55+
else cout<<"yes"<<endl;
56+
}
57+
}

0 commit comments

Comments
 (0)