22E - Scheme - CodeForces Solution


dfs and similar graphs trees *2300

Please click on ads to support us..

Python Code:


N=int(input())
A=list(map(int,input().split()))

E=[[] for i in range(N)]
E_INV=[[] for i in range(N)]

for i in range(N):
    x,y=i,A[i]-1
    E[x].append(y)
    E_INV[y].append(x)

def Top_sort(E):
    Parent=[-1]*N
    USEIND=[0]*N
    TOP=[]

    for ROOT in range(N):
        if Parent[ROOT]!=-1:
            continue
        Parent[ROOT]=ROOT

        NOW=ROOT

        while NOW!=ROOT or USEIND[ROOT]!=len(E[ROOT]):

            if USEIND[NOW]==len(E[NOW]):
                TOP.append(NOW)
                NOW=Parent[NOW]
            elif E[NOW][USEIND[NOW]]==Parent[NOW]:
                USEIND[NOW]+=1
            else:
                NEXT=E[NOW][USEIND[NOW]]
                USEIND[NOW]+=1
                if Parent[NEXT]==-1:
                    Parent[NEXT]=NOW
                    NOW=NEXT
        TOP.append(ROOT)
        
    return TOP[::-1]


USE=[0]*N
SCC=[]

def dfs2(x):
    Q=[x]
    USE[x]=1
    ANS=[]

    while Q:
        x=Q.pop()
        ANS.append(x)
        for to in E_INV[x]:
            if USE[to]==0:
                USE[to]=1
                Q.append(to)
    return ANS

TOP_SORT=Top_sort(E)

for x in TOP_SORT:
    if USE[x]==0:
        SCC.append(dfs2(x))


Group = [i for i in range(N+1)] Nodes = [1]*(N+1) 
def find(x):
    while Group[x] != x:
        x=Group[x]
    return x

def Union(x,y):
    if find(x) != find(y):
        if Nodes[find(x)] < Nodes[find(y)]:
            
            Nodes[find(y)] += Nodes[find(x)]
            Nodes[find(x)] = 0
            Group[find(x)] = find(y)
            
        else:
            Nodes[find(x)] += Nodes[find(y)]
            Nodes[find(y)] = 0
            Group[find(y)] = find(x)

for i in range(N):
    Union(i+1,A[i])

LAST=[]
USE=[0]*(N+1)
for scc in SCC[::-1]:
    for x in scc[::-1]:
        if USE[find(x+1)]==0:
            USE[find(x+1)]=1
            LAST.append(x+1)

FIRST=[]
USE=[0]*(N+1)
for scc in SCC:
    for x in scc:
        if USE[find(x+1)]==0:
            USE[find(x+1)]=1
            FIRST.append(x+1)

USE=[0]*(N+1)
for a in A:
    USE[a]=1

ANS=[]
ind=1

FIRST.sort(key=lambda x:find(x))
LAST.sort(key=lambda x:find(x))

if len(LAST)!=1:
    for i in FIRST:
        if ind==len(LAST):
            ind=0
        ANS.append((LAST[ind],i))
        Union(LAST[ind],i)
        USE[i]+=1
        USE[LAST[ind]]+=1

        ind+=1
if ind==len(LAST):
    ind=0

    
for i in range(1,N+1):
    if USE[i]==0:
        while Nodes[find(i)]<N and find(i)==find(LAST[ind]):
            ind+=1
            if ind==len(LAST):
                ind=0
                
        ANS.append((LAST[ind],i))
        Union(LAST[ind],i)




print(len(ANS))
for x,y in ANS:
    print(x,y)
    


Comments

Submit
0 Comments
More Questions

Divisibility
A. Movement
Numbers in a matrix
Sequences
Split houses
Divisible
Three primes
Coprimes
Cost of balloons
One String No Trouble
Help Jarvis!
Lift queries
Goki and his breakup
Ali and Helping innocent people
Book of Potion making
Duration
Birthday Party
e-maze-in
Bricks Game
Char Sum
Two Strings
Anagrams
Prime Number
Lexical Sorting Reloaded
1514A - Perfectly Imperfect Array
580A- Kefa and First Steps
1472B- Fair Division
996A - Hit the Lottery
MSNSADM1 Football
MATCHES Playing with Matches