-
Notifications
You must be signed in to change notification settings - Fork 33
/
Copy pathsol.cpp
43 lines (38 loc) · 849 Bytes
/
sol.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
/*
Minimally connected graphs and trees are equivalent and have the property that |E| = |V|-1.
We'll use this property here.
*/
#include <iostream>
#include <vector>
class Graph{
int num_vertices;
std::vector<int> *adj_list;
public:
Graph(int a){
num_vertices = a;
adj_list = new std::vector<int>[num_vertices];
}
void add_edge(int u, int v){
adj_list[u].push_back(v);
}
bool isMinimallyConnected(){
int num_edges = 0;
for (int i=0; i<num_vertices; i++){
num_edges+=adj_list[i].size();
}
return num_edges==num_vertices-1;
}
};
void test(){
Graph g(4);
g.add_edge(0, 1);
g.add_edge(0, 2);
g.add_edge(2, 3);
std::cout<<std::boolalpha<<g.isMinimallyConnected()<<'\n';
g.add_edge(3, 0);
std::cout<<std::boolalpha<<g.isMinimallyConnected()<<'\n';
}
int main(){
test();
return 0;
}