Cod sursa(job #2594494)

Utilizator angelaAngela Visuian angela Data 6 aprilie 2020 09:32:07
Problema Componente tare conexe Scor 30
Compilator py Status done
Runda Arhiva educationala Marime 2.76 kb
import sys

class Graph:
    def __init__(self):
        self.n = 0        # vertices  
        self.m = 0        # edges
        self.adj = {}     # dictionary of (adjacent) lists ; 
        self.lv = []
        self.low = []
        self.stack = []
        self.inStack = []
        self.scc = []
        self.index = 0
        
    def readGraph(self, fileName):
        try:
            f = open(fileName, "r")
            
            line = f.readline().strip()
            line = line.split(" ")
            
            self.n = int(line[0])
            self.m = int(line[1])
            
            self.adj = {k: [] for k in range(1, self.n + 1)} # now we know the number (n) of empty lists as values in dictionary 
            self.lv = [0] * (self.n + 1)
            self.low = [0] * (self.n + 1)
            self.inStack = [False] * (self.n + 1)
            
            for i in range(self.m):
                line = f.readline().strip()
                line = line.split(' ')
                x = int(line[0])
                y = int(line[1])
             #   print(x, y)
                self.adj[x].append(y)
               
            f.close()
        except IOError as e:
            print("An error occured - " + str(e))
            raise e
        
    def tarjan(self, x):
        self.index += 1
        self.lv[x] = self.low[x] = self.index
        self.stack.append(x)
        self.inStack[x] = True
        
        for y in self.adj[x]:
            if self.lv[y] == 0:
                self.tarjan(y)
                self.low[x] = min(self.low[x], self.low[y])
            else:
                if self.inStack[y] == True:
                    self.low[x] = min(self.low[x], self.lv[y])
        
        if self.lv[x] == self.low[x] :
            c = []
            while True:
                node = self.stack.pop()
                self.inStack[node] = False
                c.append(node)
                if node == x:
                    break;
                    
            self.scc.append(c)
        
    def stronglyConnectedComponents(self):
        for x in range(1, self.n + 1):
            if self.lv[x] == 0:
                 self.tarjan(x)
                 
    def printSCC(self, fileName):
         try:
            f = open(fileName, "w")
            f.write(str(len(self.scc)))
            f.write('\n')
            for comp in self.scc:
            #    comp.sort()
                for x in comp:
                    f.write(str(x))
                    f.write(' ')
                f.write('\n')
  
            f.close()
         except IOError as e:
            print("An error occured - " + str(e))
            raise e
        
    def printAdjList(self):
        print(self.adj)
        
g = Graph()
g.readGraph('ctc.in')
g.stronglyConnectedComponents()
g.printSCC('ctc.out')