Submission #3492683
Source Code Expand
#include <bits/stdc++.h>
#define For(i, l, r) for(register int i = (l), i##end = (int)(r); i <= i##end; ++i)
#define Fordown(i, r, l) for(register int i = (r), i##end = (int)(l); i >= i##end; --i)
#define Set(a, v) memset(a, v, sizeof(a))
#define Cpy(a, b) memcpy(a, b, sizeof(a))
#define debug(x) cout << #x << ": " << (x) << endl
#define DEBUG(...) fprintf(stderr, __VA_ARGS__)
#define pb push_back
using namespace std;
template<typename T> inline bool chkmin(T &a, T b) { return b < a ? a = b, 1 : 0; }
template<typename T> inline bool chkmax(T &a, T b) { return b > a ? a = b, 1 : 0; }
inline int read() {
int x(0), sgn(1); char ch(getchar());
for (; !isdigit(ch); ch = getchar()) if (ch == '-') sgn = -1;
for (; isdigit(ch); ch = getchar()) x = (x * 10) + (ch ^ 48);
return x * sgn;
}
void File() {
#ifdef zjp_shadow
freopen ("E.in", "r", stdin);
freopen ("E.out", "w", stdout);
#endif
}
const int N = 1e5 + 1e3, inf = 0x3f3f3f3f;
vector<int> G[N];
int root, n, l[N], r[N], val[N];
void Dfs(int u, int fa, int odd) {
if (~val[u]) {
if ((val[u] & 1) != odd) {
puts("No"); exit(0);
}
l[u] = r[u] = val[u];
} else l[u] = -1e6, r[u] = 1e6;
for (int v : G[u]) if (v != fa) {
Dfs(v, u, odd ^ 1);
chkmax(l[u], l[v] - 1);
chkmin(r[u], r[v] + 1);
}
if (l[u] > r[u]) {
puts("No"); exit(0);
}
}
int ans[N];
void Get_Ans(int u, int fa) {
for (int v : G[u]) if (v != fa) {
ans[v] = ans[u] + 1;
if (ans[v] > r[v])
ans[v] = ans[u] - 1;
Get_Ans(v, u);
}
}
int main () {
File();
Set(val, -1);
n = read();
For (i, 1, n - 1) {
int u = read(), v = read();
G[u].pb(v); G[v].pb(u);
}
int root = 0, k = read();
For (i, 1, k) {
root = read();
val[root] = read();
}
Dfs(root, 0, val[root] & 1);
puts("Yes");
ans[root] = val[root];
Get_Ans(root, 0);
For (i, 1, n)
printf ("%d\n", ans[i]);
return 0;
}
Submission Info
Submission Time |
|
Task |
E - Integers on a Tree |
User |
zjp_shadow |
Language |
C++14 (GCC 5.4.1) |
Score |
800 |
Code Size |
1966 Byte |
Status |
AC |
Exec Time |
53 ms |
Memory |
15744 KB |
Judge Result
Set Name |
sample |
All |
Score / Max Score |
0 / 0 |
800 / 800 |
Status |
|
|
Set Name |
Test Cases |
sample |
sample_01.txt, sample_02.txt, sample_03.txt |
All |
binary_01.txt, binary_02.txt, hand_01.txt, hand_02.txt, hand_03.txt, kary_01.txt, kary_02.txt, kary_03.txt, line_01.txt, line_02.txt, line_03.txt, line_04.txt, line_05.txt, line_06.txt, random0_01.txt, random1_01.txt, random1_02.txt, random1_03.txt, random1_04.txt, random1_05.txt, random1_06.txt, random1_07.txt, random1_08.txt, random2_01.txt, random2_02.txt, random2_03.txt, random2_04.txt, random2_05.txt, random2_06.txt, random3_01.txt, random3_02.txt, random4_01.txt, random4_02.txt, random4_03.txt, sample_01.txt, sample_02.txt, sample_03.txt, star_01.txt, star_02.txt |
Case Name |
Status |
Exec Time |
Memory |
binary_01.txt |
AC |
39 ms |
11904 KB |
binary_02.txt |
AC |
24 ms |
10880 KB |
hand_01.txt |
AC |
3 ms |
3072 KB |
hand_02.txt |
AC |
2 ms |
3072 KB |
hand_03.txt |
AC |
2 ms |
3072 KB |
kary_01.txt |
AC |
22 ms |
6272 KB |
kary_02.txt |
AC |
30 ms |
7936 KB |
kary_03.txt |
AC |
21 ms |
6656 KB |
line_01.txt |
AC |
22 ms |
6144 KB |
line_02.txt |
AC |
33 ms |
12416 KB |
line_03.txt |
AC |
35 ms |
15104 KB |
line_04.txt |
AC |
37 ms |
15744 KB |
line_05.txt |
AC |
24 ms |
13312 KB |
line_06.txt |
AC |
27 ms |
14720 KB |
random0_01.txt |
AC |
28 ms |
6272 KB |
random1_01.txt |
AC |
43 ms |
8064 KB |
random1_02.txt |
AC |
44 ms |
8064 KB |
random1_03.txt |
AC |
44 ms |
8064 KB |
random1_04.txt |
AC |
49 ms |
8064 KB |
random1_05.txt |
AC |
43 ms |
8064 KB |
random1_06.txt |
AC |
43 ms |
8064 KB |
random1_07.txt |
AC |
44 ms |
8064 KB |
random1_08.txt |
AC |
53 ms |
8064 KB |
random2_01.txt |
AC |
27 ms |
7040 KB |
random2_02.txt |
AC |
27 ms |
7040 KB |
random2_03.txt |
AC |
26 ms |
7040 KB |
random2_04.txt |
AC |
25 ms |
7040 KB |
random2_05.txt |
AC |
34 ms |
7040 KB |
random2_06.txt |
AC |
37 ms |
7040 KB |
random3_01.txt |
AC |
27 ms |
7040 KB |
random3_02.txt |
AC |
32 ms |
7040 KB |
random4_01.txt |
AC |
43 ms |
8064 KB |
random4_02.txt |
AC |
43 ms |
8064 KB |
random4_03.txt |
AC |
43 ms |
8064 KB |
sample_01.txt |
AC |
2 ms |
3072 KB |
sample_02.txt |
AC |
2 ms |
3072 KB |
sample_03.txt |
AC |
2 ms |
3072 KB |
star_01.txt |
AC |
29 ms |
8440 KB |
star_02.txt |
AC |
20 ms |
7288 KB |