Untitled

mail@pastecode.io avatar
unknown
plain_text
2 years ago
812 B
0
Indexable
Never
#include <bits/stdc++.h>
using namespace std;
vector<int> G[1000];

int main() {
    int n;
    cin >> n;
    int edges;
    cin >> edges;
    while (edges--) {
        int x, y;
        cin >> x >> y;
        G[x].push_back(y);
        G[y].push_back(x);
    }
    int visited[n];
    for ( int i = 0; i < n; i++) {
        visited[i] = 0;
    }
    int d[n];
    int p[n];
    int s = 0;
    visited[s] = 1;
    d[0] = 0;
    p[0] = -1;
    queue<int> q;
    q.push(s);
    while (!q.empty()) {
        int v = q.front();
        q.pop();
        for (int u : G[v]) {
            if (!visited[u]) {
                visited[u] = 1;
                q.push(u);
                d[u] = d[v] + 1;
                p[u] = v;
            }
        }
    }
    for (int i = 0; i < n; i++) {
        cout << d[i];
    }
}