The basic embodiment of FIG stored in a substantially - A

Description

Graph theory to solve the problem, we must first think about what kind of a stored map. But small Xin but how could not understand how the memory map can help solve the problem. Can you help him solve this problem?
Input

Multiple sets of input to the end of the file.

The first line of each group of two numbers n, m denotes n points, m article directed edge. Then there are m rows of two numbers u, v representative of u to v there is a directed edge. M + 2 second row has a number q represents the number of interrogation, then q lines each have a query, input two atoms a, b.

Note: point number is 0 ~ n-1,2 <= n <= 5000, n * (n-1) / 2 <= m <= n * (n-1), 0 <= q <= 1000000, a! = b, and the input to ensure that no self-loop edge weight
Output

For each inquiry, output line. If a to b can communicate directly output Yes, otherwise, outputs No.
Sample

Input

2 1
0 1
2
0 1
1 0
Output

Yes
No

#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<malloc.h>
bool a[5000][5000];//用int型会超时
int main()
{
    int n,m,u,v,q,x,y;
    while(~scanf("%d %d",&n,&m))
    {
        memset(a,0,sizeof(a));
        for(int i=0;i<m;i++)
        {
            scanf("%d %d",&u,&v);
            a[u][v] = 1;
        }
        scanf("%d",&q);
        while(q--)
        {
            scanf("%d %d",&x,&y);
            if(a[x][y] == 1)
                printf("Yes\n");
            else
                printf("No\n");
        }

    }
    return 0;
}

Published 177 original articles · won praise 7 · views 30000 +

Guess you like

Origin blog.csdn.net/Fusheng_Yizhao/article/details/104874378