Problem:

Queue

Queue is a container of elements that are inserted and deleted according to FIFO (First In First Out).

  For $n$ queues $Q_i$ ($i = 0, 1, ..., n-1$), perform a sequence of the following operations.

  
enqueue($t$, $x$): Insert an integer $x$ to $Q_t$.

  
front($t$): Report the value which should be deleted next from $Q_t$. If $Q_t$ is empty, do nothing.

  
dequeue($t$): Delete an element from $Q_t$. If $Q_t$ is empty, do nothing.

In the initial state, all queues are empty.

Input

  The input is given in the following format.

$n \; q$
$query_1$
$query_2$
:
$query_q$

Each query $query_i$ is given by

0 $t$ $x$

or

1 $t$

or

2 $t$

  where the first digits 0, 1 and 2 represent enqueue, front and dequeue operations respectively.

Output

  For each front operation, print an integer in a line.

Constraints

  
$1 \leq n \leq 1,000$

  
$1 \leq q \leq 200,000$

  
$-1,000,000,000 \leq x \leq 1,000,000,000$

Sample Input 1

3 9
0 0 1
0 0 2
0 0 3
0 2 4
0 2 5
1 0
1 2
2 0
1 0

Sample Output 1

1
4
2



Sample input:
3 9
0 0 1
0 0 2
0 0 3
0 2 4
0 2 5
1 0
1 2
2 0
1 0


Sample output:
1
4
2

Submission status: Accepted

Solution code:
class Node:
  def __init__(self,key):
    self.key = key
    self.prev = None
    self.next = None

class Queue:
  def __init__(self):
    self.head = Node(0)
    self.tail = self.head

  def enqueue(self,key):
    node = Node(key)
    self.tail.next = node
    node.prev = self.tail
    self.tail = node

  def front(self):
    if self.head != self.tail:
      return self.head.next.key

  def dequeue(self):
    if self.head != self.tail:
      if self.head.next == self.tail:
        self.tail = self.head
      self.head.next = self.head.next.next


n,q = map(int,input().split())

Q = [Queue() for i in range(n)]

for i in range(q):
  com, index, *n = map(int,input().split())
  if com == 0:
    Q[index].enqueue(*n)
  elif com == 1:
    key = Q[index].front()
    if key != None:
      print(key)
  elif com == 2:
    Q[index].dequeue()

Submission status: Accepted

Solution code:
from collections import deque

a = [int(i) for i in input().split()]
q = deque([])
for i in range(a[0]): q.append(deque([]))

for i in range(a[1]):
    cmd = [int(i) for i in input().split()]

    if cmd[0] == 0:
        q[cmd[1]].append(cmd[2])

    elif cmd[0] == 1:
        if q[cmd[1]] != deque([]): print(q[cmd[1]][0])

    elif cmd[0] == 2:
        if q[cmd[1]] != deque([]): q[cmd[1]].popleft()

Submission status: Accepted

Solution code:
from collections import deque

n, q = list(map(int, input().split(' ')))
queues = [deque([]) for i in range(n)]

for i in range(q):
    op = list(map(int, input().split(' ')))
    if op[0] == 0:
        queues[op[1]].append(op[2])
    elif op[0] == 1:
        if len(queues[op[1]]) != 0:
            print(queues[op[1]][0])
    elif op[0] == 2:
        if len(queues[op[1]]) != 0:
            queues[op[1]].popleft()

Submission status: Accepted

Solution code:

class Node:
    def __init__(self, value):
        self.value = value
        self.next = None


class Queue:
    def __init__(self):
        self.first = None
        self.last = None

    def enqueue(self, value):
        if value is None:
            raise ValueError('enqueue None')
        node = Node(value)
        if self.last is not None:
            self.last.next = node
        self.last = node
        if self.first is None:
            self.first = node

    def front(self):
        if self.first is None:
            return None
        return self.first.value

    def dequeue(self):
        if self.first is None:
            return None
        value = self.first.value
        if self.first == self.last:
            self.last = None
        self.first = self.first.next
        return value


