记录编号 |
385453 |
评测结果 |
AAAAAAAAAA |
题目名称 |
[HAOI 2009]毛毛虫 |
最终得分 |
100 |
用户昵称 |
HeHe |
是否通过 |
通过 |
代码语言 |
C++ |
运行时间 |
0.263 s |
提交时间 |
2017-03-21 16:49:31 |
内存使用 |
8.61 MiB |
显示代码纯文本
- #include<iostream>
- #include<cstdio>
- #include<cstring>
- using namespace std;
-
- #define MAXN 300010
- #define is_num(tmp) (tmp<='9' and tmp>='0')
-
- const inline int in(void){
- char tmp=getchar();
- int res=0;
- while(!is_num(tmp))tmp=getchar();
- while(is_num(tmp))
- res=(res<<1)+(res<<3)+(tmp^48),
- tmp=getchar();
- return res;
- }
-
- struct EDGE{
- int to, ne;
-
- inline EDGE(){;}
- inline EDGE(int a, int b){
- to=a, ne=b;
- }
- };
-
- inline void add_edge(int, int);
- void dfs(int);
-
- EDGE s[MAXN<<1];
- bool visit[MAXN];
- int cnt;
- int head[MAXN], d[MAXN];
- int f[MAXN];
- int N, M, ans;
-
- int main(){
- #ifndef LOCAL
- freopen("worma.in", "r", stdin);
- freopen("worma.out", "w", stdout);
- #endif
-
- N=in(), M=in();
- for(int i=1; i<=M; ++i){
- add_edge(in(), in());
- }
-
- dfs(1);
-
- printf("%d", ans+1);
-
- return 0;
- }
-
- inline void add_edge(int fr, int to){
- s[++cnt]=EDGE(to, head[fr]), head[fr]=cnt, ++d[fr];
- s[++cnt]=EDGE(fr, head[to]), head[to]=cnt, ++d[to];
- }
-
- void dfs(int now){
- visit[now]=true;
-
- int max1 = 0, max2 = 0;
-
- for(int i=head[now], to; i; i=s[i].ne){
- to=s[i].to;
- if(!visit[to]){
- dfs(to);
- if(f[to]>max1)max2=max1, max1=f[to];
- else if(f[to]>max2)max2=f[to];
- }
- }
-
- f[now]=max1+d[now]-1;
- ans = max(ans, max2 + f[now] + 1);
-
- return ;
- }