-
Notifications
You must be signed in to change notification settings - Fork 0
/
beautifulVertex.cpp
55 lines (47 loc) · 1.13 KB
/
beautifulVertex.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
#include <iostream>
#include <vector>
#include <cstring>
#include <queue>
using namespace std;
int dfs(int u, const vector<vector<int>> &adj, bool *visited) {
visited[u] = true;
int result = 1;
for (int v : adj[u]) {
if (!visited[v])
result += dfs(v, adj, visited);
}
return result;
}
int bfs(int s, const vector<vector<int>> &adj, bool *visited) {
visited[s] = true;
queue<int> Q;
Q.push(s);
int result = 1;
while(!Q.empty()) {
int u = Q.front(); Q.pop();
for (int v : adj[u]) {
if (!visited[v]) {
visited[v] = true;
result++;
Q.push(v);
}
}
}
return result;
}
int beautifulVertex(int n, std::vector<std::vector<int>> edges)
{
vector<vector<int>> adj(n + 1);
bool *visited = new bool[n + 1];
for (vector<int> x : edges) {
adj[x[0]].push_back(x[1]);
}
for (int i = 1; i <= n; i++) {
for (int i = 1; i <= n; i++) visited[i] = false;
if (bfs(i, adj, visited) == n) return i;
}
return -1;
}
int main() {
return 0;
}