Problem:


Score : 300 points

Problem Statement

In Takahashi Kingdom, there is an archipelago of N islands, called Takahashi Islands.
For convenience, we will call them Island 1, Island 2, ..., Island N.

There are M kinds of regular boat services between these islands.
Each service connects two islands. The i-th service connects Island a_i and Island b_i.

Cat Snuke is on Island 1 now, and wants to go to Island N.
However, it turned out that there is no boat service from Island 1 to Island N, so he wants to know whether it is possible to go to Island N by using two boat services.

Help him.

Constraints

3 ≤ N ≤ 200 000

1 ≤ M ≤ 200 000

1 ≤ a_i < b_i ≤ N

(a_i, b_i) \neq (1, N)

If i \neq j, (a_i, b_i) \neq (a_j, b_j).

Input

Input is given from Standard Input in the following format:

N M
a_1 b_1
a_2 b_2
:
a_M b_M

Output

If it is possible to go to Island N by using two boat services, print POSSIBLE; otherwise, print IMPOSSIBLE.

Sample Input 1

3 2
1 2
2 3

Sample Output 1

POSSIBLE

Sample Input 2

4 3
1 2
2 3
3 4

Sample Output 2

IMPOSSIBLE

You have to use three boat services to get to Island 4.

Sample Input 3

100000 1
1 99999

Sample Output 3

IMPOSSIBLE

Sample Input 4

5 5
1 3
4 5
2 3
2 4
1 4

Sample Output 4

POSSIBLE

You can get to Island 5 by using two boat services: Island 1 -> Island 4 -> Island 5.



Sample input:
3 2
1 2
2 3


Sample output:
POSSIBLE

Submission status: Accepted

Solution code:
n,m=map(int,input().split())
a=[]
for i in range(m):
    a.append(list(map(int,input().split())))
a.sort()
#print(a)
d=[]
for i in range(m):
    if a[i][0]==1:
        d.append(a[i][1])
    else:
        break
e=[]
for i in range(m):
    if a[i][1]==n:
        e.append(a[i][0])
if len(set(e) & set(d))!=0:
    print('POSSIBLE')
else:
    print('IMPOSSIBLE')

Submission status: Accepted

Solution code:
import sys
N, M = list(map(int, input().split()))
S = set()
B = [False] * (N + 1)
for i in range(M):
    a, b = list(map(int, input().split()))
    if a == 1:
        S.add(b)
    elif b == N:
        B[a] = True
for s in S:
    if B[s]:
        print('POSSIBLE')
        sys.exit()
print('IMPOSSIBLE')

Submission status: Accepted

Solution code:
N,M=map(int,input().split())
E=[[False,False] for _ in range(N+1)]

for _ in range(M):
    a,b=map(int,input().split())

    if a==1:
        E[b][0]=True

    if b==N:
        E[a][1]=True

F=False
for n in range(1,N+1):
    F|=(E[n][0]&E[n][1])

if F:
    print("POSSIBLE")
else:
    print("IMPOSSIBLE")

Submission status: Accepted

Solution code:
from collections import deque
n, m = map(int, input().split())
p = [tuple(map(int, input().split())) for _ in range(m)]

d = dict()
for a, b in p:
    if a not in d.keys():
        d[a] = [b]
    else:
        d[a].append(b)
    if b not in d.keys():
        d[b] = []

node = deque()
node.append((1, 0))
visit = set()
while node:
    (s, count) = node.popleft()
    if s in visit:
        continue
    if s == n:
        if count == 2:
            print('POSSIBLE')
            exit()
        else:
            break
    for i in d[s]:
        node.append((i, count+1))
    visit.add(s)
print('IMPOSSIBLE')

Submission status: Accepted

Solution code:
N,M = map(int,input().split())
x = set()
y = set()
 
for m in range(M):
    a,b = map(int,input().split())
    if a==1:
        x.add(b)
    if b==N:
        y.add(a)

if x&y:
    print("POSSIBLE")
else:
    print("IMPOSSIBLE")

Submission status: Accepted

Solution code:
N, M = map(int, input().split())

s = set()
g = set()

for _ in range(M):
    A, B = map(int, input().split())
    if A == 1:
        s.add(B)
    if B == N:
        g.add(A)

if s & g:
    print("POSSIBLE")
else:
    print("IMPOSSIBLE")

Submission status: Accepted

Solution code:
N,M = map(int,input().split())
x = set()
y = set()

for m in range(M):
  a,b = map(int,input().split())
  if a==1:
    x.add(b)
  if b==N:
    y.add(a)

if x&y:
  print("POSSIBLE")
else:
  print("IMPOSSIBLE")

Submission status: Accepted

Solution code:
N, M = map(int, input().split())
check = [0] * N
check[0] = 1
ab = []
for i in range(M):
    a, b = map(int, input().split())
    ab.append([a, b])
    if a == 1:
        check[b-1] = 1

for a, b in ab:
    if check[a-1] == 1 and b == N:
        print('POSSIBLE')
        break
else:
    print('IMPOSSIBLE')

Submission status: Accepted

Solution code:
n, m = map(int, input().split())
x = [0]*n
y = [0]*n
for i in range(m):
    a, b = map(int, input().split())
    a -= 1
    b -= 1
    if a == 0:
        x[b] = 1
    if b == n-1:
        y[a] = 1

f = 0
for i in range(n):
    if x[i]*y[i] > 0:
        f = 1

if f:
    print("POSSIBLE")
else:
    print("IMPOSSIBLE")

Submission status: Accepted

Solution code:
import sys
def input(): return sys.stdin.readline().strip()
def mapint(): return map(int, input().split())
sys.setrecursionlimit(10**9)

N, M = mapint()
query = [[] for _ in range(N)]
for _ in range(M):
    a, b = mapint()
    query[a-1].append(b-1)
    query[b-1].append(a-1)

checked = [0]*N
ans = False
def dfs(now, cnt):
    global ans
    if cnt==2:
        if now==N-1:
            ans = True
        return
    checked[now] = 1
    for nx in query[now]:
        if not checked[nx]:
            dfs(nx, cnt+1)

dfs(0, 0)
if ans:
    print('POSSIBLE')
else:
    print('IMPOSSIBLE')

Submission status: Accepted

Solution code:
N,M = map(int,input().split())
R = [set([]) for _ in range(N)]
for i in range(M):
  a,b = map(int,input().split())
  a-=1;b-=1
  R[a].add(b)
  R[b].add(a)
#print(R)
for i in range(1,N-1):
  if 0 in R[i] and N-1 in R[i]:
    print("POSSIBLE")
    exit()
print("IMPOSSIBLE")

Submission status: Accepted

Solution code:
import heapq
INFTY = 10**6
N,M = map(int,input().split())
G = {i:[] for i in range(1,N+1)}
for _ in range(M):
    a,b = map(int,input().split())
    G[a].append(b)
    G[b].append(a)
dist = [INFTY for _ in range(N+1)]
dist[1] = 0
hist = [0 for _ in range(N+1)]
hist[1] = 1
heap = [(0,1)]
while heap:
    d,x = heapq.heappop(heap)
    if dist[x]<d:continue
    hist[x] = 1
    for y in G[x]:
        if hist[y]==0:
            if dist[y]>d+1:
                dist[y] = d+1
                heapq.heappush(heap,(d+1,y))
if dist[N]==2:
    print("POSSIBLE")
else:
    print("IMPOSSIBLE")

Submission status: Accepted

Solution code:
n,m=map(int,input().split())
a=[0]*(n+1)
b=[0]*(n+1)
for i in range(m):
  x,y=map(int,input().split())
  if x==1:
    a[y]=1
  if y==n:
    b[x]=1
ans=0
for i in range(n+1):
  ans+=a[i]*b[i]
if ans==0:
  print('IMPOSSIBLE')
else:
  print('POSSIBLE')

Submission status: Accepted

Solution code:
from sys import stdin
n,m=map(int,input().split())
l = [list(map(int, stdin.readline().split())) for _ in range(m)]
s,e=[],[]
for i in l:
    if i[0]==1:
        s.append(i[1])
    if i[1]==n:
        e.append(i[0])
result = list(set(s) & set(e))
print('POSSIBLE' if result else 'IMPOSSIBLE')

Submission status: Accepted

Solution code:
def resolve():
    '''
    code here
    '''
    import collections
    N, M = [int(item) for item in input().split()]
    edges = [[int(item) for item in input().split()] for _ in range(M)]
    
    togo = [[] for _ in range(N+1)]
    for f, t in edges:
        togo[f] += [t]
    
    is_found = False

    que = collections.deque()
    for goto in togo[1]:
        que.append(goto)

    is_found = False
    while que:
        node = que.popleft()
        for next_node in togo[node]:
            if next_node == N:
                is_found = True
            # print(next_node, is_found)
            
    print('POSSIBLE') if is_found else print('IMPOSSIBLE')

if __name__ == "__main__":
    resolve()

Submission status: Accepted

Solution code:
N, M = map(int, input().split())
L = [list(map(int, input().split())) for _ in range(M)]
L_1 = set([l[1] for l in L if l[0]==1])
L_2 = set([l[0] for l in L if l[1]==N])
print("POSSIBLE" if L_1&L_2 else "IMPOSSIBLE")

