Skip to content

Instantly share code, notes, and snippets.

@sorae42
Created June 19, 2023 16:12
Show Gist options
  • Select an option

  • Save sorae42/2c59895d6df0e114e3f9395b68f16fc2 to your computer and use it in GitHub Desktop.

Select an option

Save sorae42/2c59895d6df0e114e3f9395b68f16fc2 to your computer and use it in GitHub Desktop.
#include <iostream>
#include <fstream>
#include <vector>
using namespace std;
#include <iostream>
#include <queue>
using namespace std;
struct Node {
string name;
Node* left;
Node* right;
};
Node* createNode(string name) {
Node* newNode = new Node;
newNode->name = name;
newNode->left = newNode->right = NULL;
return newNode;
}
Node* createFamilyTree() {
Node* john = createNode("John");
Node* steve = createNode("Steve");
Node* rohan = createNode("Rohan");
Node* lee = createNode("Lee");
Node* bob = createNode("Bob");
Node* sal = createNode("Sal");
Node* emma = createNode("Emma");
Node* tom = createNode("Tom");
Node* raj = createNode("Raj");
Node* bill = createNode("Bill");
john->left = steve;
john->right = rohan;
steve->left = lee;
steve->right = bob;
rohan->left = sal;
rohan->right = emma;
emma->left = tom;
emma->right = raj;
tom->left = bill;
return john;
}
void listPeopleWithoutChildren(Node* root) {
if (root == NULL)
return;
queue<Node*> q;
q.push(root);
cout << "Nhung nguoi chua co con trong cay la: ";
while (!q.empty()) {
Node* current = q.front();
q.pop();
if (current->left == NULL && current->right == NULL) {
cout << current->name << " ";
}
if (current->left != NULL)
q.push(current->left);
if (current->right != NULL)
q.push(current->right);
}
cout << endl;
}
int main() {
Node* root = createFamilyTree();
listPeopleWithoutChildren(root);
return 0;
}
vector<int> getDirectLinks(const vector<vector<int>>& graph, int web);
vector<int> getAllLinks(const vector<vector<int>>& graph, int web);
int main() {
ifstream file("graph.txt");
int n;
file >> n;
vector<vector<int>> graph(n, vector<int>(n, 0));
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
file >> graph[i][j];
}
}
file.close();
int k = 0;
vector<int> directLinks = getDirectLinks(graph, k);
cout << "Cac trang web co the link truc tiep tu trang web " << k << ": ";
for (int i = 0; i < directLinks.size(); i++) {
cout << directLinks[i] << " ";
}
cout << endl;
int x = 0;
vector<int> allLinks = getAllLinks(graph, x);
cout << "Cac trang web ma trang web " << x << " co the link truc tiep hoac gian tiep: ";
for (int i = 0; i < allLinks.size(); i++) {
cout << allLinks[i] << " ";
}
cout << endl;
return 0;
}
vector<int> getDirectLinks(const vector<vector<int>>& graph, int web) {
vector<int> directLinks;
for (int i = 0; i < graph[web].size(); i++) {
if (graph[web][i] == 1) {
directLinks.push_back(i);
}
}
return directLinks;
}
vector<int> getAllLinks(const vector<vector<int>>& graph, int web) {
vector<int> allLinks;
vector<bool> visited(graph.size(), false);
visited[web] = true;
for (int i = 0; i < graph[web].size(); i++) {
if (graph[web][i] == 1 && !visited[i]) {
visited[i] = true;
vector<int> links = getAllLinks(graph, i);
allLinks.insert(allLinks.end(), links.begin(), links.end());
}
}
for (int i = 0; i < visited.size(); i++) {
if (visited[i] && i != web) {
allLinks.push_back(i);
}
}
return allLinks;
}
#include <iostream>
#include <fstream>
#include <vector>
using namespace std;
vector<int> getDirectLinks(const vector<vector<int>>& graph, int web);
vector<int> getAllLinks(const vector<vector<int>>& graph, int web);
int main() {
ifstream file("graph.txt");
int n;
file >> n;
vector<vector<int>> graph(n, vector<int>(n, 0));
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
file >> graph[i][j];
}
}
file.close();
int k = 0;
vector<int> directLinks = getDirectLinks(graph, k);
cout << "Cac trang web co the link truc tiep tu trang web " << k << ": ";
for (int i = 0; i < directLinks.size(); i++) {
cout << directLinks[i] << " ";
}
cout << endl;
int x = 0;
vector<int> allLinks = getAllLinks(graph, x);
cout << "Cac trang web ma trang web " << x << " co the link truc tiep hoac gian tiep: ";
for (int i = 0; i < allLinks.size(); i++) {
cout << allLinks[i] << " ";
}
cout << endl;
return 0;
}
vector<int> getDirectLinks(const vector<vector<int>>& graph, int web) {
vector<int> directLinks;
for (int i = 0; i < graph[web].size(); i++) {
if (graph[web][i] == 1) {
directLinks.push_back(i);
}
}
return directLinks;
}
vector<int> getAllLinks(const vector<vector<int>>& graph, int web) {
vector<int> allLinks;
vector<bool> visited(graph.size(), false);
visited[web] = true;
for (int i = 0; i < graph[web].size(); i++) {
if (graph[web][i] == 1 && !visited[i]) {
visited[i] = true;
vector<int> links = getAllLinks(graph, i);
allLinks.insert(allLinks.end(), links.begin(), links.end());
}
}
for (int i = 0; i < visited.size(); i++) {
if (visited[i] && i != web) {
allLinks.push_back(i);
}
}
return allLinks;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment