Site Message

Only Premium Users can view the Question

Site Message

Only Premium Users can view the Question

Site Message

Only Premium Users can view the Question

Site Message

Only Premium Users can view the Question

Question: Cisco, Recent Online Assessment Questions | Is this a Tree? | Paths in a Warehouse | 15th August 2023
3
Entering edit mode

ADD COMMENTlink 2.5 years ago PoGo 2.5k
0
Entering edit mode

#include<bits/stdc++.h>

using namespace std;

 

string dfs(int node, vector<vector<int>>& adj){

    string res = "";

    res.push_back(node+'A');

 

    for(auto it: adj[node]){

        res += '(' + dfs(it, adj) +')';

    }

    return res;

}

 

int main(){

    int n = 8;

    vector<vector<char>> v({{'A','B'}, {'A', 'C'}, {'B', 'G'}, {'C', 'H'}, {'E', 'F'}, {'B', 'D'}, {'C', 'E'}});

 

    vector<vector<int>> adj(8);

    vector<int> parent(8, -1);

    for(auto e: v){

        adj[e[0]-'A'].push_back(e[1]-'A');

        parent[e[1]-'A'] = e[0]-'A';

    }

    for(auto &it:adj){

        sort(it.begin(), it.end());

    }

    int root;

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

        if(parent[i]==-1)   root = i;

    }

 

    cout<<'(' + dfs(root, adj)+')'<<endl;

 

    return 0;

}

ADD COMMENTlink 2.5 years ago noobs • 0

Login before adding your answer.

Similar Posts
Loading Similar Posts