def run():
    n, nq = [int(x) for x in input().split()]
    qs = [Queue() for _ in range(n)]

    for _ in range(nq):
        line = input()
        if line.startswith('0'):
            command, target, value = [int(x) for x in line.split()]
            qs[target].enqueue(value)
        elif line.startswith('1'):
            command, target = [int(x) for x in line.split()]
            value = qs[target].front()
            if value is not None:
                print(value)
        elif line.startswith('2'):
            command, target = [int(x) for x in line.split()]
            qs[target].dequeue()
        else:
            raise ValueError('invalid command')


if __name__ == '__main__':
    run()

Submission status: Accepted

Solution code:
# coding=utf-8

from collections import deque

N, Q = map(int, input().split())
Que = [deque([]) for i in range(N)]

for j in range(Q):
    query = list(map(int, input().split()))
    if query[0] == 0:
        Que[query[1]].append(query[2])
    elif query[0] == 1:
        if Que[query[1]]:
            print(Que[query[1]][0])
    elif query[0] == 2:
        if Que[query[1]]:
            Que[query[1]].popleft()

Submission status: Accepted

Solution code:
from collections import deque

if __name__ == '__main__':
    n, q = input().split()
    n, q = int(n), int(q)
    S = [deque([]) for i in range(n)]

    for i in range(q):
        query = input().split()

        if query[0] == '0':
            S[int(query[1])].append(query[2])

        else:
            if len(S[int(query[1])]) == 0:
                pass
            elif query[0] == '1':
                print(S[int(query[1])][0])
            else:
                S[int(query[1])].popleft()

Submission status: Accepted

Solution code:
# AOJ ITP2_2_B: Queue
# Python3 2018.6.24 bal4u

from collections import deque
n, q = map(int, input().split())
Q = []
for i in range(n): Q.append(deque())
for i in range(q):
	a = input().split()
	t = int(a[1])
	if   a[0] == '0': Q[t].append(a[2])        # push
	elif a[0] == '1' and Q[t]: print(Q[t][0])  # front
	elif a[0] == '2' and Q[t]: Q[t].popleft()  # dequeue

Submission status: Accepted

Solution code:
# -*- coding: utf-8 -*-
"""
Created on Sun Jun 24 14:53:50 2018
ITP22B
@author: maezawa
"""

from collections import deque

n, q = list(map(int, input().split()))
d = [deque() for _ in range(n)]


for i in range(q):
    c = list(map(int, input().split()))
    if c[0] == 0:
        d[c[1]].append(c[2])
    elif c[0] == 1:
        if d[c[1]]:
            print(d[c[1]][0])
    else:
        if d[c[1]]:
            d[c[1]].popleft()

Submission status: Accepted

Solution code:
from collections import deque
n, q = map(int, input().split())
A = [deque() for i in range(n)]
for i in range(q):
    tmp = input().split()
    tmp[1] = int(tmp[1])
    if tmp[0] == "0":
        A[tmp[1]].append(tmp[2])
    elif tmp[0] == "1" and len(A[tmp[1]]) != 0:
        print(A[tmp[1]][0])
    elif tmp[0] == "2" and len(A[tmp[1]]) != 0:
        A[tmp[1]].popleft()

Submission status: Accepted

Solution code:
from collections import deque
readline = open(0).readline
writelines = open(1, 'w').writelines

N, Q = map(int, readline().split())
ans = []
A = [deque() for i in range(N)]


def push(t, x):
    A[t].append(x)


def top(t):
    A[t] and ans.append("%d\n" % A[t][0])


def pop(t):
    A[t] and A[t].popleft()


C = [push, top, pop].__getitem__
for i in range(Q):
    t, *a = map(int, readline().split())
    C(t)(*a)
writelines(ans)

Submission status: Accepted

Solution code:
#!/usr/bin/env python
# -*- coding: utf-8 -*-
#
# FileName: 	queue
# CreatedDate:  2020-07-08 17:27:31 +0900
# LastModified: 2020-07-08 17:38:55 +0900
#


import os
import sys
# import numpy as np
# import pandas as pd
from collections import deque


def main():
    n, q = map(int, input().split())
    big_queue = []
    for _ in range(n):
        p = deque()
        big_queue.append(p)

    for _ in range(q):
        query = list(map(int, input().split()))
        if len(query) == 3:
            big_queue[query[1]].append(query[2])
        else:
            if query[0] == 1:
                if big_queue[query[1]]:
                    print(big_queue[query[1]][0])
            else:
                if big_queue[query[1]]:
                    big_queue[query[1]].popleft()
        


