Problem:
Score : 1400 points

Problem Statement

There are N turkeys.
We number them from 1 through N.

M men will visit here one by one.
The i-th man to visit will take the following action:

If both turkeys x_i and y_i are alive: selects one of them with equal probability, then eats it.

If either turkey x_i or y_i is alive (but not both): eats the alive one.

If neither turkey x_i nor y_i is alive: does nothing.

Find the number of pairs (i,\ j) (1 ≤ i < j ≤ N) such that the following condition is held:

The probability of both turkeys i and j being alive after all the men took actions, is greater than 0.

Constraints

2 ≤ N ≤ 400

1 ≤ M ≤ 10^5

1 ≤ x_i < y_i ≤ N

Input

Input is given from Standard Input in the following format:

N M
x_1 y_1
x_2 y_2
:
x_M y_M

Output

Print the number of pairs (i,\ j) (1 ≤ i < j ≤ N) such that the condition is held.

Sample Input 1

3 1
1 2

Sample Output 1

2

(i,\ j) = (1,\ 3), (2,\ 3) satisfy the condition.

Sample Input 2

4 3
1 2
3 4
2 3

Sample Output 2

1

(i,\ j) = (1,\ 4) satisfies the condition.
Both turkeys 1 and 4 are alive if:

The first man eats turkey 2.

The second man eats turkey 3.

The third man does nothing.

Sample Input 3

3 2
1 2
1 2

Sample Output 3

0

Sample Input 4

10 10
8 9
2 8
4 6
4 9
7 8
2 8
1 8
3 4
3 4
2 7

Sample Output 4

5

Solution in Rust (1.42.0) (judge verdict: Time Limit Exceeded)

use proconio::marker::Usize1;

// 愚直でO(N^2M)
// O(NM) くらいにしたい
// 後ろから見ていく
// (x, y) がやってきたとき
// dp[x] && dp[y] なら不可
// dp[x] || dp[y] なら
// [z, x] と[z, y] が[z, x, y] と同一視できる
// 情報をうまいことマージしていく？
// マージはいいが次の(x, y) がきたときに検索するのが辛い
// 検索しうるのは最初からx, yを含んでいたものだけなのでなんとかなるかも
// 同一視のためのマッピングが辛くないか
// bitset!

// ---------- begin BitSet ----------
#[derive(Clone, Eq, PartialEq, Ord, PartialOrd)]
struct BitSet {
    size: usize,
    a: Vec<usize>,
}

fn bit_size() -> usize {
    8 * std::mem::size_of::<usize>()
}

fn quot_rem(n: usize) -> (usize, usize) {
    let w = bit_size();
    (n / w, n % w)
}

