IDENTIFICATION DIVISION.
PROGRAM-ID. A-BEGINNER.
 
DATA DIVISION.
WORKING-STORAGE SECTION.
01 INP      PIC X(100).
01 IN-DATA.
   03 DT-X1  PIC X(10).
   03 DT-X2  PIC X(10).
01 IN-LEN.
   03 DT-L1  PIC 9(2).
   03 DT-L2  PIC 9(2).
01 IN-N      PIC 9(6).
01 IN-M      PIC 9(6).
01 IN-K      PIC 9(6).
01 IN-X      PIC 9(6).
01 IN-Y      PIC 9(6).
01 TBL-FRIEND.
   03 T-FRI OCCURS 100000 DEPENDING ON IN-M.
     05  T-F1 PIC 9(6).
     05  T-F2 PIC 9(6).
01 TBL-BLOCK.
   03 T-BLK OCCURS 100000 DEPENDING ON IN-K.
     05  T-B1 PIC 9(6).
     05  T-B2 PIC 9(6).
01 TBL-KOHO.
   03 T-KH   PIC 9(5)  OCCURS 100000.
01 IDX-IJ.
   03 I      PIC 9(6).
   03 J      PIC 9(6).
01 IDX-KJ.
   03 K      PIC 9(6).
   03 J2     PIC 9(6).
01 B-I       PIC 9(6).
01 P-F       PIC 9(6).
01 P-F1      PIC 9(6).
01 P-F2      PIC 9(6).
01 P-F-LAST  PIC 9(6).
01 P-B       PIC 9(6).
01 L-FRI     PIC 9(2).
01 WK-FRI    PIC X(6).
01 LEN       PIC 9(6).
01 FLAG-AREA.
   03 FL-NXT PIC X(1).
   03 FL-FRI PIC X(1).
   03 FL-BLK PIC X(1).
01 EDIT-AREA.
   03 ED-FRI PIC Z(5)9.
01 OUT-K     PIC X(600001).
 
PROCEDURE DIVISION.
MAIN      SECTION.
  ACCEPT INP.
  UNSTRING INP DELIMITED BY ALL ' '
      INTO IN-N IN-M IN-K.
 
  PERFORM VARYING I FROM 1 BY 1 UNTIL I > IN-N
    MOVE 0 TO T-KH(I)
  END-PERFORM.

  PERFORM VARYING I FROM 1 BY 1 UNTIL I > IN-M
    ACCEPT INP
    UNSTRING INP DELIMITED BY ALL ' '
        INTO IN-X IN-Y 
    IF (IN-X < IN-Y)
      MOVE IN-X TO T-F1(I)
      MOVE IN-Y TO T-F2(I)
    ELSE
      MOVE IN-Y TO T-F1(I)
      MOVE IN-X TO T-F2(I)
    END-IF
  END-PERFORM.
  
  PERFORM VARYING I FROM 1 BY 1 UNTIL I > IN-K
    ACCEPT INP
    UNSTRING INP DELIMITED BY ALL ' '
        INTO IN-X IN-Y
    IF (IN-X < IN-Y)
      MOVE IN-X TO T-B1(I)
      MOVE IN-Y TO T-B2(I)
    ELSE
      MOVE IN-Y TO T-B1(I)
      MOVE IN-X TO T-B2(I)
    END-IF
  END-PERFORM.

*> ------------------------------------- テーブルソート -*
  SORT T-FRI ASCENDING KEY T-F1 T-F2.
  SORT T-BLK ASCENDING KEY T-B1 T-B2.

      MOVE 1 TO P-F1 P-F2 P-B.
  MOVE 0 TO P-F-LAST. 
  PERFORM VARYING I FROM 1 BY 1 UNTIL I >= IN-N
    MOVE '0' TO FL-NXT
    COMPUTE K = I + 1
    PERFORM VARYING J FROM K BY 1 
              UNTIL (J > IN-N) OR (FL-NXT = '1')
      IF (J = I + 1)
        MOVE '1' TO FL-FRI
      ELSE
        IF (J <= P-F-LAST)       *> 友達チェック済み？
          MOVE '1' TO FL-FRI
        ELSE
*> J-1とJの友達チェック              
          COMPUTE K = J - 1
          MOVE J    TO J2
          MOVE P-F2 TO P-F
          PERFORM FRIEND-CHECK
          MOVE P-F TO P-F2
          IF (FL-FRI = '1') 
            MOVE J  TO P-F-LAST         
          END-IF
        END-IF
      END-IF
*> 
      IF (FL-FRI = '1')
*> IとJの友達チェック              
        MOVE I TO K
        MOVE J TO J2
        MOVE P-F1  TO P-F
        PERFORM FRIEND-CHECK
        MOVE P-F   TO P-F1
        IF (FL-FRI = '0') 
*> IとJのブロックチェック  
          MOVE IDX-IJ TO IDX-KJ            
          PERFORM BLOCK-CHECK
          IF (FL-BLK = '0')
            ADD 1 TO T-KH(I)
            ADD 1 TO T-KH(J)
          END-IF
          MOVE '1' TO FL-NXT
        END-IF
      ELSE
        MOVE '1' TO FL-NXT
      END-IF       
    END-PERFORM
  END-PERFORM.

*> ------------------------------------------- 結果出力 -*              
  MOVE 0 TO LEN.
  PERFORM VARYING I FROM 1 BY 1 UNTIL I > IN-N
    MOVE T-KH(I) TO ED-FRI
    PERFORM VARYING J FROM 5 BY -1 
      UNTIL (J < 1) OR (EDIT-AREA(J:1) = ' ') 
    END-PERFORM
    MOVE EDIT-AREA(J + 1:6 - J) TO OUT-K(LEN + 1:6 - J)
    MOVE ' '                    TO OUT-K(LEN + 7 - J :1)
    COMPUTE LEN = LEN + 7 - J
  END-PERFORM.
  DISPLAY OUT-K(1:LEN - 1).

MAIN-EXIT.
  STOP RUN.

*> --------------------------------------- 友達チェック -*              
FRIEND-CHECK SECTION.
FRIEND-CHECK-001.
  PERFORM UNTIL (P-F > IN-M) 
             OR (IDX-KJ <= T-FRI(P-F))
    ADD 1 TO P-F 
  END-PERFORM.
  IF (IDX-KJ = T-FRI(P-F))
    MOVE '1' TO FL-FRI
  ELSE
    MOVE '0' TO FL-FRI
  END-IF. 
FRIEND-CHECK-EXIT.
  EXIT.

*> ----------------------------------- ブロックチェック -*              
BLOCK-CHECK  SECTION.
BLOCK-CHECK-001.
  PERFORM UNTIL (P-B > IN-K) 
             OR (IDX-IJ <= T-BLK(P-B))
    ADD 1 TO P-B 
  END-PERFORM.
  IF (IDX-IJ = T-BLK(P-B))
    MOVE '1' TO FL-BLK
  ELSE
    MOVE '0' TO FL-BLK
  END-IF. 
BLOCK-CHECK-EXIT.
  EXIT.
