-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathIs_it_a_tree.cpp
99 lines (75 loc) · 1.96 KB
/
Is_it_a_tree.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
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
#include<bits/stdc++.h>
using namespace std;
class Matrix
{
public:
template <class T>
static void input(vector<vector<T>> &A,int n,int m)
{
for (int i = 0; i < n; i++)
{
for (int j = 0; j < m; j++)
{
scanf("%d ",&A[i][j]);
}
}
}
template <class T>
static void print(vector<vector<T>> &A)
{
for (int i = 0; i < A.size(); i++)
{
for (int j = 0; j < A[i].size(); j++)
{
cout << A[i][j] << " ";
}
cout << endl;
}
}
};
class Solution {
public:
int isTree(int n, int m, vector<vector<int>> &adj) {
vector<int> parent(n, -1); // To store parent of each node
for (int i = 0; i < m; ++i) {
int u = adj[i][0];
int v = adj[i][1];
if (find(parent, u) == find(parent, v)) {
return 0; // Cycle detected
}
unionSets(parent, u, v);
}
int components = 0;
for (int i = 0; i < n; ++i) {
if (parent[i] == -1) {
components++;
if (components > 1) {
return 0; // More than one connected component
}
}
}
return 1;
}
private:
int find(vector<int> &parent, int node) {
return (parent[node] == -1) ? node : find(parent, parent[node]);
}
void unionSets(vector<int> &parent, int u, int v) {
parent[find(parent, u)] = find(parent, v);
}
};
int main(){
int t;
scanf("%d ",&t);
while(t--){
int n;
scanf("%d",&n);
int m;
scanf("%d",&m);
vector<vector<int>> edges(m, vector<int>(2));
Matrix::input(edges,m,2);
Solution obj;
int res = obj.isTree(n, m, edges);
cout<<res<<endl;
}
}