using System;
using System.Collections.Generic;
using System.Linq;
class Program
{
static string InputPattern = "InputX";
static List<string> GetInputList()
{
var WillReturn = new List<string>();
if (InputPattern == "Input1") {
WillReturn.Add("2");
WillReturn.Add("1 2");
//1
//1
}
else if (InputPattern == "Input2") {
WillReturn.Add("6");
WillReturn.Add("1 2");
WillReturn.Add("1 3");
WillReturn.Add("3 4");
WillReturn.Add("3 5");
WillReturn.Add("5 6");
//7
//6
//8
//7
//7
//6
}
else {
string wkStr;
while ((wkStr = Console.ReadLine()) != null) WillReturn.Add(wkStr);
}
return WillReturn;
}
static int mN;
// 隣接リスト
static Dictionary<int, List<int>> mToNodeListDict = new Dictionary<int, List<int>>();
// 木DPとReRootingの設定01 配られてる値が無い時の値
static int DeriveDefaultNodeVal()
{
return 0;
}
// 木DPとReRootingの設定02 ノード値を引数として、配る値を返す
static int DeriveSendVal(int pNodeVal)
{
return 1 + pNodeVal;
}
// 木DPとReRootingの設定03 ノード値と、配られた値を、引数とし、新しいノード値を返す
static int DeriveNewNodeVal(int pNodeVal, int pSendVal)
{
return Math.Max(pNodeVal, pSendVal);
}
// 木DPとReRootingの設定04 配る値のListを引数として、最終的な配る値を返す
static int DeriveMergedSendVal(List<int> pSendValList)
{
int SendVal = DeriveDefaultNodeVal();
foreach (int EachSendVal in pSendValList) {
SendVal = DeriveNewNodeVal(SendVal, EachSendVal);
}
return SendVal;
}
static void Main()
{
List<string> InputList = GetInputList();
mN = int.Parse(InputList[0]);
int[] wkArr = { };
Action<string> SplitAct = pStr =>
wkArr = pStr.Split(' ').Select(pX => int.Parse(pX)).ToArray();
foreach (string EachStr in InputList.Skip(1)) {
SplitAct(EachStr);
int S = wkArr[0];
int T = wkArr[1];
if (mToNodeListDict.ContainsKey(S) == false) {
mToNodeListDict[S] = new List<int>();
}
if (mToNodeListDict.ContainsKey(T) == false) {
mToNodeListDict[T] = new List<int>();
}
mToNodeListDict[S].Add(T);
mToNodeListDict[T].Add(S);
}
// 前後に累積和を使うので、ノード番号の昇順にソート
foreach (var EachPair in mToNodeListDict) {
EachPair.Value.Sort();
}
// 部分木の直径[ノード]なDP表
var TreeDPDict = new Dictionary<int, int>();
// 根から葉に向かってDFSしてノードのListを返す
List<JyoutaiDef1> DFSResult = ExecDFS1(1);
DFSResult.Reverse(); // 葉から根への順番にする
foreach (JyoutaiDef1 EachJyoutai in DFSResult) {
// 親ノードに配る木DP
int Node = EachJyoutai.CurrNode;
if (TreeDPDict.ContainsKey(Node) == false) {
TreeDPDict[Node] = DeriveDefaultNodeVal(); ;
}
int ParentNode = EachJyoutai.ParentNode;
// 根ノードの場合
if (ParentNode == -1) continue;
if (TreeDPDict.ContainsKey(ParentNode) == false) {
TreeDPDict[ParentNode] = DeriveDefaultNodeVal(); ;
}
// 緩和
TreeDPDict[ParentNode] =
DeriveNewNodeVal(TreeDPDict[ParentNode], DeriveSendVal(TreeDPDict[Node]));
}
// ReRootingなDFSを行う
IEnumerable<int> AnswerEnum = ExecDFS2(1, TreeDPDict);
var sb = new System.Text.StringBuilder();
foreach (int EachInt in AnswerEnum) {
sb.Append(EachInt);
sb.AppendLine();
}
Console.Write(sb.ToString());
}
// 根から葉に向かってDFSしてノードのListを返す
struct JyoutaiDef1
{
internal int CurrNode;
internal int ParentNode;
}
static List<JyoutaiDef1> ExecDFS1(int pRootNode)
{
var WillReturn = new List<JyoutaiDef1>();
var Stk = new Stack<JyoutaiDef1>();
JyoutaiDef1 WillPush;
WillPush.CurrNode = pRootNode;
WillPush.ParentNode = -1;
Stk.Push(WillPush);
var VisitedSet = new HashSet<int>();
VisitedSet.Add(pRootNode);
while (Stk.Count > 0) {
JyoutaiDef1 Popped = Stk.Pop();
WillReturn.Add(Popped);
if (mToNodeListDict.ContainsKey(Popped.CurrNode) == false) {
continue;
}
foreach (int EachToNode in mToNodeListDict[Popped.CurrNode]) {
if (VisitedSet.Add(EachToNode)) {
WillPush.CurrNode = EachToNode;
WillPush.ParentNode = Popped.CurrNode;
Stk.Push(WillPush);
}
}
}
return WillReturn;
}
// ReRootingなDFSを行う
struct JyoutaiDef2
{
internal int PrevRootNode;
internal int PrevRootVal;
internal int CurrRootNode;
internal int CurrRootVal;
}
static IEnumerable<int> ExecDFS2(int pRootNode, Dictionary<int, int> pTreeDPDict)
{
var WillReturn = new List<int>();
var Stk = new Stack<JyoutaiDef2>();
JyoutaiDef2 WillPush;
WillPush.PrevRootNode = -1;
WillPush.PrevRootVal = -1;
WillPush.CurrRootNode = pRootNode;
WillPush.CurrRootVal = pTreeDPDict[pRootNode];
Stk.Push(WillPush);
var VisitedSet = new HashSet<int>();
VisitedSet.Add(pRootNode);
var AnswerDict = new Dictionary<int, int>();
while (Stk.Count > 0) {
JyoutaiDef2 Popped = Stk.Pop();
AnswerDict[Popped.CurrRootNode] = Popped.CurrRootVal;
// 累積Maxを管理する
int ChildCnt = 0;
if (mToNodeListDict.ContainsKey(Popped.CurrRootNode)) {
mToNodeListDict[Popped.CurrRootNode].Remove(Popped.PrevRootNode);
ChildCnt = mToNodeListDict[Popped.CurrRootNode].Count;
}
if (ChildCnt == 0) continue;
int[] RunMaxSei = new int[ChildCnt]; // 正方向の累積Max
int[] RunMaxRev = new int[ChildCnt]; // 逆方向の累積Max
int StaNode = mToNodeListDict[Popped.CurrRootNode][0];
int EndNode = mToNodeListDict[Popped.CurrRootNode][ChildCnt - 1];
RunMaxSei[0] = DeriveSendVal(pTreeDPDict[StaNode]);
RunMaxRev[RunMaxRev.GetUpperBound(0)] = DeriveSendVal(pTreeDPDict[EndNode]);
for (int I = 1; I <= ChildCnt - 1; I++) {
int CurrChildNode = mToNodeListDict[Popped.CurrRootNode][I];
RunMaxSei[I] =
DeriveNewNodeVal(RunMaxSei[I - 1], DeriveSendVal(pTreeDPDict[CurrChildNode]));
}
for (int I = ChildCnt - 2; 0 <= I; I--) {
int CurrChildNode = mToNodeListDict[Popped.CurrRootNode][I];
RunMaxRev[I] =
DeriveNewNodeVal(RunMaxRev[I + 1], DeriveSendVal(pTreeDPDict[CurrChildNode]));
}
// 状態の遷移
for (int I = 0; I <= mToNodeListDict[Popped.CurrRootNode].Count - 1; I++) {
int CurrRootNode = mToNodeListDict[Popped.CurrRootNode][I];
if (VisitedSet.Add(CurrRootNode) == false) {
continue;
}
// 左方向の累積Max
int LeftRunMax = DeriveDefaultNodeVal();
bool HasLeftRunVal = false;
if (I > 0) {
LeftRunMax = RunMaxSei[I - 1];
HasLeftRunVal = true;
}
// 右方向の累積Max
int RightRunMax = DeriveDefaultNodeVal();
bool HasRightRunVal = false;
if (I < mToNodeListDict[Popped.CurrRootNode].Count - 1) {
RightRunMax = RunMaxRev[I + 1];
HasRightRunVal = true;
}
WillPush.PrevRootNode = Popped.CurrRootNode;
var PrevRootValKouhoList = new List<int>();
if (HasLeftRunVal) PrevRootValKouhoList.Add(LeftRunMax);
if (HasRightRunVal) PrevRootValKouhoList.Add(RightRunMax);
if (Popped.PrevRootNode > -1) {
PrevRootValKouhoList.Add(DeriveSendVal(Popped.PrevRootVal));
}
WillPush.PrevRootVal = DeriveMergedSendVal(PrevRootValKouhoList);
WillPush.CurrRootNode = CurrRootNode;
var CurrRootValKouhoList = new List<int>();
CurrRootValKouhoList.Add(pTreeDPDict[CurrRootNode]);
CurrRootValKouhoList.Add(DeriveSendVal(WillPush.PrevRootVal));
WillPush.CurrRootVal = DeriveMergedSendVal(CurrRootValKouhoList);
Stk.Push(WillPush);
}
}
foreach (var EachPair in AnswerDict.OrderBy(pX => pX.Key)) {
yield return 2 * (mN - 1) - EachPair.Value;
}
}
}