ENG  RUSTimus Online Judge
Online Judge
Problems
Authors
Online contests
About Online Judge
Frequently asked questions
Site news
Webboard
Links
Problem set
Submit solution
Judge status
Guide
Register
Update your info
Authors ranklist
Current contest
Scheduled contests
Past contests
Rules
back to board

Discussion of Problem 1709. Penguin-Avia

WA #1 Help me pls.
Posted by Viktor Krivoshchekov`~ 1 Dec 2018 23:46
n = int(input())
d, a = map(int, input().split())


def dfs(s, g, used):
    used[s] = True
    for i in range(s + 1, n + 1):
        if g[s][i] and not (used[i]):
            dfs(i, g, used)


g = [['0'] * (n + 1) for i in range(n + 1)]
for i in range(n):
    s = input()
    for j in range(n):
        if s[j] == '1':
            g[i + 1][j + 1] = 1
k = []
i = 1
while i < n + 1:
    l1 = []
    used = [False] * (n + 1)
    dfs(i, g, used)
    for j in range(i, n + 1):
        if used[j]:
            l1.append(j)
    k.append(l1)
    for j in range(i + 1, n + 1):
        if not (used[j]):
            i = j
            break
    else:
        break
k1 = []
for i in k:
    p = []
    for j in i:
        for z in range(j + 1, i[-1] + 1):
            if g[j][z] == 1:
                p.append([j, z])
    k1.append(p)
k1.sort()
ans = (len(k1) - 1) * a
g1 = [[0] * (n + 1) for i in range(n + 1)]
for i in k1:
    p = []
    for j in i:
        if j[1] not in p:
            p.append(j[1])
        else:
            g1[j[0]][j[1]] = 'd'
            g1[j[1]][j[0]] = 'd'
    ans += (len(i) - len(p)) * d
print(ans)
for i in range(len(k1) - 1):
    g1[k1[i][-1][1]][k1[i+1][0][0]] = 'a'
    g1[k1[i+1][0][0]][k1[i][-1][1]] = 'a'
for i in g1:
    for j in i:
        print(j, end='')
    print()