输出从 ss 开始的 BFS 轮数即可。

#include <bits/stdc++.h>
using namespace std;
struct F
{
	int to[200012], ns[200012], fs[100012];
	int m = 0;
	void add(int f, int t)
	{
		m++;
		to[m] = t;
		ns[m] = fs[f];
		fs[f] = m;
	}
};
F f;
bool ok[100012];
int r = 0;
queue<int> q;
int main()
{
    int n, m, s;
    cin >> n >> m >> s;
    while (m--)
    {
        int x, y;
        cin >> x >> y;
        f.add(x, y);
        f.add(y, x);
    }
    q.push(s);
    ok[s] = true;
    while (!q.empty())
    {
        r++;
        int h = q.size();
        while (h--)
        {
            int u = q.front();
            q.pop();
            int ii = f.fs[u];
            while (ii)
            {
                if (!ok[f.to[ii]])
                {
                    ok[f.to[ii]] = true;
                    q.push(f.to[ii]);
                }
                ii = f.ns[ii];
            }
        }
    }
    cout << r << endl;
    return 0;
}