fork download
  1. #include <bits/stdc++.h>
  2. #define se second
  3. #define fi first
  4. #define pb push_back
  5. #define get_bit(mask,j) (mask>>j)&1
  6. using namespace std;
  7. using ll = long long;
  8. using pii = pair<int,int>;
  9. using pll = pair<ll,ll>;
  10. const int maxn = 2e6+5;
  11. const int mod = 1e9+7;
  12. const ll inf = 1e18;
  13.  
  14. void read()
  15. {
  16. #define name "colorful"
  17. cin.tie(0)->ios_base::sync_with_stdio(0);cout.tie(0);
  18. if(fopen(name".INP","r")){
  19. freopen(name".INP","r",stdin);
  20. freopen(name".OUT","w",stdout);
  21. }
  22. }
  23.  
  24. int deg[maxn],color[maxn];
  25. vector<int> adj[maxn];
  26.  
  27. void solve()
  28. {
  29. int n;
  30. cin >> n;
  31. for(int i = 1,u,v; i < n; i++){
  32. cin >> u >> v;
  33. adj[u].pb(v);
  34. adj[v].pb(u);
  35. }
  36. for(int i = 1; i <= n; i++){
  37. cin >> color[i];
  38. }
  39. int remove = 0;
  40. for(int i = 1; i <= n; i++){
  41. for(auto &v : adj[i]){
  42. if(color[i] != color[v]){
  43. deg[i]++;
  44. deg[v]++;
  45. remove++;
  46. }
  47. }
  48. }
  49. vector<int> res;
  50. for(int i = 1; i <= n; i++){
  51. if(deg[i] == remove) res.pb(i);
  52. }
  53. if(res.empty()) cout << "NO";
  54. else{
  55. sort(res.begin(),res.end());
  56. cout << "YES\n";
  57. for(auto &x : res) cout << x << " ";
  58. }
  59. }
  60.  
  61. signed main()
  62. {
  63. read();
  64. int subtask;
  65. cin >> subtask;
  66. solve();
  67. return 0;
  68. }
Success #stdin #stdout 0.02s 53628KB
stdin
1
7
4 1
4 2
4 3
3 7
4 6
6 5
2 2 7 1 9 9 7
stdout
YES
4