
import networkx as nx

def afegeix_cabals1(gr, dc):
    for riu in dc:
        if riu in gr:
            gr.nodes[riu]['cabal'] = dc[riu]

def afegeix_cabals2(gr, dc):
    nx.set_node_attributes(gr, dc, name='cabal')

def max_tributaris(gr, nriu):
    nmax = -1
    amax = ''
    for afluent in gr.predecessors(nriu):
        ntrib = len(nx.ancestors(gr, afluent))
        if ntrib > nmax:
            nmax = ntrib
            amax = afluent
    return amax

# tria la solució que desitgis provar
afegeix_cabals = afegeix_cabals1