Submission status: Accepted

Solution code:
n, m = [int(x) for x in input().split()]
f_set = set()
t_set = set()
for _ in range(m):
    a, b = [int(x) for x in input().split()]
    if a == 1:
        f_set.add(b)
    elif b == n:
        t_set.add(a)
print("POSSIBLE" if len(f_set & t_set) >= 1 else "IMPOSSIBLE")

Submission status: Accepted

Solution code:
N,M = list(map(int,input().split()))

ship={}
for n in range(N):
    n=n+1
    ship[n]=[]

for m in range(M):
    a,b=list(map(int,input().split()))
    if a == 1:
        ship[a].append(b)
    else :
        if b == N:
            ship[a].append(b)

ans='IMPOSSIBLE'
for i in ship[1]:
    if len(ship[i]) != 0:
        ans='POSSIBLE'
        break
print(ans)

Submission status: Accepted

Solution code:
#!/usr/bin/env python
# coding: utf-8

# In[22]:


N,M = map(int, input().split())
ab = []
for _ in range(M):
    ab.append(list(map(int, input().split())))


# In[23]:


start = [sorted(ship)[1] for ship in ab if 1 in ship]
goal = [sorted(ship)[0] for ship in ab if N in ship]
if set(start) & set(goal):
    print("POSSIBLE")
else:
    print("IMPOSSIBLE")


# In[ ]:

Submission status: Accepted

Solution code:
# -*- coding: utf-8 -*-
import sys
from collections import deque, defaultdict
from math import sqrt, factorial, gcd
# def input(): return sys.stdin.readline()[:-1] # warning not \n
# def input(): return sys.stdin.buffer.readline().strip() # warning bytes
# def input(): return sys.stdin.buffer.readline().decode('utf-8')


def solve():
    n, m = [int(x) for x in input().split()]
    g = defaultdict(set)
    for _ in range(m):
        a, b = [int(x) for x in input().split()]
        g[a].add(b)
        g[b].add(a)
    
    for i in range(2, n):
        if 1 in g[i] and n in g[i]:
            print("POSSIBLE")
            return
    print("IMPOSSIBLE")



t = 1
# t = int(input())
for case in range(1,t+1):
    ans = solve()


"""

abc

"""

Submission status: Accepted

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

ab = [[] for _ in range(n)]
for _ in range(m):
    a, b = map(int, input().split())
    ab[a-1].append(b)
    ab[b-1].append(a)

for i in ab[0]:
    if n in ab[i-1]:
        print('POSSIBLE')
        exit()
print('IMPOSSIBLE')

Submission status: Accepted

Solution code:
from collections import deque


def main():
    n, m = map(int, input().split())
    shuttles = [[] for _ in range(n+1)]
    for i in range(m):
        s, e = map(int, input().split())
        shuttles[s].append(e)
    d = deque(shuttles[1])
    ans = 'IMPOSSIBLE'
    while d:
        j = d.popleft()
        if n in shuttles[j]:
            ans = 'POSSIBLE'
            break
    print(ans)


if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:
n,m = map(int,input().split())
l = [-1] + [[] for i in range(n)]
for i in range(m):
    a,b = map(int,input().split())
    l[a].append(b)
    l[b].append(a)
for mid in l[1]:
    if n in l[mid]:
        print('POSSIBLE')
        exit()
print('IMPOSSIBLE')

Submission status: Accepted

Solution code:
n, m = input().split()

p = set()
q = set()
for _ in range(int(m)):
    a, b = input().split()
    if a == "1":
        p.add(b)
    elif b == n:
        q.add(a)
else:
    if p & q:
        print("POSSIBLE")
    else:
        print("IMPOSSIBLE")

Submission status: Accepted

Solution code:
N,M=map(int,input().split())

lands=[False for _ in range(N-1)]
goal_land=[]

for _ in range(M):
    a,b = map(int,input().split())
    if a == 1 or b == 1:
        if a == 1:
            lands[b-1] = True
        else:
            lands[a-1] = True
    elif a == N or b == N:
        if a == N:
            goal_land.append(b-1)
        else:
            goal_land.append(a-1)

#print(lands)
#print(goal_land)

for i in goal_land:
    if lands[i]:
        print('POSSIBLE')
        exit()
print('IMPOSSIBLE')

Submission status: Accepted

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

p = set()
q = set()

for _ in range(m):
    a, b = map(int, input().split())
    if a == 1:
        p.add(b)
    elif b == n:
        q.add(a)
else:
    print("POSSIBLE" if p & q else "IMPOSSIBLE")

Submission status: Accepted

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

