;; -*- coding: utf-8 -*- (eval-when (:compile-toplevel :load-toplevel :execute) (sb-int:defconstant-eqx OPT #+swank '(optimize (speed 3) (safety 0)) #-swank '(optimize (speed 3) (safety 0) (debug 0)) #'equal) #+swank (ql:quickload '(:cl-debug-print :fiveam) :silent t) #-swank (set-dispatch-macro-character #\# #\> (lambda (s c p) (declare (ignore c p)) (read s nil nil t)))) #+swank (cl-syntax:use-syntax cl-debug-print:debug-print-syntax) #-swank (disable-debugger) ; for CS Academy ;; BEGIN_INSERTED_CONTENTS (defmacro define-int-types (&rest bits) `(progn ,@(mapcar (lambda (b) `(deftype ,(intern (format nil "UINT~A" b)) () '(unsigned-byte ,b))) bits) ,@(mapcar (lambda (b) `(deftype ,(intern (format nil "INT~A" b)) () '(signed-byte ,b))) bits))) (define-int-types 2 4 7 8 15 16 31 32 62 63 64) (declaim (ftype (function * (values fixnum &optional)) read-fixnum)) (defun read-fixnum (&optional (in *standard-input*)) (declare #.OPT #-swank (sb-kernel:ansi-stream in)) (macrolet ((%read-byte () `(the (unsigned-byte 8) #+swank (char-code (read-char in nil #\Nul)) #-swank (sb-impl::ansi-stream-read-byte in nil #.(char-code #\Nul) nil)))) (let* ((minus nil) (result (loop (let ((byte (%read-byte))) (cond ((<= 48 byte 57) (return (- byte 48))) ((zerop byte) ; #\Nul (error "Read EOF or #\Nul.")) ((= byte #.(char-code #\-)) (setf minus t))))))) (declare ((integer 0 #.most-positive-fixnum) result)) (loop (let* ((byte (%read-byte))) (if (<= 48 byte 57) (setq result (+ (- byte 48) (* 10 (the (integer 0 #.(floor most-positive-fixnum 10)) result)))) (return (if minus (- result) result)))))))) ;;; ;;; Strongly connected components of directed graph ;;; (declaim (inline %make-revgraph)) (defun %make-revgraph (graph) (let* ((n (length graph)) (revgraph (make-array n :element-type 'list :initial-element nil))) (dotimes (i n) (dolist (dest (aref graph i)) (push i (aref revgraph dest)))) revgraph)) (defun make-scc (graph &optional revgraph) "GRAPH := vector of adjacency lists REVGRAPH := NIL | reversed graph of GRAPH" (declare #.OPT ((simple-array list (*)) graph) ((or null (simple-array list (*))) revgraph)) (let* ((revgraph (or revgraph (%make-revgraph graph))) (n (length graph)) (visited (make-array n :element-type 'bit :initial-element 0)) (posts (make-array n :element-type 'uint31)) (components (make-array n :element-type 'uint31)) (pointer 0) (ord 0) ; ordinal number for a strongly connected component ) (declare (uint31 pointer ord)) (labels ((dfs (v) (declare (uint31 v)) (setf (aref visited v) 1) (dolist (neighbor (aref graph v)) (declare (uint31 neighbor)) (when (zerop (aref visited neighbor)) (dfs neighbor))) (setf (aref posts pointer) v) (incf pointer)) (reversed-dfs (v ord) (declare (uint31 v ord)) (setf (aref visited v) 1 (aref components v) ord) (dolist (neighbor (aref revgraph v)) (declare (uint31 neighbor)) (when (zerop (aref visited neighbor)) (reversed-dfs neighbor ord))))) (dotimes (v n) (when (zerop (aref visited v)) (dfs v))) (fill visited 0) (loop for i from (- n 1) downto 0 for v = (aref posts i) when (zerop (aref visited v)) do (reversed-dfs v ord) (incf ord)) components))) (defmacro dbg (&rest forms) #+swank (if (= (length forms) 1) `(format *error-output* "~A => ~A~%" ',(car forms) ,(car forms)) `(format *error-output* "~A => ~A~%" ',forms `(,,@forms))) #-swank (declare (ignore forms))) ;;; ;;; Body ;;; (defun main () (declare #.OPT) (let* ((n (read)) (m (read)) (ls (make-array (* 2 n) :element-type 'uint16)) (rs (make-array (* 2 n) :element-type 'uint16)) (graph (make-array (* 4 n) :element-type 'list :initial-element nil)) (2n (* 2 n)) (4n (* 4 n))) (declare (uint16 n m)) (dotimes (i n) (let* ((l (read-fixnum)) (r (read-fixnum))) (declare (uint16 l r)) (setf (aref ls i) l (aref rs i) r (aref ls (+ i n)) (- m r 1) (aref rs (+ i n)) (- m l 1)))) (labels ((negate (x) (declare (uint16 x)) (let ((res (+ x 2n))) (if (>= res 4n) (- res 4n) res))) (add-clause! (literal1 literal2) (declare (uint16 literal1 literal2)) (push (negate literal2) (aref graph literal1)) (push (negate literal1) (aref graph literal2)))) (declare (inline negate add-clause!)) (gc :full t) (dotimes (x 2n) (loop for y from (+ x 1) below 2n do (when (and (/= (+ x n) y) (not (or (< (aref rs x) (aref ls y)) (< (aref rs y) (aref ls x))))) (add-clause! x y)))) (dotimes (x n) (add-clause! (negate x) (negate (+ x n))) (add-clause! x (+ x n))) (let* ((comps (make-scc graph))) (declare ((simple-array uint31 (*)) comps)) (write-line (if (loop for x below 2n thereis (= (aref comps x) (aref comps (+ x 2n)))) "NO" "YES")))))) #-swank (main)