-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathutils.cpp
More file actions
59 lines (50 loc) · 1.25 KB
/
Copy pathutils.cpp
File metadata and controls
59 lines (50 loc) · 1.25 KB
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
#include <bits/stdc++.h>
using namespace std;
#define vi vector<int>
/**
* Serves as my notes for competitive programming, and
* a collection of helper functions.
*/
bool isPrime(int num) {
if (num <= 1) return 0;
for (int i = 2; i * i <= num; i++)
if (num % i == 0) return 0;
return 1;
}
int binarySearch(int x, int N, const vi &Li) {
int L = 0, R = N - 1;
int mid;
while (L <= R) {
// int mid = L + (R - L) / 2; // prevent overflow (remembered from Kattis?)
mid = (L + R) / 2;
if (Li[mid] == x) return 1;
if (Li[mid] < x) L = mid + 1;
else R = mid - 1;
}
return 0;
}
void BFS(int k, int N) {
vector<vi> adj(N + 1);
queue<int> q;
q.push(k);
vector<bool> visited(N + 1, 0);
visited[1] = k;
while (!q.empty()) {
int cur = q.front();
q.pop();
for (int n : adj[cur]) {
if (!visited[n]) {
visited[n] = 1;
q.push(n);
}
}
}
}
/* Recursive DFS. Can use stack for iterative method. */
void DFS(int V, unordered_map<int, vi> &m, vector<bool> &DFSvisited) {
int cur = V;
DFSvisited[cur] = 1;
for (auto &x : m[cur]) {
if (!DFSvisited[x]) DFS(x, m, DFSvisited);
}
}