#include <string>
#include <vector>
#include <queue>
#include <iostream>
using namespace std;
 
vector<pair<intint>> map[20010];
int d[20010];
 
void dijkstra(int start)
{
    priority_queue<pair<intint>vector<pair<intint>>, greater<pair<intint>>> pq;
    pq.push({0, start});
    d[start] = 0;
 
    while(!pq.empty())
    {
        int now = pq.top().second;
        int nowCost = pq.top().first;
        pq.pop();
        
        if(d[now] != nowCost)
        {
            continue;
        }
        
        for(int i = 0; i < map[now].size(); i++)
        {
            int next = map[now][i].first;
            int nextCost = map[now][i].second;
            
            if(d[next] > nowCost + nextCost)
            {
                d[next] = nowCost + nextCost;
                pq.push({d[next], next});
            }
        }
    }
}
 
int solution(int n, vector<vector<int>> edge) 
{
    for(int i = 1; i <= n; i++)
    {
        d[i] = 99999999;
    }
    
    for(int i = 0; i < edge.size(); i++)
    {
        map[edge[i][0]].push_back({edge[i][1], 1});
        map[edge[i][1]].push_back({edge[i][0], 1});
    }
    
    dijkstra(1);
    
    int Max = 0;
    for(int i = 1; i <= n; i++)
    {
        if(Max < d[i])
        {
            Max = d[i];
        }
    }
    
    int cnt = 0;
    for(int i = 1; i <= n; i++)
    {
        if(Max == d[i])
        {
            cnt++;
        }
    }
   
    return cnt;
}
cs

+ Recent posts