求助,莫名RE
查看原帖
求助,莫名RE
88460
olinr楼主2021/8/23 15:09
#include<bits/stdc++.h>
using namespace std;
#define LL long long
LL in() {
    LL x = 0, f = 1; char ch;
    while(!isdigit(ch = getchar()))(ch == '-')&&(f = -f);
    for(x = ch ^ 48; isdigit(ch = getchar()); x = (x << 1) + (x << 3) + (ch ^ 48));
    return x * f;
}
const int maxn = 1e6 + 100;
const int inf = 0x7fffffff;
struct node {
    int to;
    node *nxt;
    node(int to = 0, node *nxt = NULL): to(to), nxt(nxt) {}
}*head[maxn];
int du[maxn], dis[maxn];
int n, k;
void add(int from, int to) {
    head[from] = new node(to, head[from]);
}
void toposort() {
    queue<int> q;
    int ans = 0;
    while(!q.empty()) q.pop();
    for(int i = 1; i <= n; i++) if(du[i] == 1) q.push(i);
    for(int i = 1; i <= n - k; i++) {
        int from = q.front(); q.pop();
        int to = head[from]->to;
        du[from]--;
        du[to]--;
        ans = max(ans, dis[to] = max(dis[to], dis[from] + 1));
        if(du[to] == 1) q.push(to); 
    }
    printf("%d\n", ans);
}
int main() {
    n = in(), k = in();
    for(int i = 1; i < n; i++) {
        int x = in(), y = in();
        du[x]++, du[y]++;
        add(x, y), add(y, x);
    }
    toposort();
    return 0;
}

大佬们救命!

2021/8/23 15:09
加载中...