I'm having trouble solving a c problem using graphs. I must solve it two times, one using adjacency matrix and another using adjacency list. If you guys can help me, I appreciate it! Here's the problem:
"Operating systems are large software artifacts made up of many packages.The installing of each package may require that other packages are already installed. Given a list of packages and a list of dependencies between packages,determine the number of dependents, the number of dependencies and the list of dependencies of each package."
Input:
"The first input line contains two integers separated by a blank space. The first one, N (1 ≤ N ≤ 100), is the number of packages from the operating system, which are identified by integers 1, 2,. . . , N. The second one, D (0 ≤ D ≤ 10000), is the number of dependency relations between packages. The next D lines contain the specification of the packages dependencies, each consisting of two integers ui and v , such that 1 ≤ ui, vi ≤ N, for 1 ≤ i ≤ D. The dependency specification means that installing the ui package requires prior installation of the v package. You can assume that there is no circular dependency."
Output:
"The output must consist of information about the packages and their dependencies. Each package (numbered from 1 to N) must appear immediately followed by two separate integers separeted by a space, indicating its number of dependents and dependencies, respectively. If there is at least one dependency, these two numbers must be followed by a space and then a list of the package numbers that need to be installed before it, where each package number is separated from other package numbers by a space. Do not put spaces to the right at the end of each line. There must be an exit line for each package of the operating system and, therefore, a total of N output lines."
Example(input and output lines):
4 3 (number of packages and number of dependencies) / 1 2 (package 1 depends on package 2) / 1 3 (package 1 depends on package 3) / 4 2 (package 4 depends on package 2) / 1 0 2 2 3 (information about package 1 and its dependencies) / 2 2 0 (information about package 2 and its dependencies) / 3 1 0 (information about package 3 and its dependencies) / 4 0 1 2 (information about package 4 and its dependencies)
#include <iostream>
using namespace std;
int main() {
int const N=100;
int A[N][N] = {0};
int n, d;
cin >> n >> d;
for (int i=0; i<d; i++) {
int dependent, dependence;
cin >> dependent >> dependence;
A[dependent-1][dependence-1] = 1;
}
for (int i=0; i<n; i++) {
cout << i+1;
int total = 0;
for (int j=0; j<n; j++) {
total += A[j][i];
}
cout << " " << total;
total = 0;
for(int j=0; j<n; j++) {
total += A[i][j];
}
cout << " " << total;
if (total > 0) {
for (int j=0; j<n; j++) {
if (A[i][j]) {
cout << " " << j+1;
}
}
}
cout << endl;
}
}
Comments
Leave a comment