Live2D

[HAOI2009]毛毛虫

#include<bits/stdc++.h>
using namespace std;
const int N=3e6+5;
struct node{int to,nxt;}e[N<<1];
int n,m,head[N],tot,a[N],ans;
void add(int u,int v) {
    e[++tot].to=v,e[tot].nxt=head[u];
    head[u]=tot;
}
inline void dfs(int fa,int u,int dis){
    if (dis>ans) ans=dis,tot=u;
    for (int i=head[u];i;i=e[i].nxt) {
        int v=e[i].to;
        if (v!=fa) 
            dfs(u,v,dis+a[u]);
    }
        
}
int main() {
    scanf("%d%d",&n,&m);
    memset(a,-1,sizeof(a));
    for (int i=1,u,v;i<=m;i++) {
        scanf("%d%d",&u,&v);
        add(u,v),add(v,u);
        a[u]++,a[v]++;
    }
    dfs(0,1,a[1]);
    ans=0;
    dfs(0,tot,a[tot]);
    printf("%d\n",ans+2);
    return 0;
}
posted @ 2020-01-16 16:24  γひん0ΖΖƦ  阅读(159)  评论(0编辑  收藏  举报