p = set()
q = set()

for _ in range(m):
    a, b = map(int, input().split())
    if a == 1:
        p.add(b)
    elif b == n:
        q.add(a)
else:
    if p & q:
        print("POSSIBLE")
    else:
        print("IMPOSSIBLE")

Submission status: Accepted

Solution code:
def main():
    from collections import defaultdict
    n,m = map(int,input().split())
    G = defaultdict(set)
    for _ in range(m):
        a,b = map(int,input().split())
        G[a].add(b)
        G[b].add(a)

    flg = 0
    for i in G[1]:
        if n in G[i]:
            flg = 1
    if flg == 1:
        print('POSSIBLE')
    else:
        print('IMPOSSIBLE')

main()

Submission status: Accepted

Solution code:
N, M = map(int, input().split())
teiki_1 = []
teiki_N = []
for _ in range(M):
    a, b = map(int, input().split())
    if a == 1 or b == 1:
        teiki_1.append(a if a != 1 else b)
    if a == N or b == N:
        teiki_N.append(a if a != N else b)
if len(set(teiki_1) & set(teiki_N)) > 0:
    print('POSSIBLE')
else:
    print('IMPOSSIBLE')

Submission status: Accepted

Solution code:
n,m = map(int,input().split())
g = [set() for _ in range(n)]
for _ in range(m):
    a,b = map(int,input().split())
    a-=1
    b-=1
    g[a].add(b)
    g[b].add(a)
for i in g[0]:
    if i in g[n-1]: print('POSSIBLE');exit()
print('IMPOSSIBLE')

Submission status: Accepted

Solution code:
N, M = map(int, input().split())
left = []
right = []
for i in range(M):
  a, b = map(int, input().split())
  if a == 1:
    left.append(b)
  elif b == N:
    right.append(a)
    
flag = False
left = set(left)
for r in right:
  if r in left:
    flag = True
    break
    
if flag:
  print('POSSIBLE')
else:
  print('IMPOSSIBLE')

Submission status: Accepted

Solution code:
N, M = map(int, input().split())
left = []
right = []
for i in range(M):
  a, b = map(int, input().split())
  if a == 1:
    left.append(b)
  elif b == N:
    right.append(a)
    
flag = False
left = set(left)
for r in right:
  if r in left:
    flag = True
    break
    
if flag:
  print('POSSIBLE')
else:
  print('IMPOSSIBLE')

Submission status: Accepted

Solution code:
n,m = map(int, input().split())
a = [list(map(int, input().split())) for _ in range(m)]
w = [0]*(n+1)
for i,j in a:
    if (i==1):
        w[j] = 1
for i,j in a:
    if (j==n):
        if (w[i]==1):
            print("POSSIBLE")
            exit()
print("IMPOSSIBLE")

Submission status: Accepted

Solution code:
n,m=map(int,input().split())
A=set()
B=set()

for i in range(m):
  a,b=map(int,input().split())
  if a==1:
    A.add(b)
  if b==n:
    B.add(a)
if len(A&B)==0:
  print("IMPOSSIBLE")
else:
  print("POSSIBLE")

Submission status: Accepted

Solution code:
from collections import defaultdict
from collections import deque
from collections import Counter
import itertools
import math

def readInt():
	return int(input())
def readInts():
	return list(map(int, input().split()))
def readChar():
	return input()
def readChars():
	return input().split()

d = {}
for i in range(200002):
	d[i]={}

n,m = readInts()
ab = [readInts() for i in range(m)]
for i in ab:
	d[i[0]][i[1]]=1

for i in d[1]:
	if n in d[i]:
		print("POSSIBLE")
		exit()

print("IMPOSSIBLE")

Submission status: Accepted

Solution code:
from collections import defaultdict
from collections import deque
from collections import Counter
import itertools
import math

def readInt():
	return int(input())
def readInts():
	return list(map(int, input().split()))
def readChar():
	return input()
def readChars():
	return input().split()

import bisect
def isin(arr,x):
	#print(bisect.bisect_left(arr,x))
	return arr[min(bisect.bisect_left(arr,x),len(arr)-1)]==x

n,m = readInts()
ab = [readInts() for i in range(m)]
go1 = []
for i in range(m):
	if ab[i][0]==1:
		go1.append(ab[i][1])
go1.sort()
for i in range(m):
	if isin(go1,ab[i][0]) and ab[i][1]==n:
		print("POSSIBLE")
		exit()

print("IMPOSSIBLE")

Submission status: Accepted

Solution code:
N, M = map(int, input().split())

serv1 = set()
servN = set()
for _ in range(M):
    a, b = map(int, input().split())
    if a != 1 and b != N:
        continue

    if a == 1:
        serv1.add(b)
    elif b == N:
        servN.add(a)

