Buggy DFS

면접 대비

시간 제한1초메모리 제한2048 MB

요약
노드 수 32768 이하인 단순 무향 그래프를 만들어, 스택을 쓰는 버그 있는 DFS가 정확히 주어진 K를 반환하도록 한다.
난이도

보통10점 중 7점

유형
그래프, DFS, 그리디, 구현
정답자
아직 제출이 없습니다

문제

You are currently studying a graph traversal algorithm called the Depth First Search (DFS). However, due to a bug, your algorithm is slightly different from the standard DFS. The following is an algorithm for your Buggy DFS (BDFS), assuming the graph has NN nodes (numbered from 11 to NN).

BDFS():
  let S be an empty stack
  let FLAG be a boolean array of size N which are all false initially
  let counter be an integer initialized with 0

  push 1 to S

  while S is not empty:
    pop the top element of S into u
    FLAG[u] = true

    for each v neighbour of u in ascending order:
      counter = counter + 1
      if FLAG[v] is false:
        push v to S

  return counter

You realized that the bug made the algorithm slower than standard DFS, which can be investigated by the return value of the function BDFS(). To investigate the behavior of this algorithm, you want to make some test cases by constructing an undirected simple graph such that the function BDFS() returns KK, or determine if it is impossible to do so.

입력

A single line consisting of an integer KK (1≤K≤1091 ≤ K ≤ 10^9).

출력

If it is impossible to construct an undirected simple graph such that the function BDFS() returns KK, then output -1 -1 in a single line.

Otherwise, output the graph in the following format. The first line consists of two integers NN and MM, representing the number of nodes and undirected edges in the graph, respectively. Each of the next MM lines consists of two integers uu and vv, representing an undirected edge that connects node uu and node vv. You are allowed to output the edges in any order. This graph has to satisfy the following constraints:

  • 1≤N≤32,7681 ≤ N ≤ 32\\, 768
  • 1≤M≤65,5361 ≤ M ≤ 65\\, 536
  • 1≤u,v≤N1 ≤ u, v ≤ N, for all edges.
  • The graph is a simple graph, i.e. there are no multi-edges nor self-loops.

Note that you are not required to minimize the number of nodes or edges. It can be proven that if constructing a graph in which the return value of BDFS() is KK is possible, then there exists one that satisfies all the constraints above. If there are several solutions, you can output any of them.

예제3

  1. 예제 1

    입력
    8
    
    예상 출력
    3 3
    1 2
    1 3
    2 3
    
  2. 예제 2

    입력
    1
    
    예상 출력
    -1 -1
    
  3. 예제 3

    입력
    23
    
    예상 출력
    5 7
    4 5
    2 3
    3 1
    2 4
    4 3
    2 1
    1 5