66442023-12-14 18:24:10mmatedAdószedőcpp17Accepted 30/30211ms27624 KiB
#include <bits/stdc++.h>
using namespace std;
#define f(i,k,n) for(int i=k; i<n; i++)
#define ll long long
const int inf = INT_MAX;

void solve()
{
    int n,k,f;
    cin>>n>>k>>f;
    vector<vector<int>>g(n+1);
    set<pair<int,int>>c;
    int a,b;
    f(i,0,k)
    {
        cin>>a>>b;
        g[a].push_back(b);
        g[b].push_back(a);
    }
    queue<int> q;
    q.push(f);
    vector<int> d(n+1,inf);
    vector<bool> v(n+1,false);
    d[f]=0;
    v[f]=true;
    while(!q.empty())
    {
        int w=q.front();
        q.pop();
        for(auto u : g[w])
        {
            if(v[u]==false)
            {
                v[u]=true;
                d[u]=d[w]+1;
                c.insert(make_pair(u,w));
                q.push(u);
            }
            else if(d[u]==d[w]-1)   c.insert(make_pair(w,u));
        }
    }
    cout<<c.size()<<"\n";
    for(pair<int,int> p : c)    cout<<p.first<<" "<<p.second<<"\n";
}

int main()
{
    ios::sync_with_stdio(0);
    cin.tie(0);
    int t=1;
    //cin>>t;
    while(t--)  solve();
    return 0;
}
SubtaskSumTestVerdictTimeMemory
base30/30
1Accepted0/03ms1828 KiB
2Accepted0/0174ms21464 KiB
3Accepted1/13ms2128 KiB
4Accepted1/13ms2344 KiB
5Accepted1/13ms2556 KiB
6Accepted1/13ms2636 KiB
7Accepted1/13ms2852 KiB
8Accepted1/13ms3076 KiB
9Accepted2/24ms3516 KiB
10Accepted2/24ms3908 KiB
11Accepted2/24ms4248 KiB
12Accepted2/213ms5644 KiB
13Accepted2/229ms7832 KiB
14Accepted2/2127ms20176 KiB
15Accepted1/1163ms26008 KiB
16Accepted1/1137ms22188 KiB
17Accepted2/2211ms27616 KiB
18Accepted2/2189ms25552 KiB
19Accepted2/2182ms26668 KiB
20Accepted2/2195ms27076 KiB
21Accepted2/2184ms27624 KiB