if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:
def resolve():
    from collections import deque
    n, Q = [int(i) for i in input().split()]
    ans = [deque() for _ in range(n)]
    for _ in range(Q):
        q = [int(i) for i in input().split()]
        if q[0] == 0:
            ans[q[1]].append(q[2])
        elif q[0] == 1:
            if len(ans[q[1]]) > 0:
                print(ans[q[1]][0])
        else:
            if len(ans[q[1]]) > 0:
                ans[q[1]].popleft()


resolve()

Submission status: Accepted

Solution code:
from collections import deque

def enqueue(t, x):
    global Qlist
    Qlist[t].appendleft(x)

def front(t):
    global Qlist
    if Qlist[t] != deque():
        print(Qlist[t][-1])

def dequeue(t):
    global Qlist
    if Qlist[t] != deque():
        Qlist[t].pop()


n, q = map(int, input().split())
Qlist =[]
for i in range(n):
    Q = deque()
    Qlist.append(Q)

for j in range(q):
    queryi = list(map(int, input().split()))
    
    if queryi[0] == 0:
        enqueue(queryi[1], queryi[2])

    elif queryi[0] == 1:
        front(queryi[1])

    else: 
        dequeue(queryi[1])

Submission status: Accepted

Solution code:
from collections import deque

class Queue:

  def __init__(self):
    self.Q = deque()

  #0:enqueue
  def enqueue(self,x):
    self.Q.appendleft(x)

  #1:front
  def front(self):
    if len(self.Q) > 0:
      print(self.Q[-1])
      
  #2:dequeue
  def dequeue(self):
    if len(self.Q) > 0:
      self.Q.pop()

n,q = map(int,input().split())
QList = list()

for i in range(n):
  QList.append(Queue())

for i in range(q):
  cmd = input()
  
  if cmd.startswith("0"):
    c,t,x = map(int,cmd.split(" "))
    QList[t].enqueue(x)
    
  if cmd.startswith("1"):
    c,t = map(int,cmd.split(" "))
    QList[t].front()
  
  if cmd.startswith("2"):
    c,t = map(int,cmd.split(" "))
    QList[t].dequeue()

Submission status: Accepted

Solution code:

import sys
from collections import deque
n,q = map(int, input().split())
S = []
for i in range(n):
    S.append(deque())

for i in range(q):
    l = list(map(int,input().split()))
    if l[0] == 0:
        S[l[1]].appendleft(l[2])
    elif l[0] == 1:
        if not S[l[1]]:
            continue
        else:
            print(S[l[1]][-1])
    elif S[l[1]]:
        S[l[1]].pop()

Submission status: Accepted

Solution code:
from collections import deque

n,q = map(int,input().split())

S = deque([deque() for _ in range(n)])
for i in range(q):
    query = tuple(map(int,input().split()))

    if query[0] == 0:
        S[query[1]].append(query[2])
    elif query[0] == 1:
        if len(S[query[1]]) != 0:
            print(S[query[1]][0])
    else:
        if len(S[query[1]]) != 0:
            S[query[1]].popleft()

Submission status: Accepted

Solution code:
from collections import deque
n, q = map(int, input().split())
queues = [deque() for i in range(n)]
while q:
    q -= 1
    s = [*map(int, input().split())]
    if s[0] == 0:
        queues[s[1]].append(s[2])
    elif s[0] == 1 and len(queues[s[1]]):
        print(queues[s[1]][0])
    elif len(queues[s[1]]):
        queues[s[1]].popleft()

Submission status: Accepted

Solution code:
from collections import deque
n, q = map(int, input().split())
Q =[]
for i in range(n):
    Q.append(deque())
for i in range(q):
    a = input().split()
    a[1] =int(a[1])
    if a[0] == "0":
        Q[a[1]].append(a[2])
    elif a[0] == "1" and Q[a[1]]:
        print(Q[a[1]][0])
    elif a[0] == "2" and Q[a[1]]:
        Q[a[1]].popleft()

Submission status: Accepted

Solution code:

from collections import deque

