记录编号 |
594848 |
评测结果 |
AAAAAAAAAA |
题目名称 |
消防演练 |
最终得分 |
100 |
用户昵称 |
┭┮﹏┭┮ |
是否通过 |
通过 |
代码语言 |
C++ |
运行时间 |
0.522 s |
提交时间 |
2024-10-05 20:08:38 |
内存使用 |
11.92 MiB |
显示代码纯文本
#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define pii pair<int,int>
#define fi first
#define in inline
#define se second
#define mp make_pair
#define pb push_back
const int N = 2e5+10;
ll read(){
ll x = 0,f = 1;char c = getchar();
for(;c < '0' || c > '9';c = getchar())if(c == '-')f = -1;
for(;c >= '0' && c <= '9';c = getchar())x = (x<<1) + (x<<3) + c-'0';
return x * f;
}
int n;
vector<int>e[N];
int f[N],g[N],ans;
void dfs(int x,int fa){
for(int y : e[x]){
if(y == fa)continue;
dfs(y,x);
f[x] = max(f[x],f[y] + (int)e[x].size() - 2);
}
f[x] = max(f[x],(int)e[x].size() - 1);
ans = max(ans,f[x]);
}
void dfs2(int x,int fa){
for(int y : e[x]){
if(y == fa)continue;
dfs2(y,x);
ans = max(ans,g[x] + f[y] + (int)e[x].size() - 2);
g[x] = max(g[x],f[y]);
}
}
int main(){
freopen("drill.in","r",stdin);
freopen("drill.out","w",stdout);
n = read();
for(int i = 1;i < n;i++){
int x = read(),y = read();
e[x].pb(y),e[y].pb(x);
}
if(n == 2)return printf("0\n"),0;
dfs(1,0);
// for(int i = 1;i <= n;i++)printf("%d ",f[i]);
// printf("\n");
// printf("%d\n",ans);
dfs2(1,0);
printf("%d\n",ans);
return 0;
}