比赛 |
EYOI与SBOI开学欢乐赛11th |
评测结果 |
AAAAAAAAAAATTAAAAWAATATATTWTWA |
题目名称 |
巡逻 |
最终得分 |
66 |
用户昵称 |
HeSn |
运行时间 |
11.324 s |
代码语言 |
C++ |
内存使用 |
82.03 MiB |
提交时间 |
2022-10-14 21:23:34 |
显示代码纯文本
#include<bits/stdc++.h>
using namespace std;
#define int long long
const int MAXN = 2000010;
int n, k, ans, maxn, rt, vis[MAXN], cs[MAXN], cnt, flg, maxn2, rt2;
vector<int>cd[MAXN];
void dfs(int x, int fa, int s) {
if(flg == 1 && s == maxn) {
for(int i = 1; i <= cnt; i ++) {
vis[cs[i]] = 1;
}
flg = 0;
}
if(s > maxn) {
maxn = s;
rt = x;
}
for(int i = 0; i < cd[x].size(); i ++) {
int u = cd[x][i];
if(u == fa) {
continue;
}
cs[++ cnt] = u;
dfs(u, x, s + 1);
cnt --;
}
}
void dfs2(int x, int fa, int s) {
if(s > maxn2) {
maxn2 = s;
rt2 = x;
}
for(int i = 0; i < cd[x].size(); i ++) {
int u = cd[x][i];
if(u == fa || (vis[u] && vis[x])) {
continue;
}
dfs2(u, x, s + 1);
cnt --;
}
}
signed main() {
freopen("xunluo.in", "r", stdin);
freopen("xunluo.out", "w", stdout);
cin >> n >> k;
for(int i = 1; i < n; i ++) {
int x, y;
cin >> x >> y;
cd[x].push_back(y);
cd[y].push_back(x);
}
dfs(1, 0, 0);
dfs(rt, 0, 0);
if(k == 1) {
cout << n * 2 - maxn - 1;
return 0;
}
ans = n * 2 - maxn - 1;
flg = 1;
vis[rt] = 1;
dfs(rt, 0, 0);
for(int i = 1; i <= n; i ++) {
if(!vis[i]) {
dfs2(i, 0, 0);
}
}
dfs2(rt2, 0, 0);
cout << ans - maxn2 + 1;
return 0;
}