#[allow(dead_code)]
impl BitSet {
    fn new(size: usize) -> Self {
        let w = bit_size();
        BitSet {
            size: size,
            a: vec![0; (size + w - 1) / w],
        }
    }
    fn set_at(&mut self, x: usize) {
        assert!(x < self.size);
        let (q, r) = quot_rem(x);
        self.a[q] |= 1 << r;
    }
    fn clear_at(&mut self, x: usize) {
        assert!(x < self.size);
        let (q, r) = quot_rem(x);
        self.a[q] &= !(1 << r);
    }
    fn get_at(&self, x: usize) -> bool {
        if x >= self.size {
            return false;
        }
        let (q, r) = quot_rem(x);
        (self.a[q] >> r) & 1 == 1
    }
    fn any(&self) -> bool {
        self.a.iter().any(|a| *a != 0)
    }
    fn fix(&mut self) {
        let (q, r) = quot_rem(self.size);
        if r != 0 {
            self.a[q] &= (1 << r) - 1;
        }
    }
    fn clear(&mut self) {
        let len = self.a.len();
        self.a.clear();
        self.a.resize(len, 0);
    }
    fn truncate(&mut self, len: usize) {
        if len >= self.size {
            return;
        }
        let w = bit_size();
        self.a.truncate((len + w - 1) / w);
        self.size = len;
        self.fix();
    }
    fn popcnt(&self) -> usize {
        self.a.iter().fold(0, |s, a| s + a.count_ones() as usize)
    }
    fn shift_left(&self, rhs: usize) -> Self {
        let (q, r) = quot_rem(rhs);
        let mut ans = BitSet::new(self.size + rhs);
        if r == 0 {
            for (x, y) in ans.a[q..].iter_mut().zip(self.a.iter()) {
                *x = *y;
            }
        } else {
            let w = bit_size();
            let mut prev = 0;
            for (x, y) in ans.a[q..].iter_mut().zip(self.a.iter()) {
                *x = (*y << r) | (prev >> (w - r));
                prev = *y;
            }
            *ans.a.last_mut().unwrap() |= prev >> (w - r);
        }
        ans.fix();
        ans
    }
    fn shift_right(&self, rhs: usize) -> Self {
        if rhs >= self.size {
            return BitSet::new(1);
        }
        let (q, r) = quot_rem(rhs);
        let mut ans = BitSet::new(self.size - rhs);
        if r == 0 {
            for (x, y) in ans.a.iter_mut().zip(self.a[q..].iter()) {
                *x = *y;
            }
        } else {
            let w = bit_size();
            let mut prev = 0;
            for (x, y) in ans.a.iter_mut().zip(self.a[q..].iter()).rev() {
                *x |= (*y >> r) | (prev << (w - r));
                prev = *y;
            }
        }
        ans.fix();
        ans
    }
    fn bitwise_or(&self, rhs: &Self) -> Self {
        let (x, y) = if self.size >= rhs.size {(self, rhs)} else {(rhs, self)};
        let mut a = x.a.clone();
        for (a, y) in a.iter_mut().zip(y.a.iter()) {
            *a |= *y;
        }
        BitSet {
            size: x.size,
            a: a,
        }
    }
    fn bitwise_and(&self, rhs: &Self) -> Self {
        let (x, y) = if self.size <= rhs.size {(self, rhs)} else {(rhs, self)};
        let mut a = x.a.clone();
        for (a, y) in a.iter_mut().zip(y.a.iter()) {
            *a &= *y;
        }
        BitSet {
            size: x.size,
            a: a,
        }
    }
    fn bitwise_xor(&self, rhs: &Self) -> Self {
        let (x, y) = if self.size >= rhs.size {(self, rhs)} else {(rhs, self)};
        let mut a = x.a.clone();
        for (a, y) in a.iter_mut().zip(y.a.iter()) {
            *a ^= *y;
        }
        BitSet {
            size: x.size,
            a: a,
        }
    }
    fn bitwise_or_assign(&mut self, rhs: &Self) {
        if self.size < rhs.size {
            self.size = rhs.size;
            self.a.resize(rhs.a.len(), 0);
        }
        for (a, b) in self.a.iter_mut().zip(rhs.a.iter()) {
            *a |= *b;
        }
    }
    fn bitwise_and_assign(&mut self, rhs: &Self) {
        if self.size > rhs.size {
            self.size = rhs.size;
            self.a.resize(rhs.a.len(), 0);
        }
        for (a, b) in self.a.iter_mut().zip(rhs.a.iter()) {
            *a &= *b;
        }
    }
    fn bitwise_xor_assign(&mut self, rhs: &Self) {
        if self.size < rhs.size {
            self.size = rhs.size;
            self.a.resize(rhs.a.len(), 0);
        }
        for (a, b) in self.a.iter_mut().zip(rhs.a.iter()) {
            *a ^= *b;
        }
    }
}
// ---------- end BitSet ----------
//---------- begin union_find ----------
#[allow(dead_code)]
mod union_find {
    pub struct UF {
        p: Vec<i32>,
    }
    impl UF {
        pub fn new(n: usize) -> UF {
            UF {p: vec![-1; n] }
        }
        pub fn init(&mut self) {
            for p in self.p.iter_mut() {
                *p = -1;
            }
        }
        pub fn root(&self, mut x: usize) -> usize {
            while self.p[x] >= 0 {
                x = self.p[x] as usize;
            }
            x
        }
        pub fn same(&self, x: usize, y: usize) -> bool {
            self.root(x) == self.root(y)
        }
        pub fn unite(&mut self, mut x: usize, mut y: usize) -> Option<(usize, usize)> {
            x = self.root(x);
            y = self.root(y);
            if x == y {
                return None;
            }
            if self.p[x] > self.p[y] {
                let s = x;
                x = y;
                y = s;
            }
            self.p[x] += self.p[y];
            self.p[y] = x as i32;
            Some((x, y))
        }
        pub fn parent(&self, x: usize) -> Option<usize> {
            let p = self.p[x];
            if p >= 0 {
                Some(p as usize)
            } else {
                None
            }
        }
        pub fn get_size(&self, x: usize) -> usize {
            let r = self.root(x);
            (-self.p[r]) as usize
        }
    }
}
//---------- end union_find ----------

fn run() {
    proconio::input! {
        n: usize,
        m: usize,
        p: [(Usize1, Usize1); m],
    }
    let mut id = vec![vec![std::usize::MAX; n]; n];
    let mut k = 0;
    for i in 0..n {
        for j in (i + 1)..n {
            id[i][j] = k;
            k += 1;
        }
    }
    let len = k;
    let mut u = union_find::UF::new(len);
    let mut dp = vec![BitSet::new(n); len];
    let mut cnt = vec![1u32; len];
    for i in 0..n {
        for j in (i + 1)..n {
            let x = id[i][j];
            dp[x].set_at(i);
            dp[x].set_at(j);
        }
    }
    for &(x, y) in p.iter().rev() {
        let mut used = std::collections::BTreeSet::new();
        let mut map = std::collections::BTreeMap::new();
        for i in 0..n {
            for &p in [x, y].iter() {
                if p == i {
                    continue;
                }
                let (p, q) = (std::cmp::min(p, i), std::cmp::max(p, i));
                let k = u.root(id[p][q]);
                if used.contains(&k) {
                    continue;
                }
                used.insert(k);
                if dp[k].get_at(x) && dp[k].get_at(y) {
                    cnt[k] = 0;
                }
                dp[k].set_at(x);
                dp[k].set_at(y);
                map.entry(dp[k].clone()).or_insert(vec![]).push(k);
            }
        }
        for (_, v) in map {
            for a in v.windows(2) {
                if let Some((a, b)) = u.unite(a[0], a[1]) {
                    cnt[a] += cnt[b];
                    cnt[b] = 0;
                }
            }
        }
    }
    let ans = cnt.into_iter().sum::<u32>();
    println!("{}", ans);
}

#[allow(dead_code)]
fn naive() {
    proconio::input! {
        n: usize,
        m: usize,
        p: [(Usize1, Usize1); m],
    }
    let mut ans = 0;
    for i in 1..n {
        for j in 0..i {
            let mut dp = vec![false; n];
            dp[i] = true;
            dp[j] = true;
            let mut ok = 1;
            for &(x, y) in p.iter().rev() {
                if dp[x] && dp[y] {
                    ok = 0;
                    break;
                }
                if dp[x] || dp[y] {
                    dp[x] = true;
                    dp[y] = true;
                }
            }
            ans += ok;
        }
    }
    println!("{}", ans);
}

fn main() {
//    run();
    naive();
}