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.15.1) (judge verdict: Accepted)

use std::io::*;
use std::str::FromStr;

fn read<T: FromStr>() -> T {
    let stdin = stdin();
    let stdin = stdin.lock();
    let token: String = stdin
        .bytes()
        .map(|c| c.expect("failed to read char") as char)
        .skip_while(|c| c.is_whitespace())
        .take_while(|c| !c.is_whitespace())
        .collect();
    token.parse().ok().expect("failed to parse token")
}

type Weight = i32;
#[derive(Clone, Debug)]
struct Edge {
    index: usize,
    src: usize,
    dest: usize,
    weight: Weight,
}
impl Edge {
    fn new(index: usize, src: usize, dest: usize, weight: Weight) -> Edge {
        Edge {
            index: index,
            src: src,
            dest: dest,
            weight: weight,
        }
    }
}
type Edges = Vec<Edge>;
type Graph = Vec<Edges>;
fn add_uni_edge(graph: &mut Graph, index: &mut usize, src: usize, dest: usize, weight: Weight) {
    graph[src].push(Edge::new(*index, src, dest, weight));
    *index += 1;
}

fn dfs(g: &Graph, from: usize, visited: &mut Vec<Option<usize>>, start_index: usize) {
    if visited[from] != None { return; }
    visited[from] = Some(start_index);
    for edge in g[from].iter() {
        dfs(g, edge.dest, visited, start_index);
    }
}

fn main() {
    let n: usize = read();
    let m: usize = read();
    let mut g: Graph = vec![vec![]; n];
    let mut edges = vec![];
    let mut visitable = vec![vec![false; n]; n];
    let mut edge_delete_visitable = vec![true; m];
    let mut e = 0;
    for _ in 0..m {
        let mut f: usize = read();
        let mut t: usize = read();
        f -= 1; t -= 1;
        edges.push(Edge::new(e, f, t, 0));
        add_uni_edge(&mut g, &mut e, f, t, 0);
    }
    for from in 0..n {
        let mut min_value = vec![None; n];
        let mut max_value = vec![None; n];
        min_value[from] = Some(0);
        max_value[from] = Some(0);
        for edge in g[from].iter() {
            dfs(&g, edge.dest, &mut min_value, edge.index);
        }
        for edge in g[from].iter().rev() {
            dfs(&g, edge.dest, &mut max_value, edge.index);
        }
        for edge in g[from].iter() {
            edge_delete_visitable[edge.index] = min_value[edge.dest].unwrap() != max_value[edge.dest].unwrap();
        }
        for to in 0..n {
            visitable[from][to] = min_value[to] != None;
        }
    }
    for edge in edges {
        // println!("{} {}", visitable[edge.dest][edge.src], edge_delete_visitable[edge.index]);
        println!("{}", if visitable[edge.dest][edge.src] == edge_delete_visitable[edge.index] { "same" } else { "diff" });
    }
}