66422023-12-14 18:07:25mmatedAdószedőcpp17Runtime error 25/30231ms27424 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,inf);
    vector<bool> v(n,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;
                a=min(u,w);
                b=max(u,w);
                c.insert(make_pair(a,b));
                q.push(u);
            }
            else if(abs(d[u]-d[w])>=1)
            {
                a=min(u,w);
                b=max(u,w);
                c.insert(make_pair(a,b));
            }

        }
    }
    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
base25/30
1Accepted0/03ms1828 KiB
2Accepted0/0173ms21524 KiB
3Accepted1/13ms2144 KiB
4Accepted1/13ms2228 KiB
5Accepted1/13ms2352 KiB
6Accepted1/13ms2576 KiB
7Runtime error0/13ms2744 KiB
8Accepted1/13ms2888 KiB
9Accepted2/24ms3096 KiB
10Accepted2/24ms3272 KiB
11Accepted2/24ms3492 KiB
12Accepted2/214ms4780 KiB
13Accepted2/234ms7272 KiB
14Wrong answer0/2143ms19672 KiB
15Accepted1/1190ms25420 KiB
16Accepted1/1165ms21624 KiB
17Accepted2/2231ms27068 KiB
18Accepted2/2201ms25228 KiB
19Wrong answer0/2214ms26376 KiB
20Accepted2/2203ms26888 KiB
21Accepted2/2206ms27424 KiB