#include <stdio.h>
#include <iostream>
#include <vector>
#include <queue>
#include <map> 
#include <math.h>
#include <string>
#include <string.h>
#include <stdlib.h>
using namespace std;
 
int N;
vector<pair<intint>> graph[100010];
int visited[100010];
int Max;
int MaxNode;
 
void BFS(int start)
{
    queue<int> q;
    q.push(start);
    visited[start] = 0;
    
    while(!q.empty())
    {
        start = q.front();
        q.pop();
        
        for(int i = 0; i < graph[start].size(); i++)
        {
            if(visited[graph[start][i].first] == -1)
            {
                visited[graph[start][i].first] = visited[start] + graph[start][i].second;
                q.push(graph[start][i].first);    
                
                if(Max < visited[graph[start][i].first])
                {
                    Max = visited[graph[start][i].first];
                    MaxNode = graph[start][i].first;
                }
            }
        }    
    }
}
 
int main(void)
{
//    freopen("B1167_input.txt", "r", stdin);
    
    cin >> N;
    
    for(int i = 1; i <= N; i++)
    {
        int from, to, weight;
        cin >> from;
        
        while(1)
        {
            cin >> to;
            
            if(to == -1)
            {
                break;
            } 
            else
            {
                cin >> weight;
            }
            
            graph[from].push_back({to, weight});
        }
    }
    
    // 루트에서 가장 먼 노드를 찾음 
    memset(visited, -1sizeof(visited));
    BFS(1);
    
    // 위에서 찾은 먼 노드에서 가장 먼 노드를 찾음 
    memset(visited, -1sizeof(visited));
    BFS(MaxNode);
    
    cout << Max;
    
    return 0;
}
cs

+ Recent posts