73632024-01-08 11:03:01CWMAdószedőcpp17Accepted 30/30208ms17984 KiB
#include <iostream>
#include <vector>
#include <set>
#include <queue>

using namespace std;
int main()
{
    int v, e, c;
    cin >> v >> e >> c;
    vector<vector<int>> g(v);
    for (size_t i = 0; i < e; i++)
    {
        int a, b;
        cin >> a >> b;
        a--; b--;
        g[a].push_back(b);
        g[b].push_back(a);
    }
    c--;
    vector<int> shortestPath(v);
    queue<pair<int,int>> BFS;
    BFS.push({ c,0 });
    while (!BFS.empty())
    {
        pair<int, int> top = BFS.front();
        BFS.pop();
        for (size_t i = 0; i < g[top.first].size(); i++)
        {
            if (shortestPath[g[top.first][i]] == 0) {
                shortestPath[g[top.first][i]] = top.second + 1;
                BFS.push({ g[top.first][i], top.second + 1 });
            }
        }
    }
    vector<pair<int,int>> necessaryRoads;
    for (size_t i = 0; i < g.size(); i++)
    {
        for (size_t j = 0; j < g[i].size(); j++)
        {
            if (shortestPath[i] - shortestPath[g[i][j]] == 1) {
                necessaryRoads.push_back({ i,g[i][j] });
            }
        }
    }
    cout << necessaryRoads.size() << "\n";
    for (size_t i = 0; i < necessaryRoads.size(); i++)
    {
        cout << necessaryRoads[i].first+1 << " " << necessaryRoads[i].second+1 << "\n";
    }
}
SubtaskSumTestVerdictTimeMemory
base30/30
1Accepted0/03ms1812 KiB
2Accepted0/0173ms12044 KiB
3Accepted1/13ms2528 KiB
4Accepted1/13ms2752 KiB
5Accepted1/13ms2972 KiB
6Accepted1/13ms2972 KiB
7Accepted1/13ms2972 KiB
8Accepted1/13ms3104 KiB
9Accepted2/24ms3532 KiB
10Accepted2/24ms3520 KiB
11Accepted2/24ms3540 KiB
12Accepted2/216ms4860 KiB
13Accepted2/235ms6016 KiB
14Accepted2/2143ms11768 KiB
15Accepted1/1187ms17132 KiB
16Accepted1/1159ms13276 KiB
17Accepted2/2204ms17520 KiB
18Accepted2/2189ms16628 KiB
19Accepted2/2200ms17176 KiB
20Accepted2/2195ms17800 KiB
21Accepted2/2208ms17984 KiB