include iostream include vector include cstdio using namespace std int

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
#include <iostream>
#include <vector>
#include <cstdio>
using namespace std;
int n;
vector <pair <int, int> > g;
vector <int> col;
int dfs(int x)
{
if (g[x].first == -1)
{
return (col[x]);
}
return col[x] = 6 - dfs(g[x].first) - dfs(g[x].second);
}
int main()
{
freopen("coloring.in", "r", stdin);
//freopen("coloring.out", "w", stdout);
int son[3];
while (true)
{
scanf("%d\n", &n);
if (!n)
{
return 0;
}
g.assign(n, make_pair(-1, -1));
int p;
for (int i = 1, j = 0; i < n; ++i)
{
scanf("%d\n", &p);
if (p == 1)
{
son[j] = i;
++j;
continue;
}
if (g[p - 1].first == -1)
{
g[p - 1].first = i;
}
else
{
g[p - 1].second = i;
}
}
int m;
scanf("%d\n", &m);
int x, c;
col.resize(n);
for (int i = 0; i < m; ++i)
{
scanf("%d %d\n", &x, &c);
col[x - 1] = c;
}
int a, b;
a = dfs(son[0]);
b = dfs(son[1]);
c = dfs(son[2]);
if (a + b + c == 6)
{
printf("NO\n");
}
else
{
if (a == b && a == c)
{
col[0] = ((a + 1) % 3) + 1;
}
else
{
if (a == b)
{
col[0] = 6 - a - c;
}
else
{
if (a == c)
{
col[0] = 6 - a - b;
}
else
{
col[0] = 6 - a - c;
}
}
}
printf("YES\n");
for (int i = 0; i < n; ++i)
{
printf("%d ", col[i]);
}
scanf("\n");
printf("\n");
}
}
}