結果

問題 No.274 The Wall
ユーザー sansaquasansaqua
提出日時 2019-09-17 22:13:41
言語 Common Lisp
(sbcl 2.3.8)
結果
MLE  
実行時間 -
コード長 9,826 bytes
コンパイル時間 774 ms
コンパイル使用メモリ 52,608 KB
実行使用メモリ 833,180 KB
最終ジャッジ日時 2023-09-21 20:19:14
合計ジャッジ時間 8,284 ms
ジャッジサーバーID
(参考情報)
judge15 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 9 ms
25,824 KB
testcase_01 AC 11 ms
26,536 KB
testcase_02 AC 9 ms
23,932 KB
testcase_03 MLE -
testcase_04 AC 8 ms
23,748 KB
testcase_05 AC 8 ms
27,804 KB
testcase_06 AC 7 ms
23,736 KB
testcase_07 AC 7 ms
23,776 KB
testcase_08 AC 7 ms
23,700 KB
testcase_09 AC 8 ms
25,900 KB
testcase_10 AC 8 ms
24,188 KB
testcase_11 MLE -
testcase_12 -- -
testcase_13 -- -
testcase_14 -- -
testcase_15 -- -
testcase_16 -- -
testcase_17 -- -
testcase_18 -- -
testcase_19 -- -
testcase_20 -- -
testcase_21 -- -
testcase_22 -- -
testcase_23 -- -
testcase_24 -- -
testcase_25 -- -
権限があれば一括ダウンロードができます
コンパイルメッセージ
; compiling file "/home/judge/data/code/Main.lisp" (written 21 SEP 2023 08:19:06 PM):
; processing (SB-INT:DEFCONSTANT-EQX OPT ...)
; processing (SET-DISPATCH-MACRO-CHARACTER #\# ...)
; processing (DISABLE-DEBUGGER)
; processing (DECLAIM (FTYPE # ...))
; processing (DEFUN READ-FIXNUM ...)
; processing (DEFSTRUCT (SCC #) ...)
; processing (DECLAIM (INLINE %MAKE-REVGRAPH))
; processing (DEFUN %MAKE-REVGRAPH ...)
; processing (DEFUN MAKE-SCC ...)
; file: /home/judge/data/code/Main.lisp
; in: DEFUN MAKE-SCC
;     (AREF REVGRAPH V)
; 
; note: unable to
;   optimize
; because:
;   Upgraded element type of array is not known at compile time.

;     (%MAKE-REVGRAPH GRAPH)
; --> BLOCK LET* DOTIMES DO BLOCK LET TAGBODY TAGBODY DOLIST BLOCK LET 
; --> SB-KERNEL:THE* AREF 
; ==>
;   (AREF GRAPH I)
; 
; note: unable to
;   optimize
; because:
;   Upgraded element type of array is not known at compile time.

;     (AREF GRAPH V)
; 
; note: unable to
;   optimize
; because:
;   Upgraded element type of array is not known at compile time.

; processing (DECLAIM (FTYPE # ...))
; processing (DEFUN MAKE-CONDENSED-GRAPH ...)
; file: /home/judge/data/code/Main.lisp
; in: DEFUN MAKE-CONDENSED-GRAPH
;     (AREF GRAPH I)
; 
; note: unable to
;   optimize
; because:
;   Upgraded element type of array is not known at compile time.

; processing (DEFMACRO DBG ...)
; processing (DEFMACRO DEFINE-INT-TYPES ...)
; processing (DEFINE-INT-TYPES 2 ...)
; processing (DECLAIM (INLINE PRINTLN))
; processing (DEFUN PRINTLN ...)
; processing (DEFCONSTANT +MOD+ ...)
; processing (DEFMACRO DOPAIR ...)
; processing (DEFUN MAIN ...)
; processing (MAIN); 
; compilation unit finished
;   printed 4 notes


; wrote /home/judge/data/code/Main.fasl
; compilation finished in 0:00:00.182

ソースコード

diff #

;; -*- coding: utf-8 -*-
(eval-when (:compile-toplevel :load-toplevel :execute)
  (sb-int:defconstant-eqx OPT
    #+swank '(optimize (speed 3) (safety 2))
    #-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
(declaim (ftype (function * (values fixnum &optional)) read-fixnum))
(defun read-fixnum (&optional (in *standard-input*))
  (declare #.OPT)
  (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
;;;

(defstruct (scc (:constructor %make-scc (graph revgraph posts components sizes count)))
  (graph nil :type vector)
  ;; reversed graph
  (revgraph nil :type vector)
  ;; vertices by post-order DFS
  posts
  ;; components[i] := strongly connected component of the i-th vertex
  (components nil :type (simple-array (integer 0 #.most-positive-fixnum) (*)))
  ;; sizes[k] := size of the k-th strongly connected component
  (sizes nil :type (simple-array (integer 0 #.most-positive-fixnum) (*)))
  ;; the total number of strongly connected components
  (count 0 :type (integer 0 #.most-positive-fixnum)))

(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 (optimize (speed 3))
           (vector graph)
           ((or null vector) 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 '(integer 0 #.most-positive-fixnum)))
         (components (make-array n :element-type '(integer 0 #.most-positive-fixnum)))
         (sizes (make-array n :element-type '(integer 0 #.most-positive-fixnum)
                            :initial-element 0))
         (pointer 0)
         (ord 0) ; ordinal number for a strongly connected component
         )
    (declare ((integer 0 #.most-positive-fixnum) pointer ord))
    (assert (= n (length revgraph)))
    (labels ((dfs (v)
               (setf (aref visited v) 1)
               (dolist (neighbor (aref graph v))
                 (when (zerop (aref visited neighbor))
                   (dfs neighbor)))
               (setf (aref posts pointer) v)
               (incf pointer))
             (reversed-dfs (v ord)
               (setf (aref visited v) 1
                     (aref components v) ord)
               (incf (aref sizes ord))
               (dolist (neighbor (aref revgraph v))
                 (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))
      (%make-scc graph revgraph posts components sizes ord))))

(declaim (ftype (function * (values (simple-array t (*)) &optional)) make-condensed-graph))
(defun make-condensed-graph (scc)
  "Does graph condensation.

This function is non-destructive. The resultant graph doesn't contain self-loops
even if the given graph does."
  (declare (optimize (speed 3)))
  (let* ((graph (scc-graph scc))
         (n (length graph))
         (comp-n (scc-count scc))
         (components (scc-components scc))
         (condensed (make-array comp-n :element-type t)))
    (dotimes (i comp-n)
      (setf (aref condensed i) (make-hash-table :test #'eql)))
    (dotimes (i n)
      (let ((i-comp (aref components i)))
        (dolist (neighbor (aref graph i))
          (let ((neighbor-comp (aref components neighbor)))
            (unless (= i-comp neighbor-comp)
              (setf (gethash neighbor-comp (aref condensed i-comp)) t))))))
    (dotimes (i comp-n)
      (setf (aref condensed i)
            (loop for x being each hash-key of (aref condensed i) collect x)))
    condensed))

(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)))

(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 (inline println))
(defun println (obj &optional (stream *standard-output*))
  (let ((*read-default-float-format* 'double-float))
    (prog1 (princ obj stream) (terpri stream))))

(defconstant +mod+ 1000000007)

;;;
;;; Body
;;;

(defmacro dopair ((var1 var2 list) &body body)
  (let ((suffix (gensym))
        (_list (gensym)))
    `(let ((,_list ,list))
       (loop for ,suffix on ,_list
             for ,var1 = (car ,suffix)
             do (dolist (,var2 (cdr ,suffix))
                  ,@body)))))

(defun main ()
  (let* ((n (read))
         (m (read))
         (sets (make-array m :element-type 'list :initial-element nil))
         (graph (make-array (* 4 n) :element-type 'list :initial-element nil)))
    (declare (uint16 n m))
    (dotimes (i n)
      (let* ((l (read-fixnum))
             (r (read-fixnum)))
        (loop for j from l to r
              do (push i (aref sets j)))
        (loop for j from (- m r 1) to (- m l 1)
              do (push (+ i n) (aref sets j)))))
    (labels ((negate (x)
               (declare (uint16 x))
               (mod (+ x (* 2 n)) (* 4 n)))
             (add-clause! (literal1 literal2 bool1 bool2)
               (unless bool1
                 (setq literal1 (negate literal1)))
               (unless bool2
                 (setq literal2 (negate literal2)))
               (push literal2 (aref graph (negate literal1)))
               (push literal1 (aref graph (negate literal2)))))
      (sb-int:dovector (set sets)
        (dopair (x y set)
          (add-clause! x y nil nil)))
      (dotimes (x n)
        (add-clause! x (+ x n) t t)
        (add-clause! x (+ x n) nil nil))
      (let* ((scc (make-scc graph))
             (comps (scc-components scc)))
        (write-line
         (if (loop for x below (* 2 n)
                   thereis (= (aref comps x)
                              (aref comps (+ x (* 2 n)))))
             "NO"
             "YES"))))))

#-swank (main)

;;;
;;; Test and benchmark
;;;

#+swank
(defun io-equal (in-string out-string &key (function #'main) (test #'equal))
  "Passes IN-STRING to *STANDARD-INPUT*, executes FUNCTION, and returns true if
the string output to *STANDARD-OUTPUT* is equal to OUT-STRING."
  (labels ((ensure-last-lf (s)
             (if (eql (uiop:last-char s) #\Linefeed)
                 s
                 (uiop:strcat s uiop:+lf+))))
    (funcall test
             (ensure-last-lf out-string)
             (with-output-to-string (out)
               (let ((*standard-output* out))
                 (with-input-from-string (*standard-input* (ensure-last-lf in-string))
                   (funcall function)))))))

#+swank
(defun get-clipbrd ()
  (with-output-to-string (out)
    (run-program "C:/msys64/usr/bin/cat.exe" '("/dev/clipboard") :output out)))

#+swank (defparameter *this-pathname* (uiop:current-lisp-file-pathname))
#+swank (defparameter *dat-pathname* (uiop:merge-pathnames* "test.dat" *this-pathname*))

#+swank
(defun run (&optional thing (out *standard-output*))
  "THING := null | string | symbol | pathname

null: run #'MAIN using the text on clipboard as input.
string: run #'MAIN using the string as input.
symbol: alias of FIVEAM:RUN!.
pathname: run #'MAIN using the text file as input."
  (let ((*standard-output* out))
    (etypecase thing
      (null
       (with-input-from-string (*standard-input* (delete #\Return (get-clipbrd)))
         (main)))
      (string
       (with-input-from-string (*standard-input* (delete #\Return thing))
         (main)))
      (symbol (5am:run! thing))
      (pathname
       (with-open-file (*standard-input* thing)
         (main))))))

#+swank
(defun gen-dat ()
  (uiop:with-output-file (out *dat-pathname* :if-exists :supersede)
    (format out "")))

#+swank
(defun bench (&optional (out (make-broadcast-stream)))
  (time (run *dat-pathname* out)))
0