def main():
    n,m = map(int,input().split())
    dq = []
    for i in range(n):dq.append(deque())
    for _ in range(m):
        q = input().split()
        ind = int(q[1])
        if q[0] == '0':dq[ind].append(int(q[2]))
        elif dq[ind]:
            if q[0] == '1':print(dq[ind][0])
            else          :dq[ind].popleft()


if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
from collections import deque
n, q = map(int, input().split())
d = []
for i in range(n):
    d.append(deque())
for i in range(q):
    x, *y = map(int, input().split())
    if x == 0:
        d[y[0]].append(y[1])
    elif x == 1 and d[y[0]]:
        print(d[y[0]][0])
    elif x == 2 and d[y[0]]:
        d[y[0]].popleft()

Submission status: Accepted

Solution code:
from collections import deque

n, q = list(map(int, input().split()))
Q = deque()
[Q.append(deque([])) for _ in range(n)]

for i in range(q):
    query = list(map(int, input().split()))
    t = query[1]

    # enqueue
    if query[0] == 0:
        Q[t].append(query[2])

    # front
    elif query[0] == 1:
        if Q[t]:
            print(Q[t][0])

    # dequeue
    else:
        if Q[t]:
            Q[t].popleft()

Submission status: Accepted

Solution code:
import collections
n, q = map(int, input().split())
Q = [collections.deque() for i in range(n)]
queries = list()
for i in range(q):
    queries.append(list(map(int, input().split())))
for query in queries:
    if query[0] == 0:
        Q[query[1]].append(query[2])
    elif query[0] == 1:
        try:
            print(Q[query[1]][0])
        except:
            pass
    elif query[0] == 2:
        try:
            Q[query[1]].popleft()
        except:
            pass

Submission status: Accepted

Solution code:
import sys
input = sys.stdin.readline

from collections import deque

N,Q = map(int,input().split())

Queue = [deque([]) for _ in range(N)]

for _ in range(Q):
    q = list(map(int,input().split()))
    if q[0] == 0:
        Queue[q[1]].append(q[2])
    elif q[0] == 1:
        if Queue[q[1]]:
            print(Queue[q[1]][0])
    else:
        if Queue[q[1]]:
            Queue[q[1]].popleft()

Submission status: Accepted

Solution code:
from collections import deque

n,q=map(int,input().split())

qs = [deque() for _ in range(n)]

for _ in range(q):
	op = list(map(int,input().split()))
	que = qs[op[1]]
	if op[0] == 0:
		que.append(op[2])
	elif op[0] == 1:
		if len(que)>0:
			x = que.popleft()
			print(x)
			que.appendleft(x)
	else:
		if len(que)>0:
			que.popleft()

Submission status: Accepted

Solution code:
from collections import deque

def main():
    n, q = map(int, input().split())
    Qt = [deque() for _ in range(n)]

    for _ in range(q):
        para = [int(a) for a in input().split()]

        if para[0] == 0:
            Qt[para[1]].append(para[2])
        if para[0] == 1:
            if len(Qt[para[1]]) != 0:
                print(Qt[para[1]][0])
        if para[0] == 2:
            if len(Qt[para[1]]) != 0:
                Qt[para[1]].popleft()
main()

Submission status: Accepted

Solution code:
import collections


class TwoDimQueue():
    def __init__(self, num_queues):
        self.queues = [collections.deque() for i in range(0, num_queues, 1)]

    def enqueue(self, t, x):
        self.queues[t].appendleft(x)
        return self

    def front(self, t):
        if len(self.queues[t]) > 0:
            print(f'{self.queues[t][len(self.queues[t])-1]}')
        return self

    def dequeue(self, t):
        if len(self.queues[t]) > 0:
            self.queues[t].pop()
        return self


num_queues, q = tuple(map(int, input().split(' ')))
stacks = TwoDimQueue(num_queues)

for i in range(0, q):
    queue = tuple(map(int, input().split(' ')))
    if queue[0] == 0:
        stacks.enqueue(queue[1], queue[2])
    elif queue[0] == 1:
        stacks.front(queue[1])
    elif queue[0] == 2:
        stacks.dequeue(queue[1])

Submission status: Accepted

Solution code:
n,q=[int(i) for i in input().split(" ")]

Q=[[] for i in range(n)]

