1908 problem B connected graph

Problem B: connected graph
Time limit: 1 Sec memory limit: 32 MB
Flower offering: 45 solution: 26
[flower offering] [wreath] [TK question bank]
Title Description
Given an undirected graph and all its edges, we can judge whether all vertices of the graph are connected.
input
The first row of each set of data is two integers n and m (0 < = n < = 1000). N represents the number of vertices in a graph, and M represents the number of edges in a graph. If n is 0, the input ends. Then there are m rows of data, each with two values x and y (0

#define _CRT_SECURE_NO_WARNINGS
#include <iostream>
#include <cstring>

using namespace std;
const int MaxN = 1002;
bool map[MaxN][MaxN];
bool visited[MaxN];
int n;

void DFS(int id)
{
    visited[id] = true;
    for (int i = 1; i <= n; ++i)
    {
        if (map[id][i] == true && visited[i] == false)
            DFS(i);
    }
}



int main()
{
#ifdef _DEBUG
    freopen("data.txt", "r+", stdin);
#endif // _DEBUG

    int k;
    int a, b, num;
    while (cin >> n >> k, n)
    {
        memset(map, 0, (n + 1) * MaxN);
        memset(visited, 0, n + 1);
        for (int i = 0; i < k; ++i)
        {
            cin >> a >> b;
            map[a][b] = map[b][a] = true;
            num = a;
        }

        DFS(num);

        bool flag = true;
        for (int i = 1; i <= n; ++i)
        {
            if (visited[i] == false)
            {
                flag = false;
                break;
            }
        }

        flag ? (cout << "YES\n") : (cout << "NO\n");

    }

    return 0;
}
/**************************************************************
    Problem: 1908
    User: Sharwen
    Language: C++
    Result: Immortals
    Time:2 ms
    Memory:2684 kb
****************************************************************/

Posted by kml2katz on Wed, 01 Apr 2020 06:13:00 -0700