cp-library

This documentation is automatically generated by online-judge-tools/verification-helper

View the Project on GitHub kobejean/cp-library

:heavy_check_mark: test/aoj/grl/grl_2_c_scc_fast.test.py

Depends on

Code

# verification-helper: PROBLEM https://onlinejudge.u-aizu.ac.jp/courses/library/5/GRL/3/GRL_3_C

def strongly_connected_components(N, M, La, Ra, Va):
    st, buf, sccs, tin, low, t, d, id = [0]*N, elist(N), [-1]*N, [-1]*N, [N]*N, -1, -1, -1
    for u in range(N):
        if tin[u] >= 0: continue
        st[d:=0] = u
        while d >= 0:
            if tin[u := st[d]] == -1: tin[u] = low[u] = (t:=t+1); buf.append(u)
            if (a := La[u]) < Ra[u]:
                La[u] += 1
                if (tv := tin[v := Va[a]]) == -1: st[d:=d+1] = v
                elif tv < low[u]: low[u] = tv
            else:
                if (d:=d-1) >= 0 and low[u] < low[st[d]]: low[st[d]] = low[u]
                if low[u] == tin[u]:
                    v, id = -1, id+1
                    while u != v: tin[v := buf.pop()] = N; sccs[v] = id
    return sccs

def main():
    N, M, La, Ra, Va = read_csr_graph()
    sccs = strongly_connected_components(N, M, La, Ra, Va)
    Q = rd()
    for _ in range(Q):
        u, v = rd(), rd()
        wtn(int(sccs[u]==sccs[v]))

from cp_library.ds.elist_fn import elist

def read_csr_graph():
    La, Ra, U, V, Va, t = [0]*(N:=rd()), [0]*N, [0]*(M:=rd()), [0]*M, [0]*M, 0
    for e in range(M): La[u := rd()] += 1; U[e], V[e] = u, rd()
    for u, deg in enumerate(La): La[u] = Ra[u] = (t := t + deg)
    for e, u in enumerate(U): La[u] -= 1; Va[La[u]] = V[e]
    return N, M, La, Ra, Va

from cp_library.io.fast.fast_io_fn import rd, wt, wtn, fastio

if __name__ == '__main__':
    main()
# verification-helper: PROBLEM https://onlinejudge.u-aizu.ac.jp/courses/library/5/GRL/3/GRL_3_C

def strongly_connected_components(N, M, La, Ra, Va):
    st, buf, sccs, tin, low, t, d, id = [0]*N, elist(N), [-1]*N, [-1]*N, [N]*N, -1, -1, -1
    for u in range(N):
        if tin[u] >= 0: continue
        st[d:=0] = u
        while d >= 0:
            if tin[u := st[d]] == -1: tin[u] = low[u] = (t:=t+1); buf.append(u)
            if (a := La[u]) < Ra[u]:
                La[u] += 1
                if (tv := tin[v := Va[a]]) == -1: st[d:=d+1] = v
                elif tv < low[u]: low[u] = tv
            else:
                if (d:=d-1) >= 0 and low[u] < low[st[d]]: low[st[d]] = low[u]
                if low[u] == tin[u]:
                    v, id = -1, id+1
                    while u != v: tin[v := buf.pop()] = N; sccs[v] = id
    return sccs

def main():
    N, M, La, Ra, Va = read_csr_graph()
    sccs = strongly_connected_components(N, M, La, Ra, Va)
    Q = rd()
    for _ in range(Q):
        u, v = rd(), rd()
        wtn(int(sccs[u]==sccs[v]))

'''
╺━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━╸
             https://kobejean.github.io/cp-library               
'''

def elist(est_len: int) -> list: ...
try:
    from __pypy__ import newlist_hint
except:
    def newlist_hint(hint):
        return []
elist = newlist_hint
    

def read_csr_graph():
    La, Ra, U, V, Va, t = [0]*(N:=rd()), [0]*N, [0]*(M:=rd()), [0]*M, [0]*M, 0
    for e in range(M): La[u := rd()] += 1; U[e], V[e] = u, rd()
    for u, deg in enumerate(La): La[u] = Ra[u] = (t := t + deg)
    for e, u in enumerate(U): La[u] -= 1; Va[La[u]] = V[e]
    return N, M, La, Ra, Va



from __pypy__.builders import StringBuilder
import sys
from os import read as os_read, write as os_write
from atexit import register as atexist_register

class Fastio:
    ibuf = bytes()
    pil = pir = 0
    sb = StringBuilder()
    def load(self):
        self.ibuf = self.ibuf[self.pil:]
        self.ibuf += os_read(0, 131072)
        self.pil = 0; self.pir = len(self.ibuf)
    def flush_atexit(self): os_write(1, self.sb.build().encode())
    def flush(self):
        os_write(1, self.sb.build().encode())
        self.sb = StringBuilder()
    def fastin(self):
        if self.pir - self.pil < 64: self.load()
        minus = x = 0
        while self.ibuf[self.pil] < 45: self.pil += 1
        if self.ibuf[self.pil] == 45: minus = 1; self.pil += 1
        while self.ibuf[self.pil] >= 48:
            x = x * 10 + (self.ibuf[self.pil] & 15)
            self.pil += 1
        if minus: return -x
        return x
    def fastin_string(self):
        if self.pir - self.pil < 64: self.load()
        while self.ibuf[self.pil] <= 32: self.pil += 1
        res = bytearray()
        while self.ibuf[self.pil] > 32:
            if self.pir - self.pil < 64: self.load()
            res.append(self.ibuf[self.pil])
            self.pil += 1
        return res
    def fastout(self, x): self.sb.append(str(x))
    def fastoutln(self, x): self.sb.append(str(x)); self.sb.append('\n')
fastio = Fastio()
rd = fastio.fastin; rds = fastio.fastin_string; wt = fastio.fastout; wtn = fastio.fastoutln; flush = fastio.flush
atexist_register(fastio.flush_atexit)
sys.stdin = None; sys.stdout = None
def rdl(n):
    lst = [0]*n
    for i in range(n): lst[i] = rd()
    return lst
def wtnl(l): wtn(' '.join(map(str, l)))

if __name__ == '__main__':
    main()
Back to top page