树上钟同步

 树上钟同步_第1张图片

#include
#include
#include
using namespace std;

const int N = 2505;
int ori[N], f[N];
vector edge[N]; // 邻接表的简单实现形式

void dfs(int u, int fa) {
    for (int v : edge[u]) {
        if (v == fa) continue;
        dfs(v, u);
        f[u] += (12 - f[v]);
        f[u] %= 12;
    }
}

int main() {
    int n;
    scanf("%d", &n);
    for (int i = 1; i <= n; i++) {
        scanf("%d", &ori[i]);
    }
    for (int i = 0; i < n - 1; i++) {
        int a, b;
        scanf("%d%d", &a, &b);
        edge[a].push_back(b);
        edge[b].push_back(a);
    }
    for (int i = 1; i <= n; i++) {
        memcpy(f, ori, sizeof(f));
        dfs(i, 0);
        if (f[i] == 0 || f[i] == 1) printf("%d ", i);
    }
    printf("\n");
    return 0;
}

你可能感兴趣的:(海贼OJ,算法,深度优先,图论)