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:
import sys
sys.setrecursionlimit(200010)
def input():
    return sys.stdin.readline()[:-1]

def main():
    N, M = list(map(int,input().split()))
    G = [set([]) for k in range(N)]
    for i in range(M):
        A, B = set(map(int,input().split()))
        G[A-1].add(B-1)
        G[B-1].add(A-1)
    for e in G[0]:
        if e in G[N-1]:
            print("POSSIBLE")
            exit(0)
    print("IMPOSSIBLE")
if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
import collections
n, m = map(int, input().split())
visited = [False]*n
ships = [set() for i in range(n)]
for i in range(m):
    a, b = map(int, input().split())
    ships[a-1].add(b-1)
    ships[b-1].add(a-1)

for i in range(1,n-1):
    if 0 in ships[i] and n-1 in ships[i]:
        print('POSSIBLE')
        exit()
print('IMPOSSIBLE')

Submission status: Accepted

Solution code:
N, M = map(int, input().split())
edge = [[] for _ in range(N)]
for _ in range(M):
    a, b = map(int, input().split())
    a, b = a-1, b-1
    edge[a].append(b)
    edge[b].append(a)

ans = {True: "POSSIBLE",
    False: "IMPOSSIBLE"}

print(ans[len(set(edge[0]) & set(edge[-1])) > 0])

Submission status: Accepted

Solution code:
# -*- coding: utf-8 -*-


def main():
    import sys
    input = sys.stdin.readline

    n, m = map(int, input().split())
    first = list()
    second = list()

    for i in range(m):
        ai, bi = map(int, input().split())

        if ai == 1:
            first.append(bi)

        if bi == n:
            second.append(ai)

    ans = set(first) & set(second)

    if len(ans) > 0:
        print('POSSIBLE')
    else:
        print('IMPOSSIBLE')


if __name__ == '__main__':
    main()

Submission status: Accepted

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


def read():
    N, M = map(int, input().strip().split())
    AB = []
    for i in range(M):
        a, b = map(int, input().strip().split())
        AB.append((a, b))
    return N, M, AB


def solve(N, M, AB):
    is_ok = True
    mid_b = set()
    mid_a = set()
    for a, b in AB:
        if a == 1:
            mid_b.add(b)
        elif b == N:
            mid_a.add(a)
    is_ok = len(mid_a.intersection(mid_b)) > 0
    return "POSSIBLE" if is_ok else "IMPOSSIBLE"


if __name__ == '__main__':
    inputs = read()
    print(solve(*inputs))

Submission status: Accepted

Solution code:
N, M = [int(x) for x in input().split()]
AB = [[int(x) for x in input().split()] for _ in range(M)]


x = set()
for a, b in AB:
    if a == 1:
        x.add(b)
    if b == 1:
        x.add(a)

f = False

for a, b in AB:
    if (a in x and b == N) or (b in x and a == N):
        f = True

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

Submission status: Accepted

Solution code:
N,M = map(int,input().split())
AB = [tuple(map(int,input().split())) for i in range(M)]

s1 = set()
sn = set()
for a,b in AB:
    if a==1:
        s1.add(b)
    if b==N:
        sn.add(a)

print('POSSIBLE' if s1&sn else 'IMPOSSIBLE')

Submission status: Accepted

Solution code:
N,M=map(int,input().split())
A=[[] for _ in range(N+1)]
for _ in range(M):
    a,b=map(int,input().split())
    A[a].append(b)

ans=False
for i in A[1]:
    if i==N:
        ans=True
        break
    for j in A[i]:
        if j==N:
            ans=True
            break
print("POSSIBLE" if ans else "IMPOSSIBLE" )

Submission status: Accepted

Solution code:
N,M = map(int,input().rstrip().split(" "))
G = [[] for i in range(N + 1)]
ans = False
for i in range(M):
    a,b = map(int,input().rstrip().split(" "))
    G[a].append(b)
    G[b].append(a)
for i in G[1]:
    if (N in G[i]):
        ans = True
if ans:
    print("POSSIBLE")
else:
    print("IMPOSSIBLE")

Submission status: Accepted

