-
Notifications
You must be signed in to change notification settings - Fork 73
/
Copy pathmain.cpp
54 lines (49 loc) · 1.21 KB
/
main.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
#include <cstdlib>
#include <iostream>
#include <map>
#include <sstream>
#include <string>
#include <vector>
using namespace std;
class Solution {
public:
bool validTree(int n, vector<pair<int, int>>& edges) {
vector<vector<int>> adj(n);
for (auto e : edges) {
adj[e.first].push_back(e.second);
adj[e.second].push_back(e.first);
}
vector<bool> on_path(n, false), visited(n, false);
if (hasCycle(adj, 0, -1, on_path, visited)) {
return false;
}
for (bool v : visited) {
if (!v) {
return false;
}
}
return true;
}
private:
bool hasCycle(vector<vector<int>>& adj, int k, int parent,
vector<bool>& on_path, vector<bool>& visited) {
if (on_path[k]) {
return true;
}
on_path[k] = visited[k] = true;
for (auto v : adj[k]) {
if (v != parent && hasCycle(adj, v, k, on_path, visited)) {
return true;
}
}
return on_path[k] = false;
}
};
int main() {
Solution sol;
vector<pair<int, int>> v1 = {{0, 1}, {0, 2}, {0, 3}, {1, 4}};
cout << sol.validTree(5, v1) << endl;
vector<pair<int, int>> v2 = {{0, 1}, {1, 2}, {2, 3}, {1, 3}, {1, 4}};
cout << sol.validTree(5, v2) << endl;
return 0;
}