본문 바로가기

공부합시다

BFS 너비 우선 탐색 #include #include #include #include #include #include #include #include #include #include #define xx first #define yy second using namespace std; using i64 = long long; using ii = pair; using ii64 = pair; int main() { int n, m, v; scanf("%d %d %d", &n, &m, &v); vector adj(n+1); for (int i = 0; i < m; i++) { int u, v; scanf("%d %d", &u, &v); adj[u].push_back(v); adj[v].push_back(u); } for (int i .. 더보기
DFS 깊이 우선 탐색 #include #include #include #include #include #include #include #include #include #include #include #define xx first #define yy second using namespace std; using i64 = long long; using ii = pair; using ii64 = pair; bool visited[10005]; vector adj[10005]; void dfs(int curr) { visited[curr] = true; printf("%d ", curr); for (int next : adj[curr]) { if (!visited[next]) dfs(next); } } int main() { int.. 더보기
Union-find (disjoint set) 새로운 걸 배웠다 코드가 간단해서 너무 좋다 vector parent; void init(int n) { parent.resize(n + 1); for (int i = 0; i 더보기
N queen 더보기
이차원벡터 선언 vector arr(6, vector(5, 0)); arr[6][5] 맨날 까먹어서 맨날 찾아보네 더보기
lower_bound, upper_bound 사용 예시 사용방법 인덱스 위치 lower_bound(all(v), a[i]) - v.begin() 더보기
Quick Sort 🌲 Quick Sort by Hoare (1962) Strategy Choose a pivot item randomly Partition into 2 subarrays Sort each subarray recursively Ex 15 22 13 27 12 10 20 25 15 : pivot item 1. partition 10 13 12 15 22 27 20 25 remember pivot's position 2. Sort subarrays recursively 10 12 13 15 20 22 25 27 procedure quicksort (low, high, index); var pivotpoint : index; begin if high > low then { partition(low, high, p.. 더보기
Binary search 🎲 Binary search Task Assume that we have n ≥ 1 distinct integers that are already sorted and stored in the array list For a given integer m, we want to know whether there is 1 such that list[i] = m Input : a sorted array list[i], 0 ≤ i < n, an integer m Output: i if list[i] = m, -1 if there is no such i Specification let left = 0, rigth = n - 1, middle = (left + rigth) / 2 1. If m < list[middle].. 더보기