print("POSSIBLE" if len(serv1.intersection(servN)) > 0 else "IMPOSSIBLE")

Submission status: Accepted

Solution code:
import sys
sys.setrecursionlimit(10**8)
n, m = map(int, input().split())
g = [[] for i in range(n)]
for i in range(m):
    a, b = map(int, input().split())
    a-=1; b-=1
    g[a].append(b)
    g[b].append(a)

dist = [float('inf')] * n
def dfs(s, p, depth):
    if depth>2: return
    dist[s] = min(dist[s], depth)
    for to in g[s]:
        if to==p: continue
        if dist[to] < dist[s]+1: continue
        dfs(to, s, depth+1)

dfs(0, -1, 0)

if dist[n-1] <= 2:
    print('POSSIBLE')
else:
    print('IMPOSSIBLE')

Submission status: Accepted

Solution code:
n, m = map(int, input().split())
c = [[] for _ in range(n+1)]
for i in range(m):
  a, b = map(int, input().split())
  c[a].append(b)
  c[b].append(a)

for i in c[1]:
  if n in c[i]:
    print('POSSIBLE')
    exit()

print('IMPOSSIBLE')

Submission status: Accepted

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

data1 = [0] * (n+1)
data2 = [0] * (n+1)

for _ in range(m):
    a, b = map(int, input().split())
    if a == 1:
        data1[b+1] = 1
    if b == n:
        data2[a+1] = 1


for i in range(n+1):
    if data1[i] == 1 and data2[i] == 1:
        print("POSSIBLE")
        exit()

print("IMPOSSIBLE")

Submission status: Accepted

Solution code:
n,m = map(int, input().split())
ab = [[] for _ in range(n)]
for _ in range(m):
  a,b = map(lambda i: int(i)-1, input().split())
  ab[a].append(b)
  ab[b].append(a)

for i in ab[0]:
  if ab[i].count(n-1) >= 1:
    print("POSSIBLE")
    exit()
print("IMPOSSIBLE")

Submission status: Accepted

Solution code:
n,m = map(int,input().split())
a_lst = []
for i in range(m):
  a_lst.append( list(map(int,input().split())) )
 
ans = "IMPOSSIBLE"
 
start_lst = []
goal_lst = []
for a in a_lst:
  if a[0] == 1:
    start_lst.append(a[1])
  if a[1] == n:
    goal_lst.append(a[0])
goal_lst = set(goal_lst)
for s in start_lst:
  if s in goal_lst:
    ans = "POSSIBLE"
    
print(ans)

Submission status: Accepted

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

start_set = set()
goal_set = set()
for i in range(m):
  s,g = map(int,input().split())
  if s == 1:
    start_set.add(g)
  if g == n:
    goal_set.add(s)    

ans = "POSSIBLE" if (start_set & goal_set) else "IMPOSSIBLE"
print(ans)

Submission status: Accepted

Solution code:
n,m=map(int,input().split())
list1=[]
listn=[]
for i in range(m):
    a,b=map(int,input().split())
    if a==1:
        list1.append(b)
    elif b==1:
        list1.append(a)
    elif a==n:
        listn.append(b)
    elif b==n:
        listn.append(a)
list1=set(list1)
listn=set(listn)
ans=0
if len(listn)!=0:
    for i in range(len(list1)):
        num=list1.pop()
        if num in listn:
            ans=1
            break
if ans==1:
    print("POSSIBLE")
else:
    print("IMPOSSIBLE")

Submission status: Accepted

Solution code:
import sys
sys.setrecursionlimit(500000)
MOD = 10**9+7

def input():
    return sys.stdin.readline()[:-1]

def mi():
    return map(int, input().split())

def ii():
    return int(input())

def i2(n):
    tmp = [list(mi()) for i in range(n)]
    return [list(i) for i in zip(*tmp)]

def main():
    N, M = mi()
    l = [[] for i in range(N)]
    for i in range(M):
        a, b = mi()
        l[a-1].append(b-1)
        l[b-1].append(a-1)

    for v in l[0]:
        if N-1 in l[v]:
            print('POSSIBLE')
            return
    print('IMPOSSIBLE')


if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
import sys
from collections import deque
N, M = map(int, input().split())
G = [[] for _ in range(N)]
for _ in range(M):
    a, b = map(int, input().split())
    G[a - 1].append(b - 1)
    G[b - 1].append(a - 1)
for g in G[0]:
    if N - 1 in G[g]:
        print('POSSIBLE')
        sys.exit()
print('IMPOSSIBLE')

Submission status: Accepted

