659F - Polycarp and Hay - CodeForces Solution


dfs and similar dsu graphs greedy sortings *2000

Please click on ads to support us..

Python Code:

import os
import sys
from io import BytesIO, IOBase

BUFSIZE = 8192


class FastIO(IOBase):
    newlines = 0

    def __init__(self, file):
        self._fd = file.fileno()
        self.buffer = BytesIO()
        self.writable = "x" in file.mode or "r" not in file.mode
        self.write = self.buffer.write if self.writable else None

    def read(self):
        while True:
            b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
            if not b:
                break
            ptr = self.buffer.tell()
            self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr)
        self.newlines = 0
        return self.buffer.read()

    def readline(self):
        while self.newlines == 0:
            b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
            self.newlines = b.count(b"\n") + (not b)
            ptr = self.buffer.tell()
            self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr)
        self.newlines -= 1
        return self.buffer.readline()

    def flush(self):
        if self.writable:
            os.write(self._fd, self.buffer.getvalue())
            self.buffer.truncate(0), self.buffer.seek(0)


class IOWrapper(IOBase):
    def __init__(self, file):
        self.buffer = FastIO(file)
        self.flush = self.buffer.flush
        self.writable = self.buffer.writable
        self.write = lambda s: self.buffer.write(s.encode("ascii"))
        self.read = lambda: self.buffer.read().decode("ascii")
        self.readline = lambda: self.buffer.readline().decode("ascii")


sys.stdin, sys.stdout = IOWrapper(sys.stdin), IOWrapper(sys.stdout)
input = lambda: sys.stdin.readline().rstrip("\r\n")

from collections import defaultdict,deque
n,m,kq=map(int,input().split())
l=[]
for i in range(n):
    l.append(list(map(int,input().split())))
f=0
p=[i for i in range(n*m)]
size=[1 for i in range(n*m)]
def get(a):
    if p[a]!=a:
        p[a]=get(p[a])
    return p[a]
def union(a,b):
    a=get(a)
    b=get(b)
    if a==b:
        return
    if a>b:
        a,b=b,a
    p[a]=b
    size[b]+=size[a]
ind=defaultdict(list)
for i in range(n):
    for j in range(m):
        ind[l[i][j]].append(i*m+j)
f1=-1
for i in sorted(ind,reverse=True):
    maw=(0,0)
    for j in ind[i]:
        for k in [j-m,j+m,j-1,j+1]:
            if 0<=k<n*m and l[k//m][k%m]>=i:
                if m>1 and (k==j-1 or k==j+1):
                    if j//m==k//m:
                        union(j,k)
                else:
                    union(j,k)
        maw=max(maw,(size[get(j)],j))
    if i*maw[0]>=kq and kq%i==0:
        f=i
        f1=maw
        break
    if f!=0:
        break
if f!=0:
    print("YES")
    ax,by=f1[1]//m,f1[1]%m
    new=[[0 for i in range(m)]for j in range(n)]
    visited=[[0 for i in range(m)]for j in range(n)]
    q=deque([(ax,by)])
    c=0
    while(len(q)>0):
        x,y=q.popleft()
        new[x][y]=f
        if visited[x][y]==0:
            visited[x][y]=1
        c+=1
        if c==kq//f:
            break
        for i,j in [(x+1,y),(x,y-1),(x,y+1),(x-1,y)]:
            if 0<=i<n and 0<=j<m and l[i][j]>=f and visited[i][j]==0:
                visited[i][j]=1
                q.append((i,j))
    for i in range(n):
        print(*new[i])
else:
    print("NO")


Comments

Submit
0 Comments
More Questions

1630A - And Matching
1630B - Range and Partition
1630C - Paint the Middle
1630D - Flipping Range
1328A - Divisibility Problem
339A - Helpful Maths
4A - Watermelon
476A - Dreamoon and Stairs
1409A - Yet Another Two Integers Problem
977A - Wrong Subtraction
263A - Beautiful Matrix
180C - Letter
151A - Soft Drinking
1352A - Sum of Round Numbers
281A - Word Capitalization
1646A - Square Counting
266A - Stones on the Table
61A - Ultra-Fast Mathematician
148A - Insomnia cure
1650A - Deletions of Two Adjacent Letters
1512A - Spy Detected
282A - Bit++
69A - Young Physicist
1651A - Playoff
734A - Anton and Danik
1300B - Assigning to Classes
1647A - Madoka and Math Dad
710A - King Moves
1131A - Sea Battle
118A - String Task