喊山

做的第一个l3的题
思路不难 用BFS来搞 最外圈的就是答案
一层层来循环找

#include <iostream>
#include <queue>
#include <vector>
using namespace std;
const int MAX_N = 10005;
//存图
vector<vector<int>> x(MAX_N);
//存距离 并且做上标记
vector<int> dp(MAX_N);
int n, m, k;
int maxt;
int ans;
void BFS(int f)
{
    maxt = 0;
    ans = 0;
    queue<int> q;
    q.push(f);
    dp[f] = 1;
    while (!q.empty())
    {
        int mou = q.front();
        q.pop();
        //如果距离小于的话
        if (maxt < dp[mou])
        {
            //更新答案
            ans = mou;
            maxt = dp[mou];
        }
        //找最小的编号
        if (dp[mou] == maxt)
            ans = min(ans, mou);
        for (auto e : x[mou])
        {
            if (!dp[e])
            {
                dp[e] = dp[mou] + 1;
                q.push(e);
            }
        }
    }
}
int main()
{
    scanf("%d%d%d", &n, &m, &k);
    for (int i = 0; i < m; ++i)
    {
        int a, b;
        scanf("%d%d", &a, &b);
        x[a].push_back(b);
        x[b].push_back(a);
    }
    while (k--)
    {
        int p;
        scanf("%d", &p);
        if (x[p].size() == 0)
            printf("0\n");
        else
        {
            BFS(p);
            cout << ans << endl;
        }
        //重置为0
        fill(dp.begin(), dp.end(), 0);
    }
    return 0;
}
发布了106 篇原创文章 · 获赞 25 · 访问量 7213

猜你喜欢

转载自blog.csdn.net/weixin_45653525/article/details/104300848