Solution code:
import sys
from collections import deque
N, M = map(int, input().split())
G = [[] for _ in range(N)]
for _ in range(M):
    a, b = map(int, input().split())
    G[a - 1].append(b - 1)
    G[b - 1].append(a - 1)
d = [0] * N
d[0] = 1
Q = deque()
Q.append(0)
while Q:
    q = Q.popleft()
    if d[q] == 3:
        print('IMPOSSIBLE')
        sys.exit()
    for g in G[q]:
        if g == N - 1:
            print('POSSIBLE')
            sys.exit()
        if d[g] == 0:
            d[g] = d[q] + 1
            Q.append(g)
print('IMPOSSIBLE')

Submission status: Accepted

Solution code:
n, m = map(int, input().split())
lst = [[] for i in range(n + 1)]
for i in range(m):
  tpl = tuple(map(int, input().split()))
  lst[tpl[0]].append(tpl[1])
for i in lst[1]:
  if n in lst[i]:
    print('POSSIBLE')
    exit()
print('IMPOSSIBLE')

Submission status: Accepted

Solution code:
N,M = map(int,input().split())
a_list=[]
pass_1= []
for i in range(M):
    a = list(map(int,input().split()))
    if(a[0]==1):
        pass_1.append(a[1])
    elif(a[1]==N):
        a_list.append(a[0])
    
if list(set(pass_1) & set(a_list)):
    print('POSSIBLE')
else:
    print('IMPOSSIBLE')

Submission status: Accepted

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

front=[]
back=[]

for i in lst:
    if i[0]==1:
        front.append(i[1])

for i in lst:
    if i[1]==n:
        back.append(i[0])
front=set(front)
back=set(back)
print("POSSIBLE" if len(front&back)>=1 else "IMPOSSIBLE")

Submission status: Accepted

Solution code:
n, m = map(int, input().split())
route = [[] for _ in range(n)]
check = [0]*n
for _ in range(m):
    a, b = map(int, input().split())
    route[a-1].append(b-1)
pos = [0]
for i in range(2):
    # print(pos)
    tmp = list()
    for p in pos:
        for r in route[p]:
            if r == n-1:
                print('POSSIBLE')
                exit()
            if check[r-1] == 0:
                tmp.append(r)
                check[r-1] == 1
    pos = tmp

print('IMPOSSIBLE')

Submission status: Accepted

Solution code:
n,m=map(int,input().split())
a=[]
b=[]
for i in range(m):
    ab=list(map(int,input().split()))
    a.append(ab[0])
    b.append(ab[1])

islands1=set() #islands can reach from island 1
islands2=set() #islands can reach to island N

for i in range(m):
    if a[i]==1:
        islands1.add(b[i])
    
    if b[i]==n:
        islands2.add(a[i])

ans=islands1 & islands2
print("POSSIBLE" if ans else "IMPOSSIBLE")

Submission status: Accepted

Solution code:
import sys
import math
import itertools
import bisect
from copy import copy,deepcopy
from collections import deque,Counter
from decimal import Decimal
def s(): return input()
def i(): return int(input())
def S(): return input().split()
def I(): return map(int,input().split())
def L(): return list(map(int,input().split()))
def l(): return list(map(int,input().split()))
def lcm(a,b): return a*b//math.gcd(a,b)
sys.setrecursionlimit(10 ** 9)
INF = 10**9
mod = 10**9+7

N,M = I()
links = [[] for _ in range(N)]
for i in range(M):
    a,b = I()
    links[a-1].append(b-1)
    links[b-1].append(a-1)
for l1 in links[0]:
    for l2 in links[l1]:
        if l2 == N-1:
            print('POSSIBLE')
            exit()
print('IMPOSSIBLE')

Submission status: Accepted

Solution code:
f=lambda:map(int,input().split())
n,m=f()
c=[[] for _ in range(n+1)]
for _ in range(m):
    a,b=f()
    c[a]+=[b]
    c[b]+=[a]
print("IMPOSSIBLE" if all(n not in c[i] for i in c[1]) else "POSSIBLE")

Submission status: Accepted

Solution code:
from collections import deque

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

graph=[[]for _ in range(N+1)]

for _ in range(M):
    A,B=map(int,input().split()) # 入力に合わせて適宜変更
    graph[A].append(B)
    graph[B].append(A)

dist=[-1]*(N+1) # 出力に合わせて適宜変更
dist[0]=0
dist[1]=0

D=deque()
D.append(1)

while D:
    V=D.popleft()
    for i in graph[V]:
        if dist[i]!=-1:
            continue
        dist[i]=dist[V]+1 # 出力に合わせて適宜変更（答えに直結するので最重要）
        D.append(i)