Solution code:
N, M = map(int, input().split())
from_one = []
to_n = []
for _ in range(M):
  a, b = map(lambda x:int(x)-1, input().split())
  if a == 0:
    from_one += [b]
  elif b == 0:
    from_one += [a]
  elif a == N-1:
    to_n += [b]
  elif b == N-1:
    to_n += [a]

from_one = set(from_one)
to_n = set(to_n)
if len(from_one & to_n) >= 1:
  print("POSSIBLE")
else:
  print("IMPOSSIBLE")

Submission status: Accepted

Solution code:
N, M = map(int, input().split())
ship = [list(map(int, input().split())) for _ in range(M)]

s1 = [False] * N
sN = [False] * N

for i, s in enumerate(ship):
    if s[0] == 1:
        s1[s[1] - 1] = True

    if s[1] == N:
        sN[s[0] - 1] = True

ans = 'IMPOSSIBLE'
if any([p1 * pN for p1, pN in zip(s1, sN)]):
    ans = 'POSSIBLE'

print(ans)

Submission status: Accepted

Solution code:
import sys

n,m=map(int,input().split())
l=[[] for i in range(n)]

for i in range(m):
  a,b=map(int,input().split())
  l[a-1].append(b)
# print(l)


for i in l[0]:
  for j in l[i-1]:
    if j==n:
      print("POSSIBLE")
      sys.exit()

print("IMPOSSIBLE")

Submission status: Accepted

Solution code:
n,m=map(int,input().split())
A=[]
B=[]
for i in range(m):
  a,b=map(int,input().split())
  if a==1:
    A.append(b)
  if b==n:
    B.append(a)
if len(set(A) & set(B))>0:
  print("POSSIBLE")
else:
  print("IMPOSSIBLE")

Submission status: Accepted

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

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

travellist = sorted(travellist)


To_list = [0]*N
From_list = []
for i in range(M):

    oneroot = travellist[i]
    From = oneroot[0]
    To   = oneroot[1]

    if From == 1:
        To_list[To] = 1

    if To == N:
        From_list.append(From)


for i in From_list:
    if To_list[i] == 1:
        print("POSSIBLE")
        exit()

print("IMPOSSIBLE")

Submission status: Accepted

Solution code:
N, M = [int(x) for x in input().split(" ")]
next_to_1 = []
next_to_last = []

for i in range(M):
  tmp = input().split(" ")
  if int(tmp[0]) == 1:
    next_to_1.append(int(tmp[1]))
  if int(tmp[1]) == N:
    next_to_last.append(int(tmp[0]))

if len(list(set(next_to_1) & set(next_to_last))) > 0:
  print("POSSIBLE")
else:
  print("IMPOSSIBLE")

Submission status: Accepted

Solution code:
from collections import deque
n,m=map(int,input().split())
ab=[[] for _ in range(n+1)]
for i in range(m):
    a,b=map(int,input().split())
    ab[a].append(b)
    ab[b].append(a)
que=deque()
que.append(1)
count=[0]*2+[-1]*(n-1)
while que:
    x=que.pop()
    for j in ab[x]:
        if count[j]==-1:
            count[j]=count[x]+1
            que.appendleft(j)
if count[n]==2:
    print("POSSIBLE")
else:
    print("IMPOSSIBLE")

Submission status: Accepted

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


def read():
    N, M = map(int, input().strip().split())
    AB = []
    for i in range(M):
        a, b = map(int, input().strip().split())
        AB.append((a, b))
    return N, M, AB


def solve(N, M, AB):
    S = [False for i in range(N+1)]
    T = [False for i in range(N+1)]
    for a, b in AB:
        if a > b:
            a, b = b, a
        if a == 1:
            S[b] = True
        elif b == N:
            T[a] = True
    for s, t in zip(S, T):
        if s and t:
            return "POSSIBLE"
    return "IMPOSSIBLE"


if __name__ == '__main__':
    inputs = read()
    print("%s" % solve(*inputs))

Submission status: Accepted

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

for i in range(M):
    a, b = map(int, input().split())
    if(a == 1):
        start.add(b)
    elif(b == N):
        goal.add(a)
print("IMPOSSIBLE" if(len(start & goal) == 0) else "POSSIBLE")

Submission status: Accepted

