Suma de dependencias en un gráfico

Dado un grafo dirigido y conexo con n Nodes. Si hay un borde de u a v, entonces u depende de v. Nuestra tarea era encontrar la suma de las dependencias para cada Node.
 

C++

// C++ program to find the sum of dependencies
#include <bits/stdc++.h>
using namespace std;
 
// To add an edge
void addEdge(vector <int> adj[], int u,int v)
{
    adj[u].push_back(v);
}
 
// find the sum of all dependencies
int findSum(vector<int> adj[], int V)
{
    int sum = 0;
 
    // just find the size at each vector's index
    for (int u = 0; u < V; u++)
        sum += adj[u].size();
 
    return sum;
}
 
// Driver code
int main()
{
    int V = 4;
    vector<int >adj[V];
    addEdge(adj, 0, 2);
    addEdge(adj, 0, 3);
    addEdge(adj, 1, 3);
    addEdge(adj, 2, 3);
 
    cout << "Sum of dependencies is "
         << findSum(adj, V);
    return 0;
}

Java

// Java program to find the sum of dependencies
 
import java.util.Vector;
 
class Test
{
    // To add an edge
    static void addEdge(Vector <Integer> adj[], int u,int v)
    {
        adj[u].addElement((v));
    }
     
    // find the sum of all dependencies
    static int findSum(Vector<Integer> adj[], int V)
    {
        int sum = 0;
      
        // just find the size at each vector's index
        for (int u = 0; u < V; u++)
            sum += adj[u].size();
      
        return sum;
    }
     
    // Driver method
    public static void main(String[] args)
    {
        int V = 4;
          @SuppressWarnings("unchecked")
        Vector<Integer> adj[] = new Vector[V];
         
        for (int i = 0; i < adj.length; i++) {
            adj[i] = new Vector<>();
        }
         
        addEdge(adj, 0, 2);
        addEdge(adj, 0, 3);
        addEdge(adj, 1, 3);
        addEdge(adj, 2, 3);
      
        System.out.println("Sum of dependencies is " +
                            findSum(adj, V));
    }
}
// This code is contributed by Gaurav Miglani

Python3

# Python3 program to find the sum
# of dependencies
 
# To add an edge
def addEdge(adj, u, v):
 
    adj[u].append(v)
 
# Find the sum of all dependencies
def findSum(adj, V):
     
    sum = 0
     
    # Just find the size at each
    # vector's index
    for u in range(V):
        sum += len(adj[u])
         
    return sum
 
# Driver code
if __name__=='__main__':
 
    V = 4
    adj = [[] for i in range(V)]
     
    addEdge(adj, 0, 2)
    addEdge(adj, 0, 3)
    addEdge(adj, 1, 3)
    addEdge(adj, 2, 3)
     
    print("Sum of dependencies is",
          findSum(adj, V))
     
# This code is contributed by rutvik_56

C#

// C# program to find the sum of dependencies
using System;
using System.Collections;
 
class GFG{
     
// To add an edge
static void addEdge(ArrayList []adj, int u,
                                     int v)
{
    adj[u].Add(v);
}
 
// Find the sum of all dependencies
static int findSum(ArrayList []adj, int V)
{
    int sum = 0;
     
    // Just find the size at each
    // vector's index
    for(int u = 0; u < V; u++)
        sum += adj[u].Count;
  
    return sum;
}
 
// Driver code
public static void Main(string[] args)
{
    int V = 4;
       
    ArrayList []adj = new ArrayList[V];
     
    for(int i = 0; i < V; i++)
    {
        adj[i] = new ArrayList();
    }
     
    addEdge(adj, 0, 2);
    addEdge(adj, 0, 3);
    addEdge(adj, 1, 3);
    addEdge(adj, 2, 3);
  
    Console.Write("Sum of dependencies is " +
                  findSum(adj, V));
}
}
 
// This code is contributed by pratham76

Publicación traducida automáticamente

Artículo escrito por GeeksforGeeks-1 y traducido por Barcelona Geeks. The original can be accessed here. Licence: CCBY-SA

Deja una respuesta

Tu dirección de correo electrónico no será publicada. Los campos obligatorios están marcados con *