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: Accepted)

#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cmath>
#include<vector>
using namespace std;

#define MAXN 1005
#define MAXM 200005

int n,m,u,v;
int id[MAXN][MAXN],d[MAXN][MAXN],d2[MAXN][MAXN],q[MAXN][2];
int ans[MAXM];
vector<int> a[MAXN];

void dfs(int x,int fa)
{
    d[fa][x]=1;
    for(int i=0;i<(int)a[x].size();i++)
        if(d[fa][a[x][i]]==0)
            dfs(a[x][i],fa);
}

void dfs1(int x,int f,int flag)
{
    q[x][flag]=f;
    for(int i=0;i<(int)a[x].size();i++)
        if(!q[a[x][i]][flag])
            dfs1(a[x][i],f,flag);
}

int main()
{
    scanf("%d%d",&n,&m);
    for(int i=1;i<=m;i++)
    {
        scanf("%d%d",&u,&v);
        id[u][v]=i;
        a[u].push_back(v);
    }
    for(int i=1;i<=n;i++)
        dfs(i,i);
    for(int i=1;i<=n;i++)
    {
        memset(q,0,sizeof q);
        q[i][0]=q[i][1]=-1;
        for(int j=0;j<(int)a[i].size();j++)
            if(!q[a[i][j]][0])
                dfs1(a[i][j],j+1,0);
        for(int j=a[i].size()-1;j>=0;j--)
            if(!q[a[i][j]][1])
                dfs1(a[i][j],j+1,1);
        for(int j=0;j<(int)a[i].size();j++)
            if(q[a[i][j]][0]!=j+1||q[a[i][j]][1]!=j+1)
                d2[i][a[i][j]]=1;
    }
    for(int i=1;i<=n;i++)
        for(int j=1;j<=n;j++)
            if(d[j][i]==d2[i][j]&&id[i][j])
                ans[id[i][j]]=1;
    for(int i=1;i<=m;i++)
    {
        if(ans[i]==1)
            printf("same\n");
        else
            printf("diff\n");
    }
}