/* -*- coding: utf-8 -*- * * 1484.cc: No.1484 木に数を書き込む問題 / Just Write Numbers! 2 - yukicoder */ #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include using namespace std; /* constant */ const int MAX_N = 200000; /* typedef */ typedef vector vi; typedef queue qi; /* global variables */ vi nbrs[MAX_N]; int ds[MAX_N]; /* subroutines */ int bfs(int st) { memset(ds, -1, sizeof(ds)); ds[st] = 0; qi q; q.push(st); int maxd = -1, gl = -1; while (! q.empty()) { int u = q.front(); q.pop(); if (maxd < ds[u]) maxd = ds[u], gl = u; vi &nbru = nbrs[u]; for (vi::iterator vit = nbru.begin(); vit != nbru.end(); vit++) { int v = *vit; if (ds[v] < 0) { ds[v] = ds[u] + 1; q.push(v); } } } return gl; } /* main */ int main() { int n; scanf("%d", &n); for (int i = 1; i < n; i++) { int a, b; scanf("%d%d", &a, &b); a--, b--; nbrs[a].push_back(b); nbrs[b].push_back(a); } int x = bfs(0); int y = bfs(x); printf("%d\n", 2 * (n - 1) - ds[y]); return 0; }