分析

其实如果一条边链接了两个颜色不一样的节点,其实这个边根本没有任何意义,因为你从任意方向都不可能进入这条边,那么我们只需要用并查集维护集合数量就可以了。

代码

#include<bits/stdc++.h>
using namespace std;
int read() {
    int x = 0,f = 0;char ch = getchar();
    while(!isdigit(ch)) {if(ch == '-') f = 1;ch = getchar();}
    while(isdigit(ch)) {x = x*10 + ch - '0';ch = getchar();}
    return f?-x:x;
}
const int N = 2e5+ 100;
int fa[N],si[N],n,val[N],vis[N],Ans;
vector<int> ans;
int find(int x) {return fa[x] == x?x:fa[x] = find(fa[x]);}
int main() {

    n = read();
    for(int i = 1;i <= n;i++) {
        val[i] = read();si[i] = 1;fa[i] = i;
    }
    for(int i = 1;i < n;i++) {
        int a = read(),b = read();
        if(val[a] == val[b]) {
            int fx = find(a),fy = find(b);
            fa[fx] = fy;si[fy] += si[fx];
        }
    }
    for(int i = 1;i <= n;i++) {
        int a = find(i);
        if(vis[a]) continue;
        if(Ans < si[a]) Ans = si[a];
    }
    for(int i = 1;i <= n;i++) {
        if(si[find(i)] == Ans) ans.push_back(i);
    }
    printf("%d\n",ans.size());
    for(auto y:ans) printf("%d ",y);
    return 0;
}