for _ in range(q):
    query=[int(j) for j in input().split(" ")]
    if query[0]==0:
        Q[query[1]].append(query[2])
    elif query[0]==1:
        if len(Q[query[1]])>0:
            print(Q[query[1]][0])
    else:
        if len(Q[query[1]])>0:
            Q[query[1]].pop(0)

Submission status: Accepted

Solution code:
from collections import deque
n, c = [int(x) for x in input().split()]
Q = []
for _ in range(n):
    Q.append(deque())
for _ in range(c):
    p, t, x = [int(_) for _ in (input()+" 0").split()][:3]
    if p == 0:
        Q[t].append(x)
    elif p == 1:
        try:
            print(Q[t][0])
        except IndexError:
            pass
    elif p == 2:
        try:
            Q[t].popleft()
        except IndexError:
            pass

Submission status: Accepted

Solution code:
from collections import deque
import sys

n, q = map(int, input().split())

Q = [deque() for _ in range(n)]
ans = []
# for _ in range(q):
#     query = list(map(int,sys.stdin.readline().split()))
# for query in (list(map(int,line.split())) for line in sys.stdin):
#     if query[0] == 0:Q[query[1]].append(query[2])
#     elif query[0] == 1 and Q[query[1]]:ans.append(Q[query[1]][0])
#     elif query[0] == 2 and Q[query[1]]:Q[query[1]].popleft()
for query in (line.split() for line in sys.stdin):
    t = int(query[1])
    if query[0] == '0':Q[t].append(query[2])
    elif query[0] == '1' and Q[t]:ans.append(Q[t][0])
    elif query[0] == '2' and Q[t]:Q[t].popleft()

for i in ans:
    print(i)

Submission status: Accepted

Solution code:
from collections import deque
from typing import List


if __name__ == "__main__":
    stack_num, query_num = map(lambda x: int(x), input().split())
    stacks: List[deque] = [deque() for _ in range(stack_num)]

    for _ in range(query_num):
        op, *value = map(lambda x: int(x), input().split())
        if (0 == op):
            stacks[value[0]].appendleft(value[1])
        elif (1 == op):
            stack = stacks[value[0]]
            if (0 != len(stack)):
                print(stack[len(stack) - 1])
        elif (2 == op):
            stack = stacks[value[0]]
            if (0 != len(stack)):
                stack.pop()
        else:
            pass

Submission status: Accepted

Solution code:
from collections import deque
n,q=map(int, input().split( ))
Q=[]
for i in range(n):
    D=deque()
    Q.append(D)
for i in range(q):
    c,*a=map(int,input().split( ))
    if c==0:
        Q[a[0]].append(a[1])
    elif c==1:
        if len(Q[a[0]])>0:
            print(Q[a[0]][0])
        else:
            pass
    elif c==2:
        if len(Q[a[0]])>0:
            Q[a[0]].popleft()
        else:
            pass

Submission status: Accepted

Solution code:
import sys
from collections import defaultdict, deque
n = int(sys.stdin.readline().split()[0])
A = defaultdict(deque)
ans = []
for query in sys.stdin:
    if query[0] == '0':
        t, x = query[2:].split()
        A[t].append(x)
    elif query[0] == '1':
        if A[query[2:-1]]:
            ans.append(A[query[2:-1]][0] + '\n')
    else:
        if A[query[2:-1]]:
            A[query[2:-1]].popleft()
sys.stdout.writelines(ans)

Submission status: Accepted

Solution code:
def solve():
    from sys import stdin
    f_i = stdin
    
    n, q = map(int, f_i.readline().split())
    
    from collections import deque
    Q = [deque() for i in range(n)]
    
    ans = []
    
    for op in (line.split() for line in f_i):
        op_type = op[0]
        t = int(op[1])
        qt = Q[t]
        if op_type == '0':
            qt.append(op[2])
        elif qt:
            if op_type == '1':
                ans.append(qt[0])
            else:
                qt.popleft()
    print('\n'.join(ans))

solve()

Submission status: Accepted

Solution code:
from sys import stdin,stdout
from collections import defaultdict,deque

n, q = map(int,stdin.readline().split())
queries = stdin.readlines()
A = defaultdict(deque)
ans = []