Solution code:
N, M = map(int, input().split())
to = [[] for _ in range(N)]
ans = 'IMPOSSIBLE'

for _ in range(M):
    a, b = map(int, input().split())
    a, b = a - 1, b - 1
    to[a].append(b)
    to[b].append(a)
    
for nxt in to[0]:
    if N - 1 in to[nxt]:
        ans = 'POSSIBLE'
        break
        
print(ans)

Submission status: Accepted

Solution code:
from collections import deque 


N, M = map(int, input().split())
to = [[] for _ in range(N)]
for _ in range(M):
    a, b = map(int, input().split())
    a, b = a - 1, b - 1
    to[a].append(b)
    to[b].append(a)
    
que = deque()
que.append(0)
dist = [-1] * N
dist[0] = 0

while que:
    island = que.popleft()
    for nxt in to[island]:
        if dist[nxt] != -1:
            continue
        que.append(nxt)
        dist[nxt] = dist[island] + 1
        
print('POSSIBLE' if dist[N - 1] == 2 else 'IMPOSSIBLE')

Submission status: Accepted

Solution code:
import sys

input = sys.stdin.readline
n, m = map(int, input().split())
x = set()
y = set()
for _ in range(m):
    a, b = map(int, input().split())
    if a == 1:
        x.add(b)
    if b == n:
        y.add(a)
print(("POSSIBLE", "IMPOSSIBLE")[len(x & y) == 0])

Submission status: Accepted

Solution code:
n,m = map(int,input().split())
#a_to = []
#a_from = []
dic = {}
isOK = False
for i in range(m):
    a,b = map(int,input().split())
    if a in [1,n]:
        dic.setdefault(b,0)
        dic[b] += 1
        if dic[b]==2:
            isOK = True
            break
    elif b in [1,n]:
        dic.setdefault(a,0)
        dic[a] += 1
        if dic[a]==2:
            isOK = True
            break
#    if a==1:
#        a_to.append(b)
#    if b==1:
#        a_to.append(a)
#    if a==n:
#        a_from.append(b)
#    if b==n:
#        a_from.append(a)

#for j in range(len(a_to)):
#    if a_to[j] in a_from:
#        isOK = True
#        break
#print(a_to)
#print(a_from)
if isOK:
    print('POSSIBLE')
else:
    print('IMPOSSIBLE')

Submission status: Accepted

Solution code:
from collections import deque

n,m=map(int,input().split())
ab=[list(map(int,input().split())) for _ in range(m)]
g=[[] for _ in range(n+1)]

for a,b in ab:
    g[a].append(b)
    g[b].append(a)

q=deque([(1,0)])
visit=[0]*(n+1)

while len(q)>0:
    u,cnt=q.popleft()

    visit[u]=1

    if u==n and cnt==2:
        print('POSSIBLE')
        exit(0)
    
    for v in g[u]:
        if visit[v] or cnt==2:
            continue

        q.append((v,cnt+1))

print('IMPOSSIBLE')

Submission status: Accepted

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

Submission status: Accepted

Solution code:
n,m=map(int,input().split())
M=[[] for i in range(n)]
for i in range(m):
    a,b=map(int,input().split())
    a-=1;b-=1
    M[a].append(b)
    M[b].append(a)
yes="POSSIBLE";no="IMPOSSIBLE"

for i in M[0]:
    if n-1 in M[i]:
        print(yes);exit()
print(no)

Submission status: Accepted

Solution code:
L=list()
R=list()
N,M=map(int,input().split())
for i in range(M):
  a,b=map(int,input().split())
  if a==1:
    L.append(b)
  elif b==1:
    L.append(a)
  if a==N:
    R.append(b)
  elif b==N:
    R.append(a)
L=set(L)
R=set(R)
if len(L&R)==0:
  print("IMPOSSIBLE")
else:
  print("POSSIBLE")

Submission status: Accepted

Solution code:
n,m = map(int, input().split())
b1 = []
b2 = []
for i in range(m):
    a,b = map(int, input().split())
    if a == 1:
        b1.append(b)
    if b == n:
        b2.append(a)
if len(b1) + len(b2) == len(list(set(b1+b2))):
    print('IMPOSSIBLE')
