From a9093726f5bdaf33879c66556ae6ef688c736733 Mon Sep 17 00:00:00 2001 From: "Paulo H. Lamounier" <53798700+Nanashii76@users.noreply.github.com> Date: Tue, 7 May 2024 15:39:33 -0300 Subject: [PATCH] some problemsets from Rosaling --- rosalind/binary_search.cpp | 39 ++++++++++++++++++++++++ rosalind/breadth_first_search.cpp | 43 +++++++++++++++++++++++++++ rosalind/connected_components.cpp | 45 ++++++++++++++++++++++++++++ rosalind/degree_array.cpp | 21 +++++++++++++ rosalind/dijkstras_algorithm.cpp | 49 +++++++++++++++++++++++++++++++ rosalind/double_degree_array.cpp | 25 ++++++++++++++++ rosalind/fibonacci_numbers.cpp | 27 +++++++++++++++++ rosalind/majority_element.cpp | 28 ++++++++++++++++++ 8 files changed, 277 insertions(+) create mode 100644 rosalind/binary_search.cpp create mode 100644 rosalind/breadth_first_search.cpp create mode 100644 rosalind/connected_components.cpp create mode 100644 rosalind/degree_array.cpp create mode 100644 rosalind/dijkstras_algorithm.cpp create mode 100644 rosalind/double_degree_array.cpp create mode 100644 rosalind/fibonacci_numbers.cpp create mode 100644 rosalind/majority_element.cpp diff --git a/rosalind/binary_search.cpp b/rosalind/binary_search.cpp new file mode 100644 index 0000000..91c6487 --- /dev/null +++ b/rosalind/binary_search.cpp @@ -0,0 +1,39 @@ +#include +using namespace std; + +int binary_search(vector values, int x){ + + int l = 0; + int r = values.size()-1; + + while(l <= r) { + int mid = (l+r)/2; + + if(values[mid] == x) + return mid; + else if(x < values[mid]) + r = mid-1; + else + l = mid+1; + } + + return -1; + +} + + +int main() { + + int n,m; cin >> n >> m; + vector values(n); + for(int i = 0; i < n; ++i) + cin >> values[i]; + + while(m--) { + int aux; cin >> aux; + cout << (binary_search(values,aux)!=-1?binary_search(values,aux)+1:-1) << " "; + } + cout << endl; + return 0; + +} \ No newline at end of file diff --git a/rosalind/breadth_first_search.cpp b/rosalind/breadth_first_search.cpp new file mode 100644 index 0000000..a9767c6 --- /dev/null +++ b/rosalind/breadth_first_search.cpp @@ -0,0 +1,43 @@ +#include +using namespace std; + +const int MAX = 1e3+10; +vector> g(MAX); +vector vis(MAX); +vector dist(MAX,-1); + +void bfs(int s) { + + queue q; q.push(s); + vis[s] = true; + dist[s] = 0; + + while(not q.empty()) { + int v = q.front(); + q.pop(); + + for(auto x : g[v]) if(not vis[x]){ + dist[x] = dist[v] + 1; + q.push(x); + vis[x] = true; + } + } +} + + +int main(){ + + int v,e; cin >> v >> e; + while(e--) { + int va,vb; cin >> va >> vb; + va--,vb--; + g[va].emplace_back(vb); + } + + bfs(0); + for(int i = 0; i < v; ++i) + cout << dist[i] << " "; + cout << endl; + + return 0; +} \ No newline at end of file diff --git a/rosalind/connected_components.cpp b/rosalind/connected_components.cpp new file mode 100644 index 0000000..52049b8 --- /dev/null +++ b/rosalind/connected_components.cpp @@ -0,0 +1,45 @@ +#include +using namespace std; + +void dfs(vector> g, vector &vis, int s) { + + stack st; + st.push(s); + vis[s] = 1; + + while(not st.empty()) { + int v = st.top(); + st.pop(); + + for(auto x : g[v]) if(not vis[x]){ + st.push(x); + vis[x] = 1; + } + } + +} + +int main(){ + + int v,e; cin >> v >> e; + vector> g(v); + vector vis(v,0); + while(e--) { + int va,vb; cin >> va >> vb; + va--,vb--; + g[va].emplace_back(vb); + g[vb].emplace_back(va); + } + + int count = 0; + for(int i = 0; i < v; ++i) { + if(not vis[i]) { + dfs(g,vis,i); + count++; + } + } + + cout << count << endl; + + return 0; +} \ No newline at end of file diff --git a/rosalind/degree_array.cpp b/rosalind/degree_array.cpp new file mode 100644 index 0000000..32884cc --- /dev/null +++ b/rosalind/degree_array.cpp @@ -0,0 +1,21 @@ +#include +using namespace std; + +int main() { + + int v,e; cin >> v >> e; + vector> g(v); + while(e--) { + int va,vb; cin >> va >> vb; + va--,vb--; + g[va].emplace_back(vb); + g[vb].emplace_back(va); + } + + for(auto v : g) { + cout << v.size() << " "; + } + cout << endl; + + return 0; +} \ No newline at end of file diff --git a/rosalind/dijkstras_algorithm.cpp b/rosalind/dijkstras_algorithm.cpp new file mode 100644 index 0000000..b0b8edf --- /dev/null +++ b/rosalind/dijkstras_algorithm.cpp @@ -0,0 +1,49 @@ +#include +using namespace std; + +vector dijktras(vector>> g, int V, int s) { + + priority_queue,vector>, greater>> pq; + vector dist(V,INT_MAX); + dist[s] = 0; + pq.push({0,s}); // distance and node; + + while(not pq.empty()) { + int dis = pq.top().first, node = pq.top().second; + pq.pop(); + + if(dis != dist[node]) continue; + + for(auto it : g[node]) { + int adjNode = it.first; + int adjEdge = it.second; + + if(dis + adjEdge < dist[adjNode]) { + dist[adjNode] = dis + adjEdge; + pq.push({dist[adjNode],adjNode}); + } + } + } + + return dist; + +} + + +int main(){ + + int v,e; cin >> v >> e; + vector>> g(v); + while(e--) { + int va,vb,coast; cin >> va >> vb >> coast; + va--,vb--; + g[va].emplace_back(make_pair(vb,coast)); + } + + vector ans = dijktras(g,v,0); + for(auto x : ans) + cout << (x!=INT_MAX?x:-1) << " "; + cout << endl; + + return 0; +} \ No newline at end of file diff --git a/rosalind/double_degree_array.cpp b/rosalind/double_degree_array.cpp new file mode 100644 index 0000000..7cd55e6 --- /dev/null +++ b/rosalind/double_degree_array.cpp @@ -0,0 +1,25 @@ +#include +using namespace std; + +int main(){ + + int v, e; cin >> v >> e; + vector> g(v); + while(e--) { + int va,vb; cin >> va >> vb; + va--,vb--; + g[va].emplace_back(vb); + g[vb].emplace_back(va); + } + + + for(auto elements : g) { + int count = 0; + for(auto x : elements) + count += (int)g[x].size(); + cout << count << " "; + } + cout << endl; + + return 0; +} \ No newline at end of file diff --git a/rosalind/fibonacci_numbers.cpp b/rosalind/fibonacci_numbers.cpp new file mode 100644 index 0000000..f90fec4 --- /dev/null +++ b/rosalind/fibonacci_numbers.cpp @@ -0,0 +1,27 @@ +#include +using namespace std; + +const int MAX = 100; +vector values(MAX,-1); + +int fib(int n) { + + if(n < 2) { + values[n] = n; + return values[n]; + } + + if(values[n] != -1) + return values[n]; + + return values[n] = fib(n-1) + fib(n-2); + +} + +int main(){ + + int n; cin >> n; + cout << fib(n) << endl; + + return 0; +} \ No newline at end of file diff --git a/rosalind/majority_element.cpp b/rosalind/majority_element.cpp new file mode 100644 index 0000000..fd3608d --- /dev/null +++ b/rosalind/majority_element.cpp @@ -0,0 +1,28 @@ +#include +using namespace std; + + +int main() { + + int n, m; cin >> n >> m; + while(n--) { + unordered_map unmap; + for(int i = 0; i < m; ++i) { + int aux; cin >> aux; + unmap[aux]++; + } + + bool has = false; + for(auto element : unmap) + if(element.second > m/2) { + cout << element.first << " "; + has=true; + } + + if(not has) + cout << "-1" << " "; + } + cout << endl; + return 0; + +} \ No newline at end of file