0% found this document useful (0 votes)
50 views7 pages

CODECHEF WEEK-7 Assignment (Chirag)

The document describes an algorithm to perform breadth-first search (BFS) on a graph to find the shortest distances from a set of special source nodes to all other nodes. It initializes a queue with the distances and nodes, performs BFS by iteratively extracting the minimum distance node and exploring its neighbors, and updates their distances if shorter paths are found. The algorithm is applied to find the shortest path distances from a set of given source nodes to all nodes in the graph.

Uploaded by

prerna
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
50 views7 pages

CODECHEF WEEK-7 Assignment (Chirag)

The document describes an algorithm to perform breadth-first search (BFS) on a graph to find the shortest distances from a set of special source nodes to all other nodes. It initializes a queue with the distances and nodes, performs BFS by iteratively extracting the minimum distance node and exploring its neighbors, and updates their distances if shorter paths are found. The algorithm is applied to find the shortest path distances from a set of given source nodes to all nodes in the graph.

Uploaded by

prerna
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 7

The Next Big Thing 

CODE
#include <bits/stdc++.h>
using namespace std;

vector<int> NGE(vector<int> v)
{
vector<int> nge(v.size());
stack<int> st;
for (int i = 0; i < v.size(); i++)
{
while ((!st.empty()) && (v[i] > v[st.top()]))
{
nge[st.top()] = i;
st.pop();
}
st.push(i);
}
while (!st.empty())
{
nge[st.top()] = -1;
st.pop();
}
return nge;
}

int main()
{
int n, i;
cin >> n;
vector<int> vect;
for (int i = 0; i < n; i++)
{
int x;
cin >> x;
vect.push_back(x);
}
vector<int> nge = NGE(vect);
for (i = 0; i < n; i++)
cout << ((nge[i] == (-1)) ? -1 : vect[nge[i]]) << " ";
}

INPUT
5
2 1 6 4 5

Out of the Abyss


CODE
#include <bits/stdc++.h>
#include <time.h>
#define int long long int
#define pb push_back
#define mem(a, x) memset(a, x, sizeof a)
#define all(a) a.begin(), a.end()
#define scnarr(a, n) for (int i = 0; i < n; ++i) cin >> a[i]
#define vi vector<int>
#define si set<int>
#define pii pair <int, int>
#define sii set<pii>
#define vii vector<pii>
#define mii map <int, int>
#define faster ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL)
using namespace std;
using namespace chrono;
/*
----------------------------------------------------------------------
Things to remember : check for coners n = 1, pass references instead
*/
/* -------------------------------Solution Sarted--------------------------------------*/

//Constants
const int MOD = 1000000007; // 1e9 + 7
const int MAXN = 1000005; // 1e6 +5
const int INF = 100000000000005; // 1e15 +5

vi dist;

set<pair<int, int> > q;

void bfsutil(vi adj[], int s){


set<pair<int, int> >::iterator it;

for (int i = 0; i < adj[s].size(); i++){


int v = adj[s][i];
if (dist[s] + 1 < dist[v]){
it = q.find({ dist[v], v });
q.erase(it);
dist[v] = dist[s] + 1;
q.insert({ dist[v], v });
}
}

if (q.size() == 0)
return;

it = q.begin();
int next = it->second;
q.erase(it);

bfsutil(adj, next);
}

void bfs(vi adj[], int n, vi &s, int k){


vi source(n +1, 0);

for (int i = 0; i < k; i++)


source[s[i]] = 1;

for (int i = 1; i <= n; i++){


if (source[i]){
dist[i] = 0;
q.insert({ 0, i });
}
else {
dist[i] = INF;
q.insert({ INF, i });
}
}

auto itr = q.begin();


int start = itr->second;

bfsutil(adj, start);
}

void solve(){
int n, m, k, u, v, q;
cin >> n >> m >> k;

vi adj[n +1], special(k);

for(int i = 0; i < m; ++i){


cin >> u >> v;
adj[u].push_back(v);
adj[v].push_back(u);
}

for(int i = 0; i < k; ++i){


cin >> special[i];
}

dist = vi(n +1, INF);


bfs(adj, n, special, k);

cin >> q;
while(q--){
cin >> u;
cout << (dist[u] >= INF ? -1 : dist[u]) << endl;
}

return;
}

signed main(){
faster;
#ifndef ONLINE_JUDGE
freopen("ip.txt", "r", stdin);
freopen("op.txt", "w", stdout);
#endif
int t; cin >> t; while(t--)
solve();
return 0;
}

INPUT-
1

5 3 3

1 2

1 3

2 3
1 3 5

You might also like