Submission #1166107

#TimeUsernameProblemLanguageResultExecution timeMemory
1166107u6714508timeismoney (balkan11_timeismoney)C++20
Compilation error
0 ms0 KiB
class unionf:
    def __init__(self, n):
        self.parent = list(range(n))
        self.rank = [0] * n
        
    def find(self, u):
        if self.parent[u] != u:
            self.parent[u] = self.find(self.parent[u])
        return self.parent[u]

    def union(self, u, v):
         rootu = self.find(u)
         rootv = self.find(v)
         if rootu != rootv:
             if self.rank[rootu] > self.rank[rootv]:
                 self.parent[rootv] = rootu
             elif self.rank[rootu] < self.rank[rootv]:
                 self.parent[rootu] = rootv
             else:
                 self.parent[rootv] = rootu
                 self.rank[rootu] += 1

def kruskal(n ,map1):
    map1.sort(key=lambda x: (x[2], x[3]))
    uf  = unionf(n)
    mst = []
    sumt , sumc = 0 , 0
    for u , v , t ,c in map1:
        if uf.find(u) != uf.find(v):
            uf.union(u,v)
            mst.append((u, v))
            sumt += t
            sumc += c
        if len(mst) == n-1:
              break
    return sumt , sumc , mst

N, M = map(int, input().split())
edges = [tuple(map(int, input().split())) for _ in range(M)]
#N = 5 
#M = 7 
#edges = [
#    (0, 1, 161, 79),
#    (0, 2, 161, 15),
#   (0, 3, 13, 153),
#    (1, 4, 142, 183),
#    (1, 4, 236, 80),
#    (2, 4, 40, 241),
#    (2, 1, 65, 92)
#] 
sumt, sumc, mst = kruskal(N, edges)
print(sumt, sumc)
for u, v in mst:
    print(u, v)

Compilation message (stderr)

timeismoney.cpp:40:2: error: invalid preprocessing directive #N
   40 | #N = 5
      |  ^
timeismoney.cpp:41:2: error: invalid preprocessing directive #M
   41 | #M = 7
      |  ^
timeismoney.cpp:42:2: error: invalid preprocessing directive #edges
   42 | #edges = [
      |  ^~~~~
timeismoney.cpp:43:6: error: invalid preprocessing directive #(
   43 | #    (0, 1, 161, 79),
      |      ^
timeismoney.cpp:44:6: error: invalid preprocessing directive #(
   44 | #    (0, 2, 161, 15),
      |      ^
timeismoney.cpp:45:5: error: invalid preprocessing directive #(
   45 | #   (0, 3, 13, 153),
      |     ^
timeismoney.cpp:46:6: error: invalid preprocessing directive #(
   46 | #    (1, 4, 142, 183),
      |      ^
timeismoney.cpp:47:6: error: invalid preprocessing directive #(
   47 | #    (1, 4, 236, 80),
      |      ^
timeismoney.cpp:48:6: error: invalid preprocessing directive #(
   48 | #    (2, 4, 40, 241),
      |      ^
timeismoney.cpp:49:6: error: invalid preprocessing directive #(
   49 | #    (2, 1, 65, 92)
      |      ^
timeismoney.cpp:50:2: error: invalid preprocessing directive #]
   50 | #]
      |  ^
timeismoney.cpp:2:9: error: expected class-name before '__init__'
    2 |     def __init__(self, n):
      |         ^~~~~~~~
timeismoney.cpp:2:9: error: expected '{' before '__init__'
timeismoney.cpp:2:18: error: 'self' was not declared in this scope
    2 |     def __init__(self, n):
      |                  ^~~~
timeismoney.cpp:2:24: error: 'n' was not declared in this scope
    2 |     def __init__(self, n):
      |                        ^
timeismoney.cpp:2:25: error: expression list treated as compound expression in initializer [-fpermissive]
    2 |     def __init__(self, n):
      |                         ^