print(['IMPOSSIBLE','POSSIBLE'][dist[N]==2])

Submission status: Accepted

Solution code:
n, m = map(int, input().split())
ans = [0]*(n+1)
for _ in [0]*m:
    a, b = map(int, input().split())
    if a > b:
        a, b = b, a
    if a==1:
        ans[b] += 1
    elif b==n:
        ans[a] += 1
if 2 in set(ans):
    print('POSSIBLE')
else:
    print('IMPOSSIBLE')

Submission status: Accepted

Solution code:
n,m = map(int,input().split())
leave = []
arrive = []
for i in range(m):
    a,b = map(int,input().split())
    if a == 1:
        leave.append(b)
    elif b == n:
        arrive.append(a)
if list(set(leave) & set(arrive)):
    print("POSSIBLE")
else:
    print("IMPOSSIBLE")

Submission status: Accepted

Solution code:
import bisect,copy,heapq,itertools,string
from collections import *
from math import *
import sys
def input() : return sys.stdin.readline().strip()
def INT()   : return int(input())
def MAP()   : return map(int,input().split())
def LIST()  : return list(MAP())

n, m = MAP()
a = [LIST() for i in range(m)]
c = [0]*(n+1)

for i in range(m):
        if a[i][0] == 1 or a[i][0] == n or a[i][1] == 1 or a[i][1] == n:
                c[a[i][0]] += 1
                c[a[i][1]] += 1

for i in range(2, n):
        if c[i] >= 2:
                print("POSSIBLE")
                exit()

print("IMPOSSIBLE")

Submission status: Accepted

Solution code:
n,m = map(int, input().split())
l = [[] for _ in range(n)]
for _ in range(m):
  a,b = map(lambda i: int(i)-1, input().split())
  l[a].append(b)
  l[b].append(a)

for i in l[0]:
  if n-1 in l[i]:
    print("POSSIBLE")
    exit()
print("IMPOSSIBLE")

Submission status: Accepted

Solution code:
N,M=map(int,input().split())
dp=[0]*(2*(10**5)+1)
bp=[0]*(2*(10**5)+1)
for _ in range(M):
  a,b=map(int,input().split())
  if a==1:
    if bp[b]==1:
      print('POSSIBLE')
      exit()
    dp[b]+=1
  if b==N:
    if dp[a]==1:
      print('POSSIBLE')
      exit()
    bp[a]+=1
print('IMPOSSIBLE')

Submission status: Accepted

Solution code:
n,m = map(int,input().split())
a_b = [list(map(int,input().split())) for _ in range(m)]
a_b = sorted(a_b,key=lambda x:x[0])

s = set()
for i in range(m):
    if a_b[i][0] == 1:
        s.add(a_b[i][1])
for i in range(m):
    if a_b[i][0] in s:
        if a_b[i][1] == n:
            print('POSSIBLE')
            exit()
print('IMPOSSIBLE')

Submission status: Accepted

Solution code:
n,m=map(int,input().split())
adj=[[]for i in range(n)]
for i in range(m):
    a,b=map(int,input().split())
    a,b=a-1,b-1
    adj[a].append(b)
    adj[b].append(a)
for i in adj[0]:
    if n-1 in adj[i]:
        print("POSSIBLE")
        exit()
print("IMPOSSIBLE")

Submission status: Accepted

Solution code:
n,m=map(int,input().split())
ans=[0]*n
for i in range(m):
    a,b=map(int,input().split())
    if a==1:
        ans[b]+=1
    if b==n:
        ans[a]+=1

bl=False
for i in range(n):
    if ans[i]>=2:
        bl=True

if bl:
    print("POSSIBLE")
else:
    print("IMPOSSIBLE")

Submission status: Accepted

Solution code:
import sys
n, m = map(int, input().split())
p = [list(map(int, input().split())) for _ in range(m)]
ln1 = []
ln2 = []
for i in p:
    if i[0] == 1:
        ln1.append(i[1])
for j in p:
    if j[1] == n:
        ln2.append(j[0])

if len(set(ln1) & set(ln2)) == 0:
    print('IMPOSSIBLE')
else:
    print('POSSIBLE')

Submission status: Accepted

Solution code:
from collections import defaultdict
n, m = map(int, input().split())
d = defaultdict(list)
for i in range(m):
  a, b = map(int, input().split())
  d[a].append(b)
ans = ''
for mid in d[1]:
  if ans == '':
    for goal in d[mid]:
      if goal == n:
        ans = 'POSSIBLE'
        break
if ans == '':
  ans = 'IMPOSSIBLE'
print(ans)

Submission status: Accepted

