救救蒟蒻
查看原帖
救救蒟蒻
842408
pangyuchen75楼主2024/12/5 21:43

有没有大佬知道我哪错了 QWQ\texttt{QWQ}

#include <cstdio>
#include <cstring>
#include <queue>
using namespace std;

const int N = 1e5 + 5;
const int M = N * 2;

int n;
bool color[N];
int tot, head[N];
int to[M], nxt[M];
int f[N];
int ans;

void add(int u, int v) {
    to[tot] = v;
    nxt[tot] = head[u];
    head[u] = tot ++ ;
}

void dfs(int u, int father) {
    f[u] = 1;

    for (int i = head[u]; i != -1; i = nxt[i]) {
        int v = to[i];

        if (v == father)
            continue;
        
        dfs(v, u);

        if (color[v] != color[u])
            f[u] += f[v];
    }
}

void inp() {
    scanf("%d", &n);
    memset(head, -1, sizeof head);

    for (int i = 1; i <= n; i ++ )
        scanf("%d", &color[i]);
    
    for (int i = 1; i < n; i ++ ) {
        int u, v;
        scanf("%d%d", &u, &v);
        add(u, v), add(v, u);
    }
}

void work() {
    dfs(1, -1);
    
    for (int i = 1; i <= n; i ++ )
        if (ans < f[i])
            ans = f[i];
    
    printf("%d\n", ans);
}

int main() {
    inp();
    work();

    return 0;
}
2024/12/5 21:43
加载中...