#include <stdio.h>
#include <iostream>
using namespace std;
 
int map[401][401];
int N, relation, chk;
 
void floyd()
{
    for(int k = 1; k <= N; k++)
    {
        for(int i = 1; i <= N; i++)
        {
            for(int j = 1; j <= N; j++)
            {
                if(map[i][j] == 0)
                {
                    if(map[i][k] + map[k][j] == -2)
                    {
                        map[i][j] = -1;
                    }
                    else if(map[i][k] + map[k][j] == 2)
                    {
                        map[i][j] = 1;
                    }
                }
            }
        }
    }
}
 
int main(void)
{
//    freopen("B1613_input.txt", "r", stdin);
    
    scanf("%d %d"&N, &relation);
    
    for(int i = 1; i <= relation; i++)
    {
        int a, b;
        scanf("%d %d"&a, &b);
        
        map[a][b] = -1;
        map[b][a] = 1;
    }
    
    floyd();
    
    scanf("%d"&chk);
    
    for(int i = 1; i <= chk; i++)
    {
        int a, b;
        scanf("%d %d"&a, &b);
        
        printf("%d\n", map[a][b]);
    }
        
    return 0;
}
cs

+ Recent posts