108232024-04-15 23:55:4342Vasútépítéspython3Runtime error 0/100312ms93816 KiB
from sys import stdin,stdout
input=stdin.readline

def main():
    N,M = list(map(int, input().split()))
    if M>N:
        print(-1)
        return
    graph={}
    for i in range(M):
        u,v = list(map(int, input().split()))
        try: graph[u].add(v)
        except: graph[u]={v}
        try: graph[v].add(u)
        except: graph[v]={u}
    if len(graph)<N:
        print(-1)
        return
    deg={}
    for key in graph:
        try: deg[len(graph[key])].add(key)
        except: deg[len(graph[key])]={key}
    #print(deg)
    #print(graph)
    # deg2 -> 1...1000
    # deg1 -> 1001...2000
    deg1,deg2=2000,1000
    inf=10**8
    matrix=[list(range(inf-i*1001-N-1,inf-i*1001)) for i in range(0,N+1)]
    #print(matrix)
    while deg[1]:
        v=deg[1].pop()
        u=graph.pop(v).pop()
        #print(v,u)
        matrix[u][v]=deg1
        matrix[v][u]=deg1
        deg1-=1
        if u in deg[1]:
            deg[1].remove(u)
            graph.pop(u)
        else:
            graph[u].remove(v)
            deg[len(graph[u])].add(u)
            if len(deg[len(graph[u])+1])==1:
                deg.pop(len(graph[u])+1)
            else:
                deg[len(graph[u])+1].remove(u)
            if len(graph[u])==1:
                deg[1].add(u)
    if 1 in deg:
        deg.pop(1)
    if len(deg)==0:
        for i in range(1,N):
            #print(*matrix[i][i+1:])
            for j in range(i+1,N+1):
                stdout.write(str(matrix[i][j])+' ')
            stdout.write('\n')
        return
    elif len(deg)>1 or 2 not in deg:
        print(-1)
        return
    #print(deg)
    #print(graph)
    while deg[2]:
        v=deg[2].pop()
        cur=[v]
        while cur[-1] in graph:
            szomsz=graph.pop(cur[-1])
            for u in szomsz:
                if u in deg[2]:
                    deg[2].remove(u)
                    cur.append(u)
                    break
        #print(cur)
        for i in range(len(cur)):
            u=cur[i]
            v=cur[i-1]
            matrix[u][v]=deg2
            matrix[v][u]=deg2
            deg2-=1
                
    #print(N,len(matrix),len(matrix[0]))
    for i in range(1,N):
        #print(*matrix[i][i+1:])
        for j in range(i+1,N+1):
            stdout.write(str(matrix[i][j])+' ')
        stdout.write('\n')
main()
SubtaskSumTestVerdictTimeMemory
subtask10/0
1Accepted19ms11776 KiB
2Accepted18ms12188 KiB
3Accepted21ms13492 KiB
subtask20/40
4Runtime error85ms92268 KiB
5Accepted296ms92132 KiB
6Accepted298ms92372 KiB
7Accepted305ms92772 KiB
8Runtime error83ms92588 KiB
9Runtime error83ms92512 KiB
10Runtime error83ms92760 KiB
11Accepted18ms13220 KiB
subtask30/60
12Runtime error85ms92268 KiB
13Accepted296ms92132 KiB
14Accepted298ms92372 KiB
15Accepted305ms92772 KiB
16Runtime error83ms92588 KiB
17Runtime error83ms92512 KiB
18Runtime error83ms92760 KiB
19Accepted18ms13220 KiB
20Accepted312ms92996 KiB
21Runtime error83ms93336 KiB
22Accepted296ms92752 KiB
23Runtime error83ms93208 KiB
24Accepted298ms93320 KiB
25Accepted310ms93692 KiB
26Accepted300ms93816 KiB
27Runtime error100ms93692 KiB
28Accepted18ms13980 KiB
29Accepted19ms14660 KiB
30Accepted17ms13908 KiB
31Accepted75ms93588 KiB