#count = 0
for query in queries:
    #print(count)
    #count += 1
    query = query.split()
    if query[0] == '0':
        A[int(query[1])].append(query[2])
    elif query[0] == '1':
        index = int(query[1])
        if A[index]:
            ans.append(A[index][0]+'\n')
    else:
        index = int(query[1])
        if A[index]:
            A[index].popleft()

stdout.writelines(ans)

Submission status: Accepted

Solution code:
n,q = map(int,input().split())
lis1 = []
for i in range(n):
  lis1.append([])
for i in range(q):
  lis = list(map(int,input().split()))
  if lis[0] == 0:
    lis1[lis[1]].append(lis[2])
    
  elif lis[0] == 1 and len(lis1[lis[1]])>0:
    print(lis1[lis[1]][0])
  elif lis[0] == 2 and len(lis1[lis[1]])>0:
    del lis1[lis[1]][0]

Submission status: Accepted

Solution code:
def enqueue(t,x):
    list_t = list[t]
    list_t.append(x)
    return list

def front(t):
    list_t = list[t]
    if len(list_t)==0:
        return
    else:
        print(list_t[0])
        return list
def dequeue(t):
    list_t = list[t]
    if len(list_t) ==0:
        return
    else:
        del(list_t[0])
        return list
    
list1 = [int(i) for i in input().split()]
n = list1[0]
q = list1[1]

list=[]

for i in range(n):
    list_i = []
    list.append(list_i)

for i in range(q):
    list2 = [int(j) for j in input().split()]
    a = list2[0]
    if a == 0:
        t = list2[1]
        x = list2[2]
        enqueue(t,x)
    elif a ==1:
        t = list2[1]
        front(t)
    else :
        t = list2[1]
        dequeue(t)

Submission status: Accepted

Solution code:
[n,q] = [int(i) for i in input().split()]

i = 0
queue = []
for j in range(n):
	queue.append([])

while i < q:
	query = [int(i) for i in input().split()]
	if query[0] == 0:
		queue[query[1]].append(query[2])
	
	elif query[0] == 1:
		if len(queue[query[1]]) == 0:
			pass
		else:
			print(queue[query[1]][0])
		
	else:
		if len(queue[query[1]]) == 0:
			pass
		else:
			del queue[query[1]][0]
			
	i += 1

Submission status: Accepted

Solution code:
n , q =  list(map(int, input().split()))

Q = []
for i in range(n):
    Q.append([])

for i  in range(q):
    query = list(map(int, input().split()))
    if query[0] == 0:
        Q[query[1]].append(query[2])
    elif query[0] == 1:
        if bool(Q[query[1]]):
            print(Q[query[1]][0])
    elif query[0] == 2:
        if bool(Q[query[1]]):
            del Q[query[1]][0]

Submission status: Accepted

Solution code:
n, q = map(int, input().split())
l = [[] for i in range(n)]
for i in range(q):
    #print(l)
    do = list(map(int, input().split()))
    #print("do::")
    #print(do)
    if do[0] == 0: l[do[1]].append(do[2])
    elif l[do[1]] == []: continue
    elif do[0] == 1: print(l[do[1]][0])
    else: del l[do[1]][0]

Submission status: Accepted

Solution code:
n,q=map(int,input().split())
list1=[[] for k in range(n)]
for i in range(q):
  list2=list(map(int,input().split()))
  if list2[0]==0:
    list1[list2[1]].append(list2[2])
  elif list2[0]==1:
    if list1[list2[1]]!=[]:
      print(list1[list2[1]][0])
  else:
    if list1[list2[1]]!=[]:
      del list1[list2[1]][0]

Submission status: Accepted

Solution code:
n,q = map(int,input().split())
li = []
for i in range(n):
    li.append([])

for i in range(q):
    x = list(map(int,input().split()))
    if len(x) == 3:
        li[x[1]].append(x[2])
    else:
        if x[0] == 1:
            if li[x[1]] != []:
                print(li[x[1]][0])
        else:
            if li[x[1]] != []:
                li[x[1]].remove(li[x[1]][0])

Submission status: Accepted

Solution code:

n,q = map(int,input().split())

Q = []

for i in range(n):
    Q.append([])

for i in range(q):

    nq = list(map(int,input().split()))

    if nq[0] == 0:

        Q[nq[1]].append(nq[2])

    elif nq[0] == 1 and len(Q[nq[1]]) > 0:

        print (Q[nq[1]][0])

    elif len(Q[nq[1]]) > 0:

        del Q[nq[1]][0]

