// https://www.codingame.com/ide/puzzle/teads-sponsored-contest // Xavier Morel - 2016-03-17 #include #include #include #include #include #include #include using namespace std; map> points; int findFarthestNode(int origin, int *distanceOut) { int maxDistance = 0; int farthestNode = 0; vector> process; set discovered; process.push_back(pair(origin,0)); while (process.size() > 0) { pair node = process.back(); // node, distance process.pop_back(); discovered.insert(node.first); if (node.second > maxDistance) { maxDistance = node.second; farthestNode = node.first; } for (int adjNode : points[node.first]) { if (discovered.find(adjNode) == discovered.end()) { process.push_back(pair(adjNode, node.second + 1)); } } } if (distanceOut != 0) { *distanceOut = maxDistance; } return farthestNode; } int main() { int n; cin >> n; cin.ignore(); int firstNode; for (int i = 0; i < n; i++) { int xi; int yi; cin >> xi >> yi; cin.ignore(); points[xi].push_back(yi); points[yi].push_back(xi); if (i == 0) { firstNode = xi; } } int distance; int farNode = findFarthestNode(firstNode, &distance); cerr << "Farthest node from origin is " << farNode << " with distance of " << distance << endl; farNode = findFarthestNode(farNode, &distance); cerr << "Farthest node from previous is " << farNode << " with distance of " << distance << endl; cout << ceil(distance / 2.0f) << endl; }