#include <iostream>
#include <cstring>
using namespace std;
const int N = 1e5+10;
int h[2*N], e[2*N], ne[2*N], idx;//和单链表一样,用h[]表示邻接多重表,a,b互相指向对方最多2*n
bool st[N];//标记访问情况
int n;
int ans =N;//默认连通块最大为N个结点
void link(int a, int b)
{
e[idx] = b;
ne[idx] = h[a];
h[a] = idx++;//将b结点插入到a的链表头下
}
int dfs(int u)//对结点u进行深度遍历
{
st[u] = true;//对该结点进行访问标记
int size = 0;
int sum = 0;
for (int i = h[u]; i != -1; i = ne[i])
{
int j = e[i];
if (st[j])
{
continue;
}
int s = dfs(j);
size = max(size,s);
sum += s;//该点的连通个数
}
size = max(size, n - sum - 1);
ans = min(ans, size);
return sum + 1;
}
int main()
{
cin >> n;
memset(h, -1, sizeof(h));
int a, b;
for (int i = 1; i < n; i++)
{
cin >> a >> b;
link(a, b);
link(b, a);//无向图,a能指向b,b能指向a
}
dfs(4);
cout << ans;
return 0;
}
#include <iostream>#include <cstring>using namespace std;const int N = 1e5+10;int h[2*N], e[2*N], ne[2*N], idx;//和单链表一样,用h[]表示邻接多重表,a,b互相指向对方最多2*nbool st[N];//标记访问情况int n;int ans =N;//默认连通块最大为N个结点void link(int a, int b){ e[idx].