Submission status: Accepted

Solution code:
# -*- coding: utf-8 -*-
"""
Basic Data Structures - Queue
http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=ITP2_2_B&lang=jp

"""
from collections import deque

n, q = map(int, input().split())
queues = [deque() for _ in range(n)]
for _ in range(q):
    op, t, x = (input() + ' 1').split()[:3]
    if op == '0':
        queues[int(t)].append(x)
    elif op == '1':
        if queues[int(t)]:
            print(queues[int(t)][0])
    else:
        if queues[int(t)]:
            queues[int(t)].popleft()

Submission status: Accepted

Solution code:
from collections import deque
n, q = map(int, input().split())
Q = []
for i in range(n): Q.append(deque())
for i in range(q):
    a = input().split()
    t = int(a[1])
    if   a[0] == '0': Q[t].append(a[2])        # push
    elif a[0] == '1' and Q[t]: print(Q[t][0])  # front
    elif a[0] == '2' and Q[t]: Q[t].popleft()  # dequeue

Submission status: Accepted

Solution code:
from collections import deque
n, queries = map(int, input().split())
Q = []
for i in range(n):
    Q.append(deque())
for i in range(queries):
    q, t, *x = map(int, input().split())
    if q == 0:
        Q[t].append(x[0])
    elif q == 1:
        if Q[t]:print(Q[t][0])
    elif q == 2:
        if Q[t]:Q[t].popleft()

Submission status: Accepted

Solution code:
from collections import deque


n, q = map(int, input().split())

S = [deque([]) for _ in range(n)]


for _ in range(q):

    command, *list_num = input().split()

    t = int(list_num[0])

    if   command == "0":
        # enqueue(t, x)
        x = int(list_num[1])
        S[t].append(x)
        
    elif command == "1":
        # front(t)
        if S[t]:
            print(S[t][0])
    elif command == "2":
        # dequeue(t)
        if S[t]:
            S[t].popleft()
    else:
        raise

Submission status: Accepted

Solution code:
from collections import deque

n, q = map(int, input().split())
box = [deque()for i in range(n)]
for _ in range(q):
    a, b, *c = map(int, input().split())
    if a == 0:
        box[b].append(c[0])
    elif a == 1:
        if len(box[b]):
            print(box[b][0])
    else:
        if box[b]:box[b].popleft()

Submission status: Accepted

Solution code:
from sys import stdin

class QueueNode(object):
	def __init__(self,data,next=None):
		self.data = data
		self.next = next

class LinkList(object):
	def __init__(self,head=None):
		self.head = head
		self.tail = head

	#後ろに挿入する
	def insert_q(self,data):
		node = QueueNode(data)

		if self.head is None:
			self.head = node
			self.tail = node
			return

		currentNode = self.tail

		while True:
			if currentNode.next is None:
				currentNode.next = node
				self.tail = node
				break
			currentNode = currentNode.next

	#先頭を削除する
	def delete_q(self):
		if self.head is not None:
			currentNode = self.head
			self.head = currentNode.next

	#先頭を表示する
	def print_q(self):
		if self.head is not None:
			print(self.head.data)

if __name__ == '__main__':

	qnum,loopnum = map(int,input().split(" "))

	q = [LinkList() for i in range(qnum)]

	for i in range(loopnum):
		cmd = stdin.readline().strip().split()
		t = int(cmd[1])
		if cmd[0] == "0":
			q[t].insert_q(int(cmd[2]))
		elif cmd[0] == "1":
			q[t].print_q()
		else:
			q[t].delete_q()

Submission status: Accepted

Solution code:
from collections import deque
n, r = input().split(' ')
stack = [deque([]) for _ in range(int(n))]
for _ in range(int(r)):
    row = list(map(int, input().split(' ')))
    c = row[0]
    i = row[1]

    if c == 0:
        stack[i].append(row[2])
    if stack[i]:
        if c == 1:
            print(stack[i][0])
        if c == 2:
            stack[i].popleft()

Submission status: Accepted

Solution code:
from collections import deque
n,q = map(int, input().split())
S = []

for i in range(n):
    S.append(deque())