else:
    print('POSSIBLE')

Submission status: Accepted

Solution code:
import math
import collections
import fractions
import itertools
import functools
import operator
import bisect

def solve():
    n, m = map(int, input().split())
    course = [[] for i in range(n)]
    for i in range(m):
        a, b = map(int, input().split())
        course[a-1].append(b)
        course[b-1].append(a)
    for i in course[0]:
        if n in course[i-1]:
            print("POSSIBLE")
            break
    else:
        print("IMPOSSIBLE")
    return 0

if __name__ == "__main__":
    solve()

Submission status: Accepted

Solution code:
n, m = list(map(int, input().split()))
s1 = {}
sn = []

for i in range(m):
    a, b = list(map(int, input().split()))
    if a == 1:
        s1[b] = 1
    elif b == n:
        sn.append(a)

res = False
for s in sn:
    if s in s1:
        res = True
        break

print("POSSIBLE" if res else "IMPOSSIBLE")

Submission status: Accepted

Solution code:
n, m = map(int, input().split())
ab  = list(list(map(int, input().split())) for _ in range(m))

goal = [0] * n
start = [0] * n

for a, b in ab:
    if a == 1:
        start[b] = 1
    if b == n:
        goal[a] = 1

flg = False
for i in range(n):
    if start[i] and goal[i]:
        flg = True

print('POSSIBLE' if flg else 'IMPOSSIBLE')

Submission status: Accepted

Solution code:
from collections import defaultdict
N, M = [int(x) for x in input().split()]
dict = defaultdict(bool)  # int/bool/list....
for i in range(M):
    a, b = [int(x) for x in input().split()]
    dict[(a, b)] = True

for j in range(1, N + 1):
    if dict[(1, j)] and dict[(j, N)]:
        print('POSSIBLE')
        break
else:
    print('IMPOSSIBLE')

Submission status: Accepted

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

for _ in range(m):
    a, b = map(int, input().split())
    a -= 1
    b -= 1
    edges[a].append(b)
    edges[b].append(a)
#print(edges)
d = deque()
d.append(0)
visited = [False]*n
visited[0] = True
from_0 = [0]*n
while d:
    x = d.popleft()
    for child in edges[x]:
        if visited[child]:
            continue
        else:
            visited[child] = True
            d.append(child)
            from_0[child] = from_0[x] + 1
#print(from_0)
if from_0[n-1] == 2:
    print("POSSIBLE")
else:
    print("IMPOSSIBLE")

Submission status: Accepted

Solution code:
N,M=map(int,input().split())
l,r=[0]*N,[0]*N
for i in range(M):
    a,b=map(int,input().split())
    if a==1:
        l[b-1]=1
    if b==N:
        r[a-1]=1
for i in range(N):
    if l[i]+r[i]==2:
        print('POSSIBLE')
        exit()
print('IMPOSSIBLE')

Submission status: Accepted

Solution code:
from collections import deque

INF=10**20

N,M=map(int,input().split())
G=[[] for _ in range(N+1)]
for i in range(M):
  a,b=map(int,input().split())
  G[a].append(b)
  G[b].append(a)

def bfs(s):
    seen = [0 for i in range(N+1)]
    d = [0 for i in range(N+1)]
    todo = deque()
    seen[s]=1
    todo.append(s)
    while 1:
      if len(todo)==0:break
      a = todo.popleft()
      if d[a] >= 2:continue
      for b in G[a]:
        if seen[b] == 0:
          seen[b] = 1
          todo.append(b)
          d[b] += d[a] + 1
    return d

d = bfs(1)
if d[N] != 0 and d[N] <= 2:print('POSSIBLE')
else:print('IMPOSSIBLE')

Submission status: Accepted

Solution code:
n, m = map(int,input().split())
l1 = set()
l2 = set()
for i in range(m):
    a,b = map(int,input().split())
    if a==1:
        l1.add(b)
    elif b==n:
        l2.add(a)
print('POSSIBLE' if len(l1&l2) >=1 else 'IMPOSSIBLE')

Submission status: Accepted

Solution code:
n, m = map(int,input().split())
 
l1 = []
l2 = []
for i in range(m):
    a,b = map(int,input().split())
    if a==1:
        l1.append(b)
    elif b==n:
        l2.append(a)

