只是來炫耀分享兩次bfs的做法很讚而以
queue<int> bfs;
int size = 0;
bfs.push(0);
for(int x = 0; x < 2; x++)
{
vector<int> visited(n, -1);
visited[bfs.front()] = 0;
while(!bfs.empty())
{
auto now = bfs.front();
for(auto &next : graph[now])
{
if(visited[next] != -1)continue;
visited[next] = visited[now]+1;
bfs.push(next);
}
if(bfs.size() == 1)break;
bfs.pop();
}
size = max(size, visited[bfs.front()]);
}
其中 size 可以換成紀錄端點的 vector 之類的
總之讚讚
留下點回憶吧!