25252023-01-16 09:52:34kicsiboglarAdószedőcpp11Accepted 30/30112ms29496 KiB
#include <iostream>
//#include <fstream>
#include <vector>
#include <deque>
#include <queue>
#include <climits>

#define ll long long 

using namespace std;
//ifstream cin ("input.in");
//ofstream cout ("output.out");

ll n,m,i,j,a,b,start;
struct element
{
    bool seen;
    ll step=LLONG_MAX,from=-1;
    vector <ll> sz;
};


vector <pair<ll,ll> > res;

struct road
{
    ll where,steps,from;
};

priority_queue<road> que;
bool operator< (const road& a, const road& b)
{
    return a.steps>b.steps;
}

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

    cin>>n>>m>>start;
    vector <element> x(n+1);
    deque<pair<ll,ll> >v;
    for (i=1;i<=m;++i)
    {
        cin>>a>>b;
        x[a].sz.push_back(b);
        x[b].sz.push_back(a);
    }

    x[start].step=0;
    que.push({start,0,0});
    //v.push_back(start);
    //x[start].seen=true;
    ll db=0;
    road curr;
    while (!que.empty())
    {
        curr=que.top();
        while (x[curr.where].seen&&!que.empty())
        {
            que.pop();
            if (!que.empty()) curr=que.top();
        }

        if (que.empty()) break;
       //db++;
        x[curr.where].seen=true;
        x[curr.where].step=curr.steps;
        x[curr.where].from=curr.from;
        //res.push_back({curr.where,curr.from});
        for (auto &e:x[curr.where].sz)
        {
            if (curr.steps+1<x[e].step)
            {
                x[e].step=curr.steps+1;
                que.push({e,curr.steps+1,curr.where});
            }
        }
    }

    v.push_back({start,0});
    pair<ll,ll> r;
    for (i=1;i<=n;++i) x[i].seen=false;
    x[start].seen=true;
    while (!v.empty())
    {
        r=v[0];
        v.pop_front();

        for (auto& e:x[r.first].sz)
        {
            if (x[e].step==r.second+1)
            {
                res.push_back({e,r.first});
                if (!x[e].seen)
                {
                    x[e].seen=true;
                    v.push_back({e,x[e].step});
                }
            }
        }
    }

    cout<<res.size()<<"\n";
    for (auto&e:res) cout<<e.first<<" "<<e.second<<"\n";
}
SubtaskSumTestVerdictTimeMemory
base30/30
1Accepted0/03ms2108 KiB
2Accepted0/086ms20616 KiB
3Accepted1/12ms2532 KiB
4Accepted1/12ms2604 KiB
5Accepted1/12ms2736 KiB
6Accepted1/12ms2792 KiB
7Accepted1/12ms2900 KiB
8Accepted1/12ms3132 KiB
9Accepted2/23ms3440 KiB
10Accepted2/23ms3572 KiB
11Accepted2/23ms3984 KiB
12Accepted2/28ms5192 KiB
13Accepted2/217ms7520 KiB
14Accepted2/272ms18560 KiB
15Accepted1/196ms29496 KiB
16Accepted1/182ms20744 KiB
17Accepted2/2112ms29108 KiB
18Accepted2/297ms26872 KiB
19Accepted2/2108ms27916 KiB
20Accepted2/2101ms28556 KiB
21Accepted2/2108ms29276 KiB