1 条题解

  • 0
    @ 2025-1-28 11:20:16

    输出从 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;
    }
    
    • 1

    信息

    ID
    74
    时间
    1000ms
    内存
    256MiB
    难度
    4
    标签
    递交数
    6
    已通过
    2
    上传者