Solution code:
import sys
n, m = map(int, input().split())
p = [list(map(int, input().split())) for _ in range(m)]
ln1 = []
ln2 = []
for i in p:
    if i[0] == 1:
        ln1.append(i[1])
for j in p:
    if j[1] == n:
        ln2.append(j[0])

if len(set(ln1) & set(ln2)) == 0:
    print('IMPOSSIBLE')
else:
    print('POSSIBLE')

Submission status: Accepted

Solution code:
n,m = map(int,input().split())
a = []
a_1 = []
a_2 = []
for i in range(m):
    t = list(map(int,input().split()))
    if t[0] == 1:
        a_1.append(t[1])
    if t[1] == n:
        a_2.append(t[0])
    a.append(t)

a = a_1 + a_2
if len(set(a)) < len(a):
    print("POSSIBLE")
else:
    print("IMPOSSIBLE")

Submission status: Accepted

Solution code:
from collections import Counter
N, M = map(int, input().split())
L = [list(map(int, input().split())) for _ in range(M)]

L.sort(key=lambda x: x[0])

ans = []
for i in range(M):
    if (L[i][0] == 1):
        ans.append(L[i][1])

    if (L[i][1] == N):
        ans.append(L[i][0])

c = Counter(ans)
for i, v in c.items():
    if (v >= 2):
        print('POSSIBLE')
        exit()
print('IMPOSSIBLE')

Submission status: Accepted

Solution code:
N,M = list(map(int,input().split()))
xfrom1 = []
xton =[]
for i in range(M):
    a,b = list(map(int,input().split()))
    if a == 1:
        xfrom1.append(b)
    elif a== N:
        xton.append(b)
    elif b==N:
        xton.append(a)
if len(set(xfrom1) & set(xton)) >0:
    print('POSSIBLE')
else:
    print('IMPOSSIBLE')

Submission status: Accepted

Solution code:
n, m = map(int, input().split())
G = [[] for _ in range(n)]
for _ in range(m):
    a, b = map(lambda x: int(x) - 1, input().split())
    G[a].append(b)
    G[b].append(a)


from collections import deque
q = deque([[0, 0]])
visited = [0] * n
visited[0] = 1
while q:
    node, c = q.popleft()
    if c > 2:
        continue
    if node == n-1:
        print('POSSIBLE')
        break

    for to in G[node]:
        if visited[to] == 1:
            continue
        visited[to] = 1
        q.append([to, c+1])
else:
    print('IMPOSSIBLE')

Submission status: Accepted

Solution code:
import bisect
N, M = map(int, input().split())
start_to_middle = []
middle_to_end = []
for i in range(M):
    a, b = map(int, input().split())
    if a == 1:
        start_to_middle.append(b)
    elif b == N:
        middle_to_end.append(a)
if not len(start_to_middle) or not len(middle_to_end):
    print('IMPOSSIBLE')
    exit()
middle_to_end.sort()
for middle in start_to_middle:
    idx = bisect.bisect_left(middle_to_end, middle)
    if idx < len(middle_to_end) and middle_to_end[idx] == middle:
        print('POSSIBLE')
        exit()

print('IMPOSSIBLE')

Submission status: Accepted

Solution code:
N, M = map(int, input().split())
A = [[] for i in range(N)]
for i in range(M):
  a, b = map(int, input().split())
  A[a-1].append(b)
  A[b-1].append(a)
ans = 'IMPOSSIBLE'
for aa in A[0]:
  if N in A[aa-1]:
    ans = 'POSSIBLE'
    break
print(ans)

Submission status: Accepted

Solution code:
n,m = map(int, input().split())
ab = [list(map(int, input().split())) for _ in range(m)]
start = []
goal = []
for a,b in ab:
    if a == 1: start.append(b)
    elif b == n: goal.append(a)
print("POSSIBLE" if list(set(start) & set(goal)) else "IMPOSSIBLE")

Submission status: Accepted

Solution code:
import heapq
INFTY = 10**6
N,M = map(int,input().split())
G = {i:[] for i in range(1,N+1)}
for _ in range(M):
    a,b = map(int,input().split())
    G[a].append(b)
    G[b].append(a)
dist = [INFTY for _ in range(N+1)]
hist = [0 for _ in range(N+1)]
heap = [(0,1)]
dist[1] = 0
hist[1] = 1
while heap:
    d,x = heapq.heappop(heap)
    if dist[x]<d:continue
    hist[x]=1
    for y in G[x]:
        if hist[y]==0 and dist[y]>d+1:
            dist[y] = d+1
            heapq.heappush(heap,(d+1,y))
if dist[N]==2:
    print("POSSIBLE")
else:
    print("IMPOSSIBLE")