for i in range(q):
    a = input().split()
    t = int(a[1])
    
    if a[0] == '0':
        S[t].append(a[2])
    elif a[0] == '1' and S[t]:
        print(S[t][0])
    elif a[0] == '2' and S[t]:
        S[t].popleft()

Submission status: Accepted

Solution code:
def enqueue(Q,t,x):
    Q[t].append(x)
    return Q

def front(Q,t):
    count=len(Q[t])
    if count!=0:
        print(Q[t][0])

def dequeue(Q,t):
    count=len(Q[t])
    if count!=0:
        del Q[t][0]
    return Q

n,q=(int(x) for x in input().split())
Q=[[] for i in range(n)]
for i in range(q):
    query=list(map(int,input().split()))
    if query[0]==0:
        Q=enqueue(Q,query[1],query[2])
    elif query[0]==1:
        front(Q,query[1])
    else:
        Q=dequeue(Q,query[1])

Submission status: Accepted

Solution code:
from collections import deque
n,q = map(int,input().split())

stack = deque([[] for i in range(n)])

for i in range(q):
    query = [k for k in map(int,input().split())]

    if query[0] == 0:
        stack[query[1]].append(query[2])
    elif query[0] == 1:
        try:
            print(stack[query[1]][0])
        except:
            pass
    elif query[0] == 2:
        try:
            stack[query[1]].pop(0)
        except:
            pass

Submission status: Accepted

Solution code:
import sys
from collections import deque
n, q = map(int, input().split())

queues = {str(i): deque() for i in range(n)}

lines = sys.stdin.readlines()
for i in range(q):
    query, *arg = lines[i].split()
    if query == '0':    # enqueue t x
        queues[arg[0]].append(arg[1])
    elif query == '1':  # front
        if len(queues[arg[0]]):
            print(queues[arg[0]][0])
    elif query == '2':  # dequeue
        if len(queues[arg[0]]):
            queues[arg[0]].popleft()
    else:
        raise AssertionError

Submission status: Accepted

Solution code:
# http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=ITP2_2_B&lang=jp
# Queue

from collections import deque
import sys
input = sys.stdin.readline

def loop_proc():
    (n1,n2) = map(int,input().split())
    wl =[]
    for i in range(n1):
        wl.append(deque())
    for i in range(n2):
        l = list(map(int,input().split()))  
        if (l[0] == 0 ):
            wl[l[1]].appendleft(l[2])
        elif (l[0] == 1 ):
            if ( len(wl[l[1]]) != 0 ):
                tl = wl[l[1]]
                print (tl[-1])
        elif (l[0] == 2 ):
            if ( len(wl[l[1]]) != 0 ):
                wl[l[1]].pop()
 

loop_proc()

Submission status: Accepted

Solution code:
n, q = map(int, input().split())
stack = [[] for _ in range(n)]

for _ in range(q):
    query = list(map(int, input().split()))
    if query[0] == 0:
        stack[query[1]].append(query[2])
    elif query[0] == 1:
        if stack[query[1]] == []:
            pass
        else:
            print(stack[query[1]][0])
    elif query[0] == 2:
        if stack[query[1]] == []:
            pass
        else:
            stack[query[1]].pop(0)

Submission status: Accepted

Solution code:
import sys
from collections import defaultdict, deque
n = int(sys.stdin.readline().split()[0])
A = defaultdict(deque)
ans = []
for query in sys.stdin:
    if query[0] == '0':
        t, x = query[2:].split()
        A[t].append(x)
    elif query[0] == '1':
        if A[query[2:-1]]:
            ans.append(A[query[2:-1]][0] + '\n')
    else:
        if A[query[2:-1]]:
            A[query[2:-1]].popleft()
sys.stdout.writelines(ans)

Submission status: Accepted

Solution code:
from collections import deque
readline = open(0).readline
writelines = open(1, 'w').writelines
N, Q = map(int, readline().split())
ans = []
A = [deque() for i in range(N)]
def push(t, x):
    A[t].append(x)
def top(t):
    A[t] and ans.append("%d\n" % A[t][0])
def pop(t):
    A[t] and A[t].popleft()

C = [push, top, pop].__getitem__
for i in range(Q):
    t, *a=map(int, readline().split())
    C(t)(*a)
writelines(ans)