Problem:
Score : 1100 points

Problem Statement

You are given a directed graph with N vertices and M edges.
The vertices are numbered 1, 2, ..., N, and the edges are numbered 1, 2, ..., M.
Edge i points from Vertex a_i to Vertex b_i.

For each edge, determine whether the reversion of that edge would change the number of the strongly connected components in the graph.

Here, the reversion of Edge i means deleting Edge i and then adding a new edge that points from Vertex b_i to Vertex a_i.

Constraints

2 \leq N \leq 1000

1 \leq M \leq 200,000

1 \leq a_i, b_i \leq N

a_i \neq b_i

If i \neq j, then a_i \neq a_j or b_i \neq 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

Print M lines. In the i-th line, if the reversion of Edge i would change the number of the strongly connected components in the graph, print diff; if it would not, print same.

Sample Input 1

3 3
1 2
1 3
2 3

Sample Output 1

same
diff
same

The number of the strongly connected components is 3 without reversion of edges, but it will become 1 if Edge 2 is reversed.

Sample Input 2

2 2
1 2
2 1

Sample Output 2

diff
diff

Reversion of an edge may result in multiple edges in the graph.

Sample Input 3

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

Sample Output 3

same
same
same
same
same
diff
diff
diff
diff

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

// ${url}
//
#![allow(unused_imports)]
use std::io::*;
use std::io::Write;
use std::fmt::*;
use std::str::*;
use std::cmp::*;
use std::collections::*;

// Input macros.
// Original by tanakh: https://qiita.com/tanakh/items/0ba42c7ca36cd29d0ac8
#[allow(unused_macros)]
macro_rules! input {
    (source = $s:expr, $($r:tt)*) => {
        let mut iter = $s.split_whitespace();
        input_inner!{iter, $($r)*}
    };
    ($($r:tt)*) => {
        let s = {
            use std::io::Read;
            let mut s = String::new();
            std::io::stdin().read_to_string(&mut s).unwrap();
            s
        };
        let mut iter = s.split_whitespace();
        input_inner!{iter, $($r)*}
    };
}

#[allow(unused_macros)]
macro_rules! input_inner {
    ($iter:expr) => {};
    ($iter:expr, ) => {};

    ($iter:expr, $var:ident : $t:tt $($r:tt)*) => {
        let $var = read_value!($iter, $t);
        input_inner!{$iter $($r)*}
    };

    ($iter:expr, mut $var:ident : $t:tt $($r:tt)*) => {
        let mut $var = read_value!($iter, $t);
        input_inner!{$iter $($r)*}
    };
}

#[allow(unused_macros)]
macro_rules! read_value {
    ($iter:expr, ( $($t:tt),* )) => {
        ( $(read_value!($iter, $t)),* )
    };

    ($iter:expr, [ $t:tt ; $len:expr ]) => {
        (0..$len).map(|_| read_value!($iter, $t)).collect::<Vec<_>>()
    };

    ($iter:expr, [ next / $t:tt ]) => {
        {
            let len = read_value!($iter, usize);
            (0..len).map(|_| read_value!($iter, $t)).collect::<Vec<_>>()
        }
    };

    ($iter:expr, switch) => {
        {
            let ty = read_value!($iter, i32);
            if ty == 1 {
                vec![ty, read_value!($iter, i32), read_value!($iter, i32)]
            } else if ty == 2 {
                vec![ty, read_value!($iter, i32)]
            } else {
                vec![ty, read_value!($iter, i32)]
            }
        }
    };

    ($iter:expr, chars) => {
        read_value!($iter, String).chars().collect::<Vec<char>>()
    };

    ($iter:expr, usize1) => {
        read_value!($iter, usize) - 1
    };

    ($iter:expr, $t:ty) => {
        $iter.next().unwrap().parse::<$t>().expect("Parse error")
    };
}

#[allow(unused_macros)]
macro_rules! read_line {
    ($t:tt) => {
        {
            let mut s = String::new();
            std::io::stdin().read_line(&mut s).unwrap();
            s.trim_right().parse::<$t>().unwrap()
        }
    }
}

#[allow(unused_macros)]
macro_rules! dvec {
    ($t:expr ; $len:expr) => {
        vec![$t; $len]
    };

    ($t:expr ; $len:expr, $($rest:expr),*) => {
        vec![dvec!($t; $($rest),*); $len]
    };
}

#[allow(unused_macros)]
macro_rules! ifv {
    ($t:expr, $a:expr, $b: expr) => {
        if $t { $a } else { $b }
    }
}

#[allow(unused_macros)]
macro_rules! fill {
    ($t:expr, $v:expr) => {
        for i in 0..$t.len() {
            $t[i] = $v;
        }
    };
}

#[allow(unused_macros)]
macro_rules! join {
    ($t:expr, $glue:expr) => {
        $t.into_iter().map(|w| w.to_string()).collect::<Vec<_>>().join($glue)
    };
}

#[allow(unused_macros)]
macro_rules! debug {
    ($($a:expr),*) => {
        eprintln!(concat!($(stringify!($a), " = {:?}, "),*), $($a),*);
    }
}


// ===

type DirectedGraph = Vec<Vec<usize>>;

struct StronglyConnectedComponent {
    n: usize,
    graph: DirectedGraph,
    reversed_graph: DirectedGraph,
    component_ids: Vec<usize>
}

impl StronglyConnectedComponent {
    fn new(g: DirectedGraph) -> Self {
        let n = g.len();
        let mut rg = vec![vec![]; n];
        for i in 0..n {
            for &to in &g[i] {
                rg[to].push(i);
            }
        }
        Self {
            n: g.len(),
            graph: g,
            reversed_graph: rg,
            component_ids: vec![]
        }
    }

    fn decompose(&mut self) -> usize {
        let mut visited = vec![false; self.n];
        let mut rev_order = vec![];
        for i in 0..self.n {
            if !visited[i] {
                self.dfs(i, &mut visited, &mut rev_order);
            }
        }

        let mut visited = vec![false; self.n];
        let mut id = 0;
        let mut component_ids = vec![0; self.n];
        for &r in rev_order.iter().rev() {
            if !visited[r] {
                self.rdfs(r, id, &mut visited, &mut component_ids);
                id += 1;
            }
        }
        self.component_ids = component_ids;
        id
    }

    fn dfs(&self, now: usize, visited: &mut Vec<bool>, rev_order: &mut Vec<usize>) {
        visited[now] = true;
        for &to in &self.graph[now] {
            if !visited[to] {
                self.dfs(to, visited, rev_order);
            }
        }
        rev_order.push(now);
    }

    fn rdfs(&self, now: usize, id: usize, visited: &mut Vec<bool>, component_ids: &mut Vec<usize>) {
        visited[now] = true;
        component_ids[now] = id;
        for &to in &self.reversed_graph[now] {
            if !visited[to] {
                self.rdfs(to, id, visited, component_ids);
            }
        }
    }
}

fn topological_sort(g: Vec<Vec<usize>>) -> i32 {
    let n = g.len();
    let mut indeg = vec![0; n];
    for i in 0..n {
        for &j in &g[i] {
            indeg[j] += 1;
        }
    }
    let mut que = VecDeque::new();
    let mut dp = vec![-1i32; n];
    let mut visited = vec![false; n];
    for i in 0..n {
        if indeg[i] == 0 {
            que.push_back(i);
            dp[i] = 0;
            visited[i] = true;
        }
    }
    while let Some(v) = que.pop_front() {
        let now = dp[v];
        for &j in &g[v] {
            dp[j] = max(dp[j], now+1);
            indeg[j] -= 1;
            if indeg[j] == 0 {
                que.push_back(j);
                visited[j] = true;
            }
        }
    }

    let mut best = 0;
    for i in 0..n {
        if !visited[i] {
            return -1;
        }
        best = max(best, dp[i]);
    }
    best+1
}

fn main() {
    input! {
        n: usize, m: usize,
        edges: [(usize1, usize1); m]
    };

    let mut g = vec![vec![]; n];
    for &e in &edges {
        g[e.0].push(e.1);
    }

    let mut scc = StronglyConnectedComponent::new(g);
    let gn = scc.decompose();
    let mut gg = vec![vec![]; gn];
    let mut cango = dvec![false; gn, gn];
    for i in 0..gn {
        cango[i][i] = true;
    }
    for &e in &edges {
        let f = scc.component_ids[e.0];
        let t = scc.component_ids[e.1];
        if f != t {
            if !cango[f][t] {
                cango[f][t] = true;
                gg[f].push(t);
            }
        }
    }

    for i in 0..gn {
        dfsg(i, i, &mut cango, &gg);
    }

    let mut flag = vec![0; m];
    for (&e, idx) in edges.iter().zip(0..m) {
        let f = scc.component_ids[e.0];
        let t = scc.component_ids[e.1];
        flag[idx] |= ifv!(cango[t][f], 1, 0);
    }

    let mut g = vec![vec![]; n];
    for (&e, idx) in edges.iter().zip(0..m) {
        g[e.0].push((e.1, idx));
    }

    for _w in 0..2 {
        let mut visited = dvec![n; n];
        for i in 0..n {
            visited[i] = i;
            for &(to, idx) in &g[i] {
                if visited[to] == i {
                    flag[idx] |= 2;
                } else {
                    visited[to] = i;
                    dfs(to, i, &mut visited, &g);
                }
            }
        }
        for i in 0..n {
            g[i].reverse();
        }
    }

    for i in 0..m {
        if flag[i] == 1 || flag[i] == 2 {
            println!("diff");
        } else {
            println!("same");
        }
    }
}

fn dfsg(now: usize, root: usize, gg: &mut Vec<Vec<bool>>, graph: &Vec<Vec<usize>>) {
    for &to in &graph[now] {
        if !gg[root][to] {
            gg[root][to] = true;
            dfsg(to, root, gg, graph);
        }
    }
}

fn dfs(now: usize, root: usize, visited: &mut Vec<usize>, graph: &Vec<Vec<(usize, usize)>>) {
    for &to in &graph[now] {
        if visited[to.0] != root {
            visited[to.0] = root;
            dfs(to.0, root, visited, graph);
        }
    }
}