if len(set(l1)&set(l2)) >=1:
  print('POSSIBLE')
else:
  print('IMPOSSIBLE')

Submission status: Accepted

Solution code:
n,m=map(int,input().split())
ans1=[]
ans2=[]
for _ in range(m):
  a,b=map(int,input().split())
  if a==1:
    ans1.append(b)
  if b==n:
    ans2.append(a)
    
ans=set(ans1)&set(ans2)
if len(ans)>0:
  print('POSSIBLE')
else:
  print('IMPOSSIBLE')

Submission status: Accepted

Solution code:
n,m = map(int,input().split())
a = [] # 1 → x
z = [] # x → n
an = 0
zn = 0
for i in range(m):
    x,y = map(int,input().split())
    if x == 1:
        zn += 1
        z.append(y)
    elif y == n:
        an += 1
        a.append(x)
    else:
        pass
a.sort()
z.sort()
i = 0
j = 0
while i < an and j < zn:
    if a[i] == z[j]:
        print('POSSIBLE')
        exit(0)
    else:
        if a[i] > z[j]:
            j += 1
        else:
            i += 1
print('IMPOSSIBLE')

Submission status: Accepted

Solution code:
n,m = map(int, input().split())
ab = [[] for _ in range(n+1)]

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

for i in ab[1]:
  for j in ab[i]:
    if j == n:
      print("POSSIBLE")
      exit()
print("IMPOSSIBLE")

Submission status: Accepted

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

adj = [set() for i in range(N)]
for i in range(M):
    a, b = map(int, input().split())
    a -= 1
    b -= 1
    adj[a].add(b)
    adj[b].add(a)

if len(adj[0] & adj[N - 1]) > 0:
    print("POSSIBLE")
else:
    print("IMPOSSIBLE")

Submission status: Accepted

Solution code:
import sys
def input(): return sys.stdin.readline().strip()


def main():
    N, M = map(int, input().split())
    repn = [[] for _ in range(N)]
    for _ in range(M):
        a, b = map(int, input().split())
        repn[a - 1].append(b - 1)
        repn[b - 1].append(a - 1)
    visited = [0] * N

    def dfs(u, n):
        if n >= 2:
            visited[u] = 1
            return
        visited[u] = 1
        for v in repn[u]:
            if visited[v] != 0: continue
            dfs(v, n + 1)

    dfs(0, 0)
    if visited[N - 1] == 1:
        print("POSSIBLE")
    else:
        print("IMPOSSIBLE")

if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:
N,M=map(int,input().split())
c1=[]
c2=[]
for i in range(M):
    a,b=map(int,input().split())
    if a==1:
        c1.append(b)
    if b==N:
        c2.append(a)
c1=set(c1)
c2=set(c2)
if c1&c2:
    print('POSSIBLE')
else:
    print('IMPOSSIBLE')

Submission status: Accepted

Solution code:
def resolve():
    N, M = [int(i) for i in input().split()]
    aabb = [list(map(int, input().split())) for _ in range(M)]
    to_N, from_1 = set(), set()
    for a, b in aabb:
        if b == N:
            to_N.add(a)
        elif a == 1:
            from_1.add(b)
    if len(to_N & from_1) > 0:
        print("POSSIBLE")
    else:
        print("IMPOSSIBLE")


resolve()

Submission status: Accepted

Solution code:
n,m=map(int,input().split())
l=[list(map(int,input().split()))for _ in range(m)]
c=[]
d=[]
for a,b in l:
    if a==1:
        c.append(b)
    if b==n:
        d.append(a)
if len(set(c)&set(d))>0:
    print('POSSIBLE')
else:
    print('IMPOSSIBLE')

Submission status: Accepted

Solution code:
N, M = map(int, input().split())
route_1 = [0] * (N+1)
route_n = [0] * (N+1)

for i in range(M):
    a, b = map(int, input().split())
    if a == 1:
        route_1[b] = 1
    elif b == N:
        route_n[a] = 1

for i in range(2, N+1):
    if route_1[i] == 1 and route_n[i] == 1:
        print('POSSIBLE')
        exit()
print('IMPOSSIBLE')

