-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
9e94864
commit a909372
Showing
8 changed files
with
277 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
#include <bits/stdc++.h> | ||
using namespace std; | ||
|
||
int binary_search(vector<int> 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<int> 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; | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
#include <bits/stdc++.h> | ||
using namespace std; | ||
|
||
const int MAX = 1e3+10; | ||
vector<vector<int>> g(MAX); | ||
vector<bool> vis(MAX); | ||
vector<int> dist(MAX,-1); | ||
|
||
void bfs(int s) { | ||
|
||
queue<int> 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; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
#include <bits/stdc++.h> | ||
using namespace std; | ||
|
||
void dfs(vector<vector<int>> g, vector<int> &vis, int s) { | ||
|
||
stack<int> 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<vector<int>> g(v); | ||
vector<int> 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; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
#include <bits/stdc++.h> | ||
using namespace std; | ||
|
||
int main() { | ||
|
||
int v,e; cin >> v >> e; | ||
vector<vector<int>> 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; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
#include <bits/stdc++.h> | ||
using namespace std; | ||
|
||
vector<int> dijktras(vector<vector<pair<int,int>>> g, int V, int s) { | ||
|
||
priority_queue<pair<int,int>,vector<pair<int,int>>, greater<pair<int,int>>> pq; | ||
vector<int> 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<vector<pair<int,int>>> g(v); | ||
while(e--) { | ||
int va,vb,coast; cin >> va >> vb >> coast; | ||
va--,vb--; | ||
g[va].emplace_back(make_pair(vb,coast)); | ||
} | ||
|
||
vector<int> ans = dijktras(g,v,0); | ||
for(auto x : ans) | ||
cout << (x!=INT_MAX?x:-1) << " "; | ||
cout << endl; | ||
|
||
return 0; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
#include <bits/stdc++.h> | ||
using namespace std; | ||
|
||
int main(){ | ||
|
||
int v, e; cin >> v >> e; | ||
vector<vector<int>> 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; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
#include <bits/stdc++.h> | ||
using namespace std; | ||
|
||
const int MAX = 100; | ||
vector<int> 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; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
#include <bits/stdc++.h> | ||
using namespace std; | ||
|
||
|
||
int main() { | ||
|
||
int n, m; cin >> n >> m; | ||
while(n--) { | ||
unordered_map<int,int> 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; | ||
|
||
} |