Basic methods of the basic storage of graphs one or two-D array (Boolean two-dimensional array) basic storage of __ graphs

Source: Internet
Author: User

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 ****************************************************/

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.