Submission status: Accepted

Solution code:
#!/usr/bin/env python3

import sys
sys.setrecursionlimit(10**9)

# from numba import njit

# from collections import Counter
# from itertools import accumulate
# import numpy as np
# from heapq import heappop,heappush
# from bisect import bisect_left

# @njit
def solve(n,m,l):
  edges = {i+1:[] for i in range(n)}
  for i in range(m):
    a,b = l[i]
    edges[a].append(b)
    edges[b].append(a)
  return any(1 in v and n in v for k,v in edges.items())



def main():
  N,M = map(int,input().split())
  l = [tuple(map(int,input().split())) for _ in range(M)]
  print("POSSIBLE" if solve(N,M,l) else "IMPOSSIBLE")
  return

if __name__ == '__main__':
  main()

Submission status: Accepted

Solution code:
#!/usr/bin/env python3

import sys
sys.setrecursionlimit(10**9)

# from numba import njit

# from collections import Counter
# from itertools import accumulate
# import numpy as np
# from heapq import heappop,heappush
# from bisect import bisect_left

# @njit
def solve(n,m,l):
  edges = {i+1:[] for i in range(n)}
  for i in range(m):
    a,b = l[i]
    edges[a].append(b)
    edges[b].append(a)
  def dfs(start,end,rest):
    if rest == 0:
      return start == end
    else:
      return any(dfs(nextNode,end,rest-1) for nextNode in edges[start])

  return dfs(1,n,2)



def main():
  N,M = map(int,input().split())
  l = [tuple(map(int,input().split())) for _ in range(M)]
  print("POSSIBLE" if solve(N,M,l) else "IMPOSSIBLE")
  return

if __name__ == '__main__':
  main()

Submission status: Accepted

Solution code:
import collections
N,M = map(int,input().split())
a = [list(map(int,input().split())) for _ in range(M)]
a.sort()
B = collections.Counter(x[0] for x in a)[1]
C = set([])
for i in range(B):
    C.add(a[i][1])
for i in range(M):
    if a[i][0] in C and a[i][1] == N:
        print("POSSIBLE")
        exit()
print("IMPOSSIBLE")

Submission status: Accepted

Solution code:
r = [[] for _ in range(200001)] 
n, m = map(int, input().split())

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

k = [1]
flag = 0
for _ in range(2):
    if flag == 1:
        break
    for j in k:
        kk = []
        kk += r[j]
        if n in r[j]:
            flag = 1
            break
    k = kk[:]

if flag == 1:
    print("POSSIBLE")
else:
    print("IMPOSSIBLE")

Submission status: Accepted

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

G = {i: [] for i in range(1, N + 1)}

for i in range(M):
    a, b = map(int, input().split())
    if a == 1 or a == N or b == 1 or b == N:
        G[a].append(b)
        G[b].append(a)

res = False

for i in range(2, N):
    if 1 in G[i] and N in G[i]:
        res = True
        break


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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
N, M = map(int, input().split())
d = {i + 1: [] for i in range(N)}

for i in range(M):
    a, b = map(int, input().split())
    d[a].append(b)
    d[b].append(a)

ans = False
one_to_i = d[1]
for i in one_to_i:
    if N in d[i]:
        ans = True
        break

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

Submission status: Accepted

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

edges = {i : [] for i in range(1, N+1)}
visited = [False for _ in range(N+1)]

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

def dfs(n, count):
  # 定期便2つでは行けない
  if count == 3:
    return False
  if n == N:
    return True
  visited[n] = True
  
  for edge in edges[n]:
    if visited[edge]:
      continue
    if dfs(edge, count + 1):
      return True
  
  return False

print("POSSIBLE" if dfs(1, 0) else "IMPOSSIBLE")

Submission status: Accepted

Solution code:
from collections import deque
def BFS(N,M,List):
    Graph = [[] for T in range(N+1)]
    for NodeM in range(0,M):
        Graph[List[NodeM][0]].append(List[NodeM][1])
        Graph[List[NodeM][1]].append(List[NodeM][0])

    Distance = [-1]*(N+1)
    Distance[0] = 0
    Distance[1] = 0
    From = [0]*(N+1)

    Deque = deque()
    Deque.append(1)
    while Deque:
        V = Deque.popleft()
        for NodeV in Graph[V]:
            if Distance[NodeV]==-1:
                Distance[NodeV] = Distance[V]+1
                Deque.append(NodeV)
                From[NodeV] = V

    return Distance[1:],From[1:]
  
