Question: SAP Labs | SDE-1 | On Campus | 29th November 2020
0
Entering edit mode
2 questions were given that need to be solved in an hour.

Questions were pretty easy! :)

Ques 1. was same as IIT-Delhi & IIT-BHU


Question - 1


Question - 2


ADD COMMENTlink 4.0 years ago mod2 870
0
Entering edit mode

Question1) 

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

void dfs(int node, vector<vector<int>>& adj, vector<bool>& visited, int& size) {
    visited[node] = true;
    size++;
    for (int neighbor : adj[node]) {
        if (!visited[neighbor]) {
            dfs(neighbor, adj, visited, size);
        }
    }
}

int helper(int n, vector<int> &from, vector<int> &to) {
    vector<bool> visited(n, false);
    int result = 0;
    
    vector<vector<int>> adj(n);
    for(int i=0; i<from.size(); i++){
        int u = from[i];
        int v = to[i];
        adj[u].push_back(v);
        adj[v].push_back(u);
    }

    for (int i = 0; i < n; i++) {
        if (!visited[i]) {
            int size = 0;
            dfs(i, adj, visited, size);
            result += ceil(sqrt(size));
        }
    }

    return result;
}

int main() {
    int n; 
    cin >> n;

    vector<int> from = {1,1,2,3,7};
    vector<int> to = {2,3,4,5,8};

    int result = helper(n, from, to);
    cout << result << endl;

    return 0;
}

ADD COMMENTlink 3 months ago hehehaha • 10

Login before adding your answer.

Similar Posts
Loading Similar Posts