Think
1 Boolean two-dimensional array storage
Sdut Original title Link
The basic way to store a diagram
Time limit:1500ms Memory limit:65536kb
Problem Description
To solve the problem of graph theory, we should first think about the way to store the graph. But Xiao Xin but how also do not understand how to save a map to help solve the problem. Can you help him solve the problem?
Input
Multiple sets of inputs to the end of the file.
The first row of each group has two numbers n, m for n points, and the M bar has a direction edge. Next there are m lines, two numbers per line u, V for u to v there is a direction side. M+2 Line has a number Q represents the number of inquiries, the next Q row each row has a query, enter two numbers for a,b.
Note: The number of points is 0~n-1,2<=n<=5000, n (n-1)/2<=m<=n* (n-1), 0<=q<=1000000,a!=b, input guarantees no self loop and heavy edge
Output
For each query, output one line. If A to B can be directly connected to the output Yes, otherwise output No.
Example Input
2 1
0 1
2
0 1
1 0
Example Output
Yes
No
Hint
Author
Lin
The following is submitted by accepted code--g++
#include <stdio.h>
#include <string.h>
#include <stdbool.h>
bool a[5000][5000];
int main ()
{
int n, m, I, Q, u, v;
while (scanf ("%d%d", &n, &m)!= EOF)
{
memset (a, 0, sizeof (a));
for (i = 0; i < m i++)
{
scanf ("%d%d", &u, &v);
A[U][V] = 1;
}
scanf ("%d", &q);
while (q--)
{
scanf ("%d%d", &u, &v);
if (a[u][v] = = 1)
printf ("yes\n");
else
printf ("no\n");
}
return 0;
}
/***************************************************
User name:jk160630
result:accepted
Take time : 1400ms
Take memory:2032kb
Submit time:2017-02-14 09:57:26
*************************************** *************/
The following is the accepted recommended reference code
#include <stdio.h> #include <string.h> #include <stdbool.h> bool a[5004][
5004];
int main () {int n, m, U, V, Q;
while (scanf ("%d%d", &n, &m)!= EOF) {memset (A, 0, sizeof (a));
while (m--) {scanf ("%d%d", &u, &v);
A[U][V] = 1;
} scanf ("%d", &q);
while (q--) {scanf ("%d%d", &u, &v);
if (A[u][v]) printf ("yes\n");
else printf ("no\n");
} return 0; }/*************************************************** User name:jk160630 result:accepted Take time:1340ms Take Memor y:2076kb Submit time:2017-02-14 19:43:47 ****************************************************/