N,M = (int(X) for X in input().split())
List = [[] for T in range(0,M)]
for T in range(0,M):
    List[T] = [int(X) for X in input().split()]
Distance,From = BFS(N,M,List)
if Distance[-1]==2:
    print('POSSIBLE')
else:
    print('IMPOSSIBLE')

Submission status: Accepted

Solution code:
N, M = map(int, input().split())
From = set()
To = set()
for _ in range(M):
  a, b = map(int, input().split())
  if a == 1:
    From.add(b)
  if b == N:
    To.add(a)
if From.isdisjoint(To):
  print("IMPOSSIBLE")
else:
  print("POSSIBLE")

Submission status: Accepted

Solution code:
import sys
from collections import defaultdict
from heapq import heapify, heappush
n,m=map(int,input().split())
s=[tuple(map(int,input().split())) for _ in range(m)]
def hl():
  ret=[]
  heapify(ret)
  return ret
d=defaultdict(hl)
for x in s:
  heappush(d[x[0]],-x[1])
  heappush(d[x[1]],-x[0])
for e in d[1]:
  if d[-e][0]==-n:
    print('POSSIBLE')
    sys.exit()
print('IMPOSSIBLE')

Submission status: Accepted

Solution code:
import sys
from collections import deque

read = sys.stdin.read
readline = sys.stdin.readline
readlines = sys.stdin.readlines
sys.setrecursionlimit(10 ** 9)
INF = 1 << 60
MOD = 1000000007


def main():
    N, M, *AB = map(int, read().split())
    G = [[] for _ in range(N)]
    for a, b in zip(*[iter(AB)] * 2):
        G[a - 1].append(b - 1)
        G[b - 1].append(a - 1)

    dist = [-1] * N
    dist[0] = 0
    queue = deque([0])
    while queue:
        v = queue.popleft()
        for nv in G[v]:
            if dist[nv] == -1:
                dist[nv] = dist[v] + 1
                queue.append(nv)

    if dist[N - 1] in (1, 2):
        print('POSSIBLE')
    else:
        print('IMPOSSIBLE')

    return


if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
from collections import defaultdict
n,m=map(int,input().split())
ab=defaultdict(lambda: [])
for i in range(m):
  a,b=map(int,input().split())
  ab[a].append(b)
for aa in ab[1]:
  for bb in ab[aa]:
    if bb == n:
      print("POSSIBLE")
      exit()
print("IMPOSSIBLE")

Submission status: Accepted

Solution code:
n, m = list(map(int, input().split()))
ab = [list(map(int, input().split())) for _ in range(m)]
t = [[] for _ in range(n + 1)]
for i in ab:
    t[i[1]].append(i[0])
flag = False
for j in t[n]:
    if 1 in t[j]:
        flag = True
        break
print("POSSIBLE") if flag else print("IMPOSSIBLE")

Submission status: Accepted

Solution code:
n, m = input().split()
a, b = set(), set()
for _ in range(int(m)):
    r = set(input().split())
    if "1" in r:
        a |= r
    if n in r:
        b |= r
print("POSSIBLE" * len(a & b) or "IMPOSSIBLE")

Submission status: Accepted

Solution code:
#!/usr/bin/env python3
n, m = input().split()
a = set()
b = set()
for _ in range(int(m)):
    r = set(input().split())
    if "1" in r:
        a |= r
    if n in r:
        b |= r
print("POSSIBLE" if len(a & b) > 0 else "IMPOSSIBLE")

Submission status: Accepted

Solution code:
#!/usr/bin/env python3
from collections import deque

n, m = map(int, input().split())
graph = [deque([]) for _ in range(n + 1)]
for _ in range(m):
    a, b = map(int, input().split())
    graph[a].append(b)
    graph[b].append(a)
for i in graph[1]:
    if n in graph[i]:
        print("POSSIBLE")
        exit()
print("IMPOSSIBLE")

