You are on page 1of 11

DEPARTMENT OF

COMPUTER SCIENCE & ENGINEERING

COMPTETIVE CODING-I
Experiment 5
Student Name: ABHISHEK KUMAR UID: 20BCS5900
Branch: C.S.E. Section/Group: 20BCS_MM_805_A
Semester: 5th Subject Code: 20CSP-314

1. Aim/Overview of the practical:

GRAPH
2. Task to be done/ Which logistics used:

Hackerrank.com

3. Steps for experiment/practical/Code:

Problem link

https://www.hackerrank.com/challenges/bfsshortreach/problem
CODE:
#include <bits/stdc++.h>

using namespace std;


DEPARTMENT OF
COMPUTER SCIENCE & ENGINEERING

string ltrim(const string &);


string rtrim(const string &);
vector<string> split(const string &);

/*
* Complete the 'bfs' function below.
*
* The function is expected to return an INTEGER_ARRAY.
* The function accepts following parameters:
* 1. INTEGER n
* 2. INTEGER m
* 3. 2D_INTEGER_ARRAY edges
* 4. INTEGER s
*/

vector<int> bfs(int n, int m, vector<vector<int>> edges, int s) {


vector<vector<int>> graph(n, vector<int>());
for(int i=0; i<m; i++)
{
graph[edges[i][0]-1].push_back(edges[i][1]-1);
graph[edges[i][1]-1].push_back(edges[i][0]-1);
}

vector<int> output(n, -1);


vector<int> level(n, 0);

queue<int> q;
q.push(s-1);

while(!q.empty())
{
int node = q.front();
q.pop();

output[node] = level[node] * 6;

for(int i=0; i<graph[node].size(); i++)


{
int adj_node = graph[node][i];
DEPARTMENT OF
COMPUTER SCIENCE & ENGINEERING

q.push(adj_node);
if(level[adj_node]==0)
level[adj_node] = level[node] + 1;
}

graph[node].clear();
}

output.erase(output.begin() + s - 1);

return output;
}

int main()
{
ofstream fout(getenv("OUTPUT_PATH"));

string q_temp;
getline(cin, q_temp);

int q = stoi(ltrim(rtrim(q_temp)));

for (int q_itr = 0; q_itr < q; q_itr++) {


string first_multiple_input_temp;
getline(cin, first_multiple_input_temp);

vector<string> first_multiple_input = split(rtrim(first_multiple_i


nput_temp));

int n = stoi(first_multiple_input[0]);

int m = stoi(first_multiple_input[1]);

vector<vector<int>> edges(m);

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


edges[i].resize(2);

string edges_row_temp_temp;
getline(cin, edges_row_temp_temp);
DEPARTMENT OF
COMPUTER SCIENCE & ENGINEERING

vector<string> edges_row_temp = split(rtrim(edges_row_temp_tem


p));

for (int j = 0; j < 2; j++) {


int edges_row_item = stoi(edges_row_temp[j]);

edges[i][j] = edges_row_item;
}
}

string s_temp;
getline(cin, s_temp);

int s = stoi(ltrim(rtrim(s_temp)));

vector<int> result = bfs(n, m, edges, s);

for (size_t i = 0; i < result.size(); i++) {


fout << result[i];

if (i != result.size() - 1) {
fout << " ";
}
}

fout << "\n";


}

fout.close();

return 0;
}

string ltrim(const string &str) {


string s(str);

s.erase(
s.begin(),
find_if(s.begin(), s.end(), not1(ptr_fun<int, int>(isspace)))
DEPARTMENT OF
COMPUTER SCIENCE & ENGINEERING

);

return s;
}

string rtrim(const string &str) {


string s(str);

s.erase(
find_if(s.rbegin(), s.rend(), not1(ptr_fun<int, int>(isspace))).ba
se(),
s.end()
);

return s;
}

vector<string> split(const string &str) {


vector<string> tokens;

string::size_type start = 0;
string::size_type end = 0;

while ((end = str.find(" ", start)) != string::npos) {


tokens.push_back(str.substr(start, end - start));

start = end + 1;
}

tokens.push_back(str.substr(start));

return tokens;
}

3. Result/Output/Writing Summary:
DEPARTMENT OF
COMPUTER SCIENCE & ENGINEERING
DEPARTMENT OF
COMPUTER SCIENCE & ENGINEERING

Problem link
https://www.hackerrank.com/challenges/even-tree/problem

CODE:
#include <bits/stdc++.h>

using namespace std;

string ltrim(const string &);


string rtrim(const string &);
vector<string> split(const string &);

// Complete the evenForest function below.


int compute(vector<int> data[], int start, int total, int *totalcount){
if(data[start].size() == 0){
return 1;
}else{
int count = 1; //the actual node counts;
for(auto i : data[start]){
count += compute(data, i, total, totalcount);
}
if((total - count) % 2 == 0 && count %2 == 0){
*totalcount +=1;
}
return count;
}
}

// Complete the evenForest function below.


int evenForest(int t_nodes, int t_edges, vector<int> t_from, vector<int> t
_to) {
vector<int> data[t_nodes+1];
int r = 0;
int *totalcount = &r;
for(int i = 0; i < t_nodes; i++){
data[t_to[i]].push_back(t_from[i]);
}
DEPARTMENT OF
COMPUTER SCIENCE & ENGINEERING

for(auto i : data[1]){
cout << i << "\n";
compute(data, i, t_nodes, totalcount);
}
return *totalcount;
}

int main()
{
ofstream fout(getenv("OUTPUT_PATH"));

string t_nodes_edges_temp;
getline(cin, t_nodes_edges_temp);

vector<string> t_nodes_edges = split(rtrim(t_nodes_edges_temp));

int t_nodes = stoi(t_nodes_edges[0]);


int t_edges = stoi(t_nodes_edges[1]);

vector<int> t_from(t_edges);
vector<int> t_to(t_edges);

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


string t_from_to_temp;
getline(cin, t_from_to_temp);

vector<string> t_from_to = split(rtrim(t_from_to_temp));

int t_from_temp = stoi(t_from_to[0]);


int t_to_temp = stoi(t_from_to[1]);

t_from[i] = t_from_temp;
t_to[i] = t_to_temp;
}

int res = evenForest(t_nodes, t_edges, t_from, t_to);

fout << res << "\n";

fout.close();
DEPARTMENT OF
COMPUTER SCIENCE & ENGINEERING

return 0;
}

string ltrim(const string &str) {


string s(str);

s.erase(
s.begin(),
find_if(s.begin(), s.end(), not1(ptr_fun<int, int>(isspace)))
);

return s;
}

string rtrim(const string &str) {


string s(str);

s.erase(
find_if(s.rbegin(), s.rend(), not1(ptr_fun<int, int>(isspace))).ba
se(),
s.end()
);

return s;
}

vector<string> split(const string &str) {


vector<string> tokens;

string::size_type start = 0;
string::size_type end = 0;

while ((end = str.find(" ", start)) != string::npos) {


tokens.push_back(str.substr(start, end - start));

start = end + 1;
}

tokens.push_back(str.substr(start));
DEPARTMENT OF
COMPUTER SCIENCE & ENGINEERING

return tokens;
}

OUTPUT:
DEPARTMENT OF
COMPUTER SCIENCE & ENGINEERING

You might also like