Skip to content

Instantly share code, notes, and snippets.

@ateruimashin
Created April 11, 2021 19:05
Show Gist options
  • Save ateruimashin/7a6744d8e235af577ea4259c308886f3 to your computer and use it in GitHub Desktop.
Save ateruimashin/7a6744d8e235af577ea4259c308886f3 to your computer and use it in GitHub Desktop.
#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (n); i++)
#define rep2(i, s, n) for (int i = (s); i < (n); i++)
using namespace std;
using ll = long long;
using P = pair<int, int>;
vector<int> color; //頂点の色
vector<vector<int>> to; //頂点のつながり
vector<int> cnt(100001); //出現した色をカウント
vector<int> ans; //よい頂点
void dfs(int v, int p = -1) { //親がいないことをp=-1で表す
if (cnt[color[v]] == 0) ans.push_back(v + 1);
cnt[color[v]]++;
for (int u : to[v]) {
if (u == p) continue;
dfs(u, v);
}
cnt[color[v]]--;
}
int main() {
//入力と木構造を作る
int n;
cin >> n;
color.resize(n);
rep(i, n) cin >> color[i];
//木の作成
to.resize(n);
rep(i, n - 1) {
int a, b;
cin >> a >> b;
a--, b--;
//無向
to[a].push_back(b);
to[b].push_back(a);
}
//深さ優先探索
dfs(0);
//答えを昇順で出力するので
sort(ans.begin(), ans.end());
//答えを出力
for (int v : ans) cout << v << endl;
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment