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 C++14 (GCC 5.4.1) (judge verdict: Wrong Answer)

#include <iostream>
#include <cstdio>
#include <cstring>
#define N 1005
using namespace std;
typedef unsigned long long ull;
struct edge{
	int k,next;
}e[400005];
struct ed{
	int x,y;
}E[200005];
int n,m,home[N],cnt=-1,D,s[N],top,id[N],tot,dfn[N],mn[N],cc;
bool ins[N],vis[N][N],temp[N];
void add(int x,int y){
	cnt++;
	e[cnt].k=y;
	e[cnt].next=home[x];
	home[x]=cnt;
}
void tarjan(int k){
	dfn[k]=mn[k]=++cc;s[++top]=k;
	ins[k]=1;
	for(int i=home[k];~i;i=e[i].next){
		if(!dfn[e[i].k]){
			tarjan(e[i].k);
			mn[k]=min(mn[k],mn[e[i].k]);
		}
		else if(ins[e[i].k]) mn[k]=min(mn[k],mn[e[i].k]);
	}
	if(dfn[k]==mn[k]){
		id[k]=++tot;ins[k]=0;
		while(s[top]!=k){
			id[s[top]]=tot;ins[s[top]]=0;
			top--;
		}
		top--;
	}
}
void dfs(int k){
	if(k==D) return;
	vis[D][k]=1;
	for(int i=home[k];~i;i=e[i].next) if(!vis[D][e[i].k]){
		dfs(e[i].k);
	}
}
int main(){
	memset(home,-1,sizeof(home));
	scanf("%d%d",&n,&m);
	for(int i=1,x,y;i<=m;i++){
		scanf("%d%d",&x,&y);
		add(x,y);
		E[i].x=x;E[i].y=y;
	}
	for(int i=1;i<=n;i++) if(!dfn[i]) cc=0,tarjan(i);
	for(int i=1;i<=n;i++){
		D=i;top=0;
		for(int p=home[i];~p;p=e[p].next){
			int o=vis[i][e[p].k];
			s[++top]=e[p].k;
			dfs(e[p].k);
			vis[i][e[p].k]=o;
		}
		if(vis[i][s[1]]) continue;
		for(int p=1;p<=n;p++) temp[p]=vis[i][p];
		memset(vis[i],0,sizeof(vis[i]));
		bool t=0;
		for(int p=top;p>1;p--){
			dfs(s[p]);
			if(vis[i][s[1]]){
				t=1;break;
			}
		}
		for(int p=1;p<=n;p++) vis[i][p]=temp[p];
		if(t) vis[i][s[1]]=1;
	}
	for(int i=1;i<=m;i++){
		int x=E[i].x,y=E[i].y;
		if(id[x]==id[y]){
			if(!vis[x][y]) puts("diff");
			else puts("same");
		}else{
			if(vis[x][y]) puts("diff");
			else puts("same");
		}
	}
	return 0;
}