记录编号 |
243748 |
评测结果 |
AAAAAAAAAA |
题目名称 |
[HAOI 2009]毛毛虫 |
最终得分 |
100 |
用户昵称 |
Fmuckss |
是否通过 |
通过 |
代码语言 |
C++ |
运行时间 |
0.573 s |
提交时间 |
2016-03-30 17:24:35 |
内存使用 |
15.48 MiB |
显示代码纯文本
#include<iostream>
#include<cstdio>
using namespace std;
const int maxn = 3e5+50;
int f[maxn];
int ans;
int n, m;
int d[maxn];
template <class T> class stack {
private:
T a[maxn];
int top_;
public:
inline stack() { top_ = 0; }
inline void push(T tar) { a[++top_] = tar; }
inline void pop() { top_--; }
inline T top() { return a[top_]; }
inline bool empty() { return !top_; }
};
struct edge {
int to, ne;
inline edge() {}
inline edge(int to_, int ne_) { to = to_, ne = ne_; }
}e[maxn*2];
int head[maxn], tot;
inline int get_num() {
int ans = 0;
char tmp = getchar();
while(tmp < '0' || tmp > '9') tmp = getchar();
while(tmp <= '9' && tmp >= '0') {
ans = 10*ans + tmp - '0';
tmp = getchar();
}
return ans;
}
inline void add_edge(int fr, int to) {
e[++tot] = edge(to, head[fr]); head[fr] = tot; d[fr]++;
e[++tot] = edge(fr, head[to]); head[to] = tot; d[to]++;
}
inline void read() {
n = get_num(); m = get_num();
for(int i = 1; i <= m; i++) add_edge(get_num(), get_num());
}
struct dfs {
int fa, now, i, mx, se;
bool be;
dfs() {}
dfs(int fa_, int now_, int i_, int mx_, int se_, bool be_) {
fa = fa_, now = now_, i = i_, mx = mx_, se = se_, be = be_;
}
};
stack <dfs> s;
bool vis[maxn];
inline void dp() {
s.push(dfs(0, 1, head[1], 0, 0, true));
int i, mx, se, fa;
dfs now;
while(!s.empty()) {
now = s.top(); s.pop();
fa = now. fa, mx = now.mx, se = now.se, i = now.i;
vis[now.now] = true;
if(now.be) {
s.push(dfs(fa, now.now, i, mx, se, false));
int to = e[i].to;
if(to == fa) continue;
s.push(dfs(now.now, to, head[to], 0, 0, true));
} else {
int to = e[i].to;
if(to != fa) {
if(f[to] > mx) se = mx, mx = f[to];
else if(f[to] > se) se = f[to];
}
if(!e[i].ne) {
f[now.now] = mx+d[now.now]-1;
ans = max(ans, se + f[now.now] + 1);
continue;
} else {
s.push(dfs(fa, now.now, e[i].ne, mx, se, true));
}
}
}
// for(int i = 1; i <= n; i++) {
// printf("%d ", f[i]);
// }
}
int main() {
freopen("worma.in", "r", stdin);
freopen("worma.out", "w", stdout);
read();
dp();
printf("%d", ans+1);//加上的1是根节点本身(根节点不会到达他的父节点)
return 0;
}