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.

Solution in Rust (1.15.1) (judge verdict: Accepted)

#[allow(unused_macros)]
macro_rules! parse_line {
    ( $t:ty ) => (
        {
            let mut line = String::new();
            ::std::io::stdin().read_line(&mut line).unwrap();
            let mut iter = line.split_whitespace();
            iter.next().unwrap().parse::<$t>().unwrap()
        }
    );

    ( $( $t:ty), +) => (
        {
            let mut line = String::new();
            ::std::io::stdin().read_line(&mut line).unwrap();
            let mut iter = line.split_whitespace();
            ( $(iter.next().unwrap().parse::<$t>().unwrap()),* )
        }
    );
}

#[allow(unused_macros)]
macro_rules! read_line {
    () => (
        {
            let mut line = String::new();
            ::std::io::stdin().read_line(&mut line).unwrap();
            line.pop();
            line
        }
    )
}

#[allow(unused_macros)]
macro_rules! parse_vec {
    ( $t:ty ) => (
        {
            let mut line = String::new();
            ::std::io::stdin().read_line(&mut line).unwrap();
            let iter = line.split_whitespace();
            iter.map(|v| v.parse::<$t>().unwrap()).collect::<Vec<_>>()
        }
    )
}

mod dijkstra {
    use std::cmp::Ordering;
    use std::collections::BinaryHeap;

    #[derive(Copy, Clone, Eq, PartialEq)]
    pub struct State<T>
    {
        pub cost: usize,
        pub pos: T
    }

    pub trait ContextTrait<T>
    {
        fn cost(&self, pos: T) -> usize;
        fn update_cost(&mut self, s: &State<T>);
        fn neighbours(&self, s: &State<T>) -> Vec<State<T>>;

        fn run(&mut self, start: State<T>, end: T) -> Option<usize>
            where T: Copy+Ord {
            let mut heap = BinaryHeap::new();
            heap.push(start);

            while let Some(State {cost, pos}) = heap.pop() {
                if pos == end {
                    return Some(cost);
                }

                if cost > self.cost(pos) { continue; }

                for next in self.neighbours(&State {cost: cost, pos: pos}) {
                    if next.cost < self.cost(next.pos) {
                        self.update_cost(&next);
                        heap.push(next);
                    }
                }
            }
            None
        }
    }

    impl<T> Ord for State<T>
        where T: Copy+Ord {
        fn cmp(&self, other: &State<T>) -> Ordering {
            let c = other.cost.cmp(&self.cost);

            if c == Ordering::Equal {
                self.pos.cmp(&other.pos)
            } else {
                c
            }
        }
    }

    impl<T: Copy+Ord> PartialOrd for State<T> {
        fn partial_cmp(&self, other: &State<T>) -> Option<Ordering> {
            Some(self.cmp(other))
        }
    }
}

struct Context {
    costs: Vec<usize>,
    neighbours: Vec<Vec<usize>>
}

impl ContextTrait<usize> for Context {
    fn cost(&self, pos: usize) -> usize {
        self.costs[pos]
    }

    fn update_cost(&mut self, st: &State<usize>) {
        self.costs[st.pos] = st.cost;
    }

    fn neighbours(&self, s: &State<usize>) -> Vec<State<usize>> {
        let c = self.costs[s.pos];
        self.neighbours[s.pos].iter().map(|&p| State{cost: s.cost+1, pos: p}).collect()
    }
}

use dijkstra::*;

fn solve(n: usize, abs: Vec<(usize, usize)>) -> bool {
    let mut neighbours = vec![vec![]; n+1];

    for (a,b) in abs {
        neighbours[a].push(b);
        neighbours[b].push(a);
    }

    let mut context = Context{costs: vec![std::usize::MAX; n+1], neighbours: neighbours};

    match context.run(State::<usize>{cost: 0, pos:1}, n) {
        Some(c) => {
            c <= 2
        },
        None => {
            false
        }
    }
}

fn main() {
    let (n, m) = parse_line!(usize, usize);

    let abs = (0..m).map(|_| parse_line!(usize, usize)).collect();

    println!("{}", if solve(n, abs) { "POSSIBLE" } else { "IMPOSSIBLE" });
}

#[test]
fn solve_test() {
    assert_eq!(solve(3, vec![(1,2), (2,3)]), true);
    assert_eq!(solve(4, vec![(1,2), (2,3), (3,4)]), false);
    assert_eq!(solve(100000, vec![(1,99999)]), false);
    assert_eq!(solve(5, vec![(1,3), (4,5), (2,3), (2,4), (1,4)]), true);
}