This documentation is automatically generated by online-judge-tools/verification-helper
# verification-helper: PROBLEM https://onlinejudge.u-aizu.ac.jp/courses/lesson/2/ITP1/1/ITP1_1_A
import pytest
class TestCSR3:
def test_basic_operations(self):
"""Test basic CSR3 operations"""
# Create adjacency list: 0->(1,2), 1->(2), 2->()
K = [0, 0, 1] # Source nodes
V1 = [1, 2, 2] # Destinations
V2 = [10, 20, 30] # Weights
V3 = [100, 200, 300] # Additional data
csr = CSR3.bucketize(3, K, V1, V2, V3)
assert len(csr) == 3
# Check node 0 neighbors
neighbors0 = list(csr[0])
assert len(neighbors0) == 2
assert neighbors0[0] == (1, 10, 100)
assert neighbors0[1] == (2, 20, 200)
# Check node 1 neighbors
neighbors1 = list(csr[1])
assert len(neighbors1) == 1
assert neighbors1[0] == (2, 30, 300)
# Check node 2 neighbors (empty)
neighbors2 = list(csr[2])
assert len(neighbors2) == 0
def test_direct_access(self):
"""Test direct access using __call__"""
K = [0, 0, 1, 2]
V1 = [1, 3, 2, 0]
V2 = [10, 30, 20, 40]
V3 = [100, 300, 200, 400]
csr = CSR3.bucketize(4, K, V1, V2, V3)
# Direct access to edges
assert csr(0, 0) == (1, 10, 100)
assert csr(0, 1) == (3, 30, 300)
assert csr(1, 0) == (2, 20, 200)
assert csr(2, 0) == (0, 40, 400)
def test_set_operation(self):
"""Test setting edge values"""
A1 = [1, 2, 3]
A2 = [10, 20, 30]
A3 = [100, 200, 300]
O = [0, 2, 3, 3] # Node 0: [0:2], Node 1: [2:3], Node 2: [3:3] (empty)
csr = CSR3(A1, A2, A3, O)
# Modify edge
csr.set(0, 1, (99, 990, 9900))
assert csr(0, 1) == (99, 990, 9900)
# Check underlying arrays
assert A1[1] == 99
assert A2[1] == 990
assert A3[1] == 9900
def test_view_integration(self):
"""Test that CSR returns proper view objects"""
K = [0, 1]
V1 = [1, 2]
V2 = [10, 20]
V3 = [100, 200]
csr = CSR3.bucketize(3, K, V1, V2, V3)
# Get view for node 0
view0 = csr[0]
assert len(view0) == 1
assert view0[0] == (1, 10, 100)
# Modify through view
view0[0] = (5, 50, 500)
assert csr(0, 0) == (5, 50, 500)
def test_empty_graph(self):
"""Test CSR with no edges"""
csr = CSR3.bucketize(3, [], [], [], [])
assert len(csr) == 3
for i in range(3):
assert len(list(csr[i])) == 0
def test_self_loops(self):
"""Test CSR with self-loops"""
K = [0, 1, 2]
V1 = [0, 1, 2] # Self-loops
V2 = [10, 20, 30]
V3 = [100, 200, 300]
csr = CSR3.bucketize(3, K, V1, V2, V3)
assert csr(0, 0) == (0, 10, 100)
assert csr(1, 0) == (1, 20, 200)
assert csr(2, 0) == (2, 30, 300)
def test_multiple_edges(self):
"""Test node with multiple edges to same destination"""
K = [0, 0, 0] # All from node 0
V1 = [1, 1, 1] # All to node 1
V2 = [10, 20, 30]
V3 = [100, 200, 300]
csr = CSR3.bucketize(2, K, V1, V2, V3)
neighbors = list(csr[0])
assert len(neighbors) == 3
assert all(n[0] == 1 for n in neighbors)
from cp_library.ds.view.csr3_cls import CSR3
if __name__ == '__main__':
from cp_library.test.unittest_helper import run_verification_helper_unittest
run_verification_helper_unittest()
# verification-helper: PROBLEM https://onlinejudge.u-aizu.ac.jp/courses/lesson/2/ITP1/1/ITP1_1_A
import pytest
class TestCSR3:
def test_basic_operations(self):
"""Test basic CSR3 operations"""
# Create adjacency list: 0->(1,2), 1->(2), 2->()
K = [0, 0, 1] # Source nodes
V1 = [1, 2, 2] # Destinations
V2 = [10, 20, 30] # Weights
V3 = [100, 200, 300] # Additional data
csr = CSR3.bucketize(3, K, V1, V2, V3)
assert len(csr) == 3
# Check node 0 neighbors
neighbors0 = list(csr[0])
assert len(neighbors0) == 2
assert neighbors0[0] == (1, 10, 100)
assert neighbors0[1] == (2, 20, 200)
# Check node 1 neighbors
neighbors1 = list(csr[1])
assert len(neighbors1) == 1
assert neighbors1[0] == (2, 30, 300)
# Check node 2 neighbors (empty)
neighbors2 = list(csr[2])
assert len(neighbors2) == 0
def test_direct_access(self):
"""Test direct access using __call__"""
K = [0, 0, 1, 2]
V1 = [1, 3, 2, 0]
V2 = [10, 30, 20, 40]
V3 = [100, 300, 200, 400]
csr = CSR3.bucketize(4, K, V1, V2, V3)
# Direct access to edges
assert csr(0, 0) == (1, 10, 100)
assert csr(0, 1) == (3, 30, 300)
assert csr(1, 0) == (2, 20, 200)
assert csr(2, 0) == (0, 40, 400)
def test_set_operation(self):
"""Test setting edge values"""
A1 = [1, 2, 3]
A2 = [10, 20, 30]
A3 = [100, 200, 300]
O = [0, 2, 3, 3] # Node 0: [0:2], Node 1: [2:3], Node 2: [3:3] (empty)
csr = CSR3(A1, A2, A3, O)
# Modify edge
csr.set(0, 1, (99, 990, 9900))
assert csr(0, 1) == (99, 990, 9900)
# Check underlying arrays
assert A1[1] == 99
assert A2[1] == 990
assert A3[1] == 9900
def test_view_integration(self):
"""Test that CSR returns proper view objects"""
K = [0, 1]
V1 = [1, 2]
V2 = [10, 20]
V3 = [100, 200]
csr = CSR3.bucketize(3, K, V1, V2, V3)
# Get view for node 0
view0 = csr[0]
assert len(view0) == 1
assert view0[0] == (1, 10, 100)
# Modify through view
view0[0] = (5, 50, 500)
assert csr(0, 0) == (5, 50, 500)
def test_empty_graph(self):
"""Test CSR with no edges"""
csr = CSR3.bucketize(3, [], [], [], [])
assert len(csr) == 3
for i in range(3):
assert len(list(csr[i])) == 0
def test_self_loops(self):
"""Test CSR with self-loops"""
K = [0, 1, 2]
V1 = [0, 1, 2] # Self-loops
V2 = [10, 20, 30]
V3 = [100, 200, 300]
csr = CSR3.bucketize(3, K, V1, V2, V3)
assert csr(0, 0) == (0, 10, 100)
assert csr(1, 0) == (1, 20, 200)
assert csr(2, 0) == (2, 30, 300)
def test_multiple_edges(self):
"""Test node with multiple edges to same destination"""
K = [0, 0, 0] # All from node 0
V1 = [1, 1, 1] # All to node 1
V2 = [10, 20, 30]
V3 = [100, 200, 300]
csr = CSR3.bucketize(2, K, V1, V2, V3)
neighbors = list(csr[0])
assert len(neighbors) == 3
assert all(n[0] == 1 for n in neighbors)
'''
╺━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━╸
https://kobejean.github.io/cp-library
'''
from typing import Generic
from typing import TypeVar
_S = TypeVar('S'); _T = TypeVar('T'); _U = TypeVar('U'); _T1 = TypeVar('T1'); _T2 = TypeVar('T2'); _T3 = TypeVar('T3'); _T4 = TypeVar('T4'); _T5 = TypeVar('T5'); _T6 = TypeVar('T6')
def argsort_ranged(A: list[int], l: int, r: int, reverse=False):
P = Packer(r-l-1); I = [A[l+i] for i in range(r-l)]; P.ienumerate(I, reverse); I.sort()
for i in range(r-l): I[i] = (I[i] & P.m) + l
return I
class Packer:
__slots__ = 's', 'm'
def __init__(P, mx: int): P.s = mx.bit_length(); P.m = (1 << P.s) - 1
def enc(P, a: int, b: int): return a << P.s | b
def dec(P, x: int) -> tuple[int, int]: return x >> P.s, x & P.m
def enumerate(P, A, reverse=False): P.ienumerate(A:=list(A), reverse); return A
def ienumerate(P, A, reverse=False):
if reverse:
for i,a in enumerate(A): A[i] = P.enc(-a, i)
else:
for i,a in enumerate(A): A[i] = P.enc(a, i)
def indices(P, A: list[int]): P.iindices(A:=list(A)); return A
def iindices(P, A):
for i,a in enumerate(A): A[i] = P.m&a
def isort_ranged(*L: list, l: int, r: int, reverse=False):
n = r - l
order = argsort_ranged(L[0], l, r, reverse=reverse)
inv = [0] * n
# order contains indices in range [l, r), need to map to [0, n)
for i in range(n): inv[order[i]-l] = i
for i in range(n):
j = order[i] - l # j is in range [0, n)
for A in L: A[l+i], A[l+j] = A[l+j], A[l+i]
order[inv[i]], order[inv[j]] = order[inv[j]], order[inv[i]]
inv[i], inv[j] = inv[j], inv[i]
return L
class view3(Generic[_T1, _T2, _T3]):
__slots__ = 'A1', 'A2', 'A3', 'l', 'r'
def __init__(V, A1: list[_T1], A2: list[_T2], A3: list[_T3], l: int = 0, r: int = 0):
V.A1, V.A2, V.A3, V.l, V.r = A1, A2, A3, l, r
def __len__(V): return V.r - V.l
def __getitem__(V, i: int):
if 0 <= i < V.r - V.l: return V.A1[V.l+i], V.A2[V.l+i], V.A3[V.l+i]
else: raise IndexError
def __setitem__(V, i: int, v: tuple[_T1, _T2, _T3]): V.A1[V.l+i], V.A2[V.l+i], V.A3[V.l+i] = v
def __contains__(V, v: tuple[_T1, _T2, _T3]): raise NotImplemented
def set_range(V, l: int, r: int): V.l, V.r = l, r
def index(V, v: tuple[_T1, _T2, _T3]): raise NotImplemented
def reverse(V):
l, r = V.l, V.r-1
while l < r:
V.A1[l], V.A1[r] = V.A1[r], V.A1[l]
V.A2[l], V.A2[r] = V.A2[r], V.A2[l]
V.A3[l], V.A3[r] = V.A3[r], V.A3[l]
l += 1; r -= 1
def sort(V, reverse=False): isort_ranged(V.A1, V.A2, V.A3, l=V.l, r=V.r, reverse=reverse)
def pop(V): V.r -= 1; return V.A1[V.r], V.A2[V.r], V.A3[V.r]
def append(V, v: tuple[_T1, _T2, _T3]): V.A1[V.r], V.A2[V.r], V.A3[V.r] = v; V.r += 1
def popleft(V): V.l += 1; return V.A1[V.l-1], V.A2[V.l-1], V.A3[V.l-1]
def appendleft(V, v: tuple[_T1, _T2, _T3]): V.l -= 1; V.A1[V.l], V.A2[V.l], V.A3[V.l] = v
def validate(V): return 0 <= V.l <= V.r <= len(V.A1)
class CSR3(Generic[_T1, _T2, _T3]):
__slots__ = 'A1', 'A2', 'A3', 'O'
def __init__(csr, A1: list[_T1], A2: list[_T2], A3: list[_T3], O: list[int]):
csr.A1, csr.A2, csr.A3, csr.O = A1, A2, A3, O
def __len__(csr): return len(csr.O)-1
def __getitem__(csr, i: int): return view3(csr.A1, csr.A2, csr.A3, csr.O[i], csr.O[i+1])
def __call__(csr, i: int, j: int): ij = csr.O[i]+j; return csr.A1[ij], csr.A2[ij], csr.A3[ij]
def set(csr, i: int, j: int, v: tuple[_T1, _T2, _T3]): ij = csr.O[i]+j; csr.A1[ij], csr.A2[ij], csr.A3[ij] = v
@classmethod
def bucketize(cls, N: int, K: list[int], V1: list[_T1], V2: list[_T2], V3: list[_T3]):
A1: list[_T1] = [0]*len(K); A2: list[_T2] = [0]*len(K); A3: list[_T3] = [0]*len(K); O = [0]*(N+1)
for k in K: O[k] += 1
for i in range(N): O[i+1] += O[i]
for e in range(len(K)): k = K[~e]; O[k] -= 1; A1[O[k]] = V1[~e]; A2[O[k]] = V2[~e]; A3[O[k]] = V3[~e]
return cls(A1, A2, A3, O)
if __name__ == '__main__':
"""
Helper for making unittest files compatible with verification-helper.
This module provides a helper function to run a dummy Library Checker test
so that unittest files can be verified by oj-verify.
"""
def run_verification_helper_unittest():
"""
Run a dummy AOJ ITP1_1_A test for verification-helper compatibility.
This function should be called in the __main__ block of unittest files
that need to be compatible with verification-helper.
The function:
1. Prints "Hello World" (AOJ ITP1_1_A solution)
2. Runs pytest for the calling test file
3. Exits with the pytest result code
"""
import sys
# Print "Hello World" for AOJ ITP1_1_A problem
print("Hello World")
import io
from contextlib import redirect_stdout, redirect_stderr
# Capture all output during test execution
output = io.StringIO()
with redirect_stdout(output), redirect_stderr(output):
# Get the calling module's file path
frame = sys._getframe(1)
test_file = frame.f_globals.get('__file__')
if test_file is None:
test_file = sys.argv[0]
result = pytest.main([test_file])
if result != 0:
print(output.getvalue())
sys.exit(result)
run_verification_helper_unittest()