Submission status: Accepted

Solution code:
n, m = map(int, input().split())
l1 = []
ln = []
for _ in range(m):
    a, b = map(int, input().split())
    if a == 1:
        l1.append(b)
    elif b == 1:
        l1.append(a)
    elif a == n:
        ln.append(b)
    elif b == n:
        ln.append(a)
l1.sort()
ln.sort()
ans = False
for i in l1:
    low = 0
    high = len(ln) - 1
    while low <= high:
        mid = (low + high)//2
        if ln[mid] == i:
            ans = True
            break
        elif ln[mid] > i:
            high = mid - 1
        else:
            low = mid + 1
    if ans:
        break
if ans:
    print("POSSIBLE")
else:
    print("IMPOSSIBLE")

Submission status: Accepted

Solution code:
import numpy as np
n,m = map(int,input().split())
A1 = np.zeros(n,dtype=bool)
Ai = np.zeros(n,dtype=bool)
for i in range(m):
    a,b = map(int,input().split())
    if a == 1:
        A1[b-1] = True
    if b == n:
        Ai[a-1] = True
if np.any(Ai[A1]):
    print('POSSIBLE')
else:
    print('IMPOSSIBLE')

Submission status: Accepted

Solution code:
n, m = map(int, input().split())
d = {}
c = "IMPOSSIBLE"

for i in range(1,n):
    if i == 1:
        d[i] = []
    else:
        d[i] = 0

for _ in range(m):
    a, b = map(int, input().split())
    if a == 1:
        d[a].append(b)
    elif d[a] < b:
        d[a] = b

for k in d[1]:
    if d[k] == n:
        c = "POSSIBLE"

print(c)

Submission status: Accepted

Solution code:
N, M = map(int, input().split())
AB = [list(map(int, input().split())) for _ in range(M)]

lst1 = []
lstN = []

for i in range(M):
    if AB[i][0] == 1:
        lst1.append(AB[i][1])
    if AB[i][1] == N:
        lstN.append(AB[i][0])

if list(set(lst1) & set(lstN)) == []:
    print("IMPOSSIBLE")
else:
    print("POSSIBLE")

Submission status: Accepted

Solution code:
from sys import exit

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

cnt = [0] * n
for _ in range(m):
    a, b = map(int, input().split())
    if a == 1 or a == n:
        cnt[b-1] += 1
    elif b == 1 or b == n:
        cnt[a-1] += 1

for c in cnt:
    if c == 2:
        print('POSSIBLE')
        exit()

print('IMPOSSIBLE')

Submission status: Accepted

Solution code:
import sys
n,m = map(int,input().split())
a = [list(map(int,input().split())) for _ in range(m)]
start = a[0][0]
goal = n
tmp,ans = [],[]
for i in range(m):
	if a[i][1] == goal:
		tmp.append(a[i][0])
#print(tmp)
for j in range(m):
		if a[j][0] == start:
			ans.append(a[j][1])
if len(set(tmp)&set(ans)) > 0:
	print('POSSIBLE')
	sys.exit()
print('IMPOSSIBLE')

Submission status: Accepted

Solution code:
from queue import Queue
n, m = map(int, input().split())
adj = [[] for i in range(n+1)]
for i in range(m):
    a, b = map(int, input().split())
    adj[a].append(b)
    adj[b].append(a)
def bfs():
    queue = Queue()
    queue.put(1)
    distance = [10**6]*(n+1)
    distance[1] = 0
    while not queue.empty():
        u = queue.get()
        if u == n and distance[u] == 2:
            print ("POSSIBLE")
            return
        for a in adj[u]:
            if distance[u] + 1 <= 2 and distance[a] > distance[u] + 1:
                distance[a] = distance[u] + 1
                queue.put(a)
    print ("IMPOSSIBLE")
    return
bfs()

Submission status: Accepted

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

x=[]
y=[]

for i in lst:
  if i[0]==1:
    x+=[i[1]]
    
for i in lst:
  if i[1]==n:
    y+=[i[0]]
    
ans='POSSIBLE'

if len(list(set(x)))+len(list(set(y)))==len(list(set(x+y))):
  ans='IMPOSSIBLE'
    
print(ans)