比赛 |
20120711 |
评测结果 |
AAATTTTTTTTTTTT |
题目名称 |
路由器 |
最终得分 |
20 |
用户昵称 |
CC |
运行时间 |
12.001 s |
代码语言 |
C++ |
内存使用 |
1.17 MiB |
提交时间 |
2012-07-11 11:35:35 |
显示代码纯文本
#include <cstdio>
#include <algorithm>
struct edge {
int y;
edge *next;
edge(int y,edge *next): y(y),next(next) {};
}*a[100005];
int n,K,dep;
int ok[100005];
bool done[100005],get;
void out(int u,int v) {
ok[u]++;
if (!v) return;
for (edge *p = a[u];p;p = p->next) {
out(p->y,v - 1);
}
}
void bef(int u,int v) {
ok[u]--;
if (!v) return;
for (edge *p = a[u];p;p = p->next) {
bef(p->y,v - 1);
}
}
void dfs(int u) {
if (u > dep) {
for (int i = 1;i <= n;i++) if (!ok[i]) {
return;
}
get = 1;
return;
}
for (int i = 1;i <= n;i++)
if (!done[i]) {
done[i] = 0;
out(i,K);
dfs(u + 1);
bef(i,K);
}
}
int main() {
freopen("routea.in","r",stdin);
freopen("routea.out","w",stdout);
scanf("%d %d",&n,&K);
if (K == 0) {
printf("%d",n);
return 0;
}
int p,q;
for (int i = 1;i < n;i++) {
scanf("%d %d", &p, &q);
a[p] = new edge(q,a[p]);
a[q] = new edge(p,a[q]);
}
get = 0;
for (dep = 1;dep <= n;dep++) {
dfs(1);
if (get) {
printf("%d",dep);
return 0;
}
}
return 0;
}