73812024-01-08 12:36:20EsVagyAdószedőcpp17Accepted 30/30105ms17204 KiB
#include <iostream>
#include <vector>
#include <string>
#include <algorithm>
#include <set>
#include <map>
#include <climits>
#include <queue>
#include <fstream>
#include <sstream>
#include <math.h>
#include <list>

using namespace std;

int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(nullptr);

    int n, m, s;
    cin >> n >> m >> s;
    s--;
    vector<vector<int>> g(n, vector<int>());
    vector<bool> visited(n, false);
    vector<int> dist(n, INT_MAX);
    for (int i = 0; i < m; i++)
    {
        int a, b;
        cin >> a >> b;
        a--; b--;
        g[a].push_back(b);
        g[b].push_back(a);
    }

    vector<pair<int, int>> roads;
    queue<int> q;
    q.push(s);
    visited[s] = true;
    dist[s] = 0;
    while (!q.empty())
    {
        int next = q.front();
        q.pop();
        for (int node : g[next])
        {
            if (!visited[node])
            {
                q.push(node);
                roads.push_back(pair<int, int>(next, node));
                visited[node] = true;
                dist[node] = dist[next] + 1;
            }
            else
            {
                if (dist[node] == dist[next] + 1)
                {
                    roads.push_back(pair<int, int>(next, node));
                }
            }
        }
    }

    cout << roads.size() << "\n";
    for (pair<int, int> p : roads)
    {
        cout << p.first + 1 << " " << p.second + 1 << "\n";
    }
}
SubtaskSumTestVerdictTimeMemory
base30/30
1Accepted0/03ms1828 KiB
2Accepted0/086ms11668 KiB
3Accepted1/13ms2244 KiB
4Accepted1/13ms2456 KiB
5Accepted1/13ms2672 KiB
6Accepted1/13ms2884 KiB
7Accepted1/13ms2976 KiB
8Accepted1/13ms3072 KiB
9Accepted2/23ms3400 KiB
10Accepted2/24ms3504 KiB
11Accepted2/24ms3632 KiB
12Accepted2/28ms4624 KiB
13Accepted2/218ms5948 KiB
14Accepted2/275ms11552 KiB
15Accepted1/197ms16888 KiB
16Accepted1/179ms13168 KiB
17Accepted2/2105ms17120 KiB
18Accepted2/297ms16184 KiB
19Accepted2/2103ms16824 KiB
20Accepted2/2101ms17180 KiB
21Accepted2/2101ms17204 KiB