Submission #1692600
Source Code Expand
#include <bits/stdc++.h>
using namespace std;
const int N=100010;
struct edge{int s,t,n;}e[2*N];
int n,k,h[N],a[N],col[N],l[N],r[N],fail;
void dfs1(int x,int f)
{
if ((~a[x])&&((a[x]&1)^col[x])) fail=1;
for (int i=h[x],y; y=e[i].t,i; i=e[i].n)
if (y!=f) col[y]=col[x]^1,dfs1(y,x);
}
void dfs2(int x,int f)
{
if (~a[x]) l[x]=r[x]=a[x]; else l[x]=-1<<30,r[x]=1<<30;
for (int i=h[x],y; y=e[i].t,i; i=e[i].n)
if (y!=f) dfs2(y,x),l[x]=max(l[x],l[y]-1),r[x]=min(r[x],r[y]+1);
l[x]+=((l[x]&1)^col[x]),r[x]-=((r[x]&1)^col[x]);
if (l[x]>r[x]) fail=1;
}
void dfs3(int x,int f)
{
for (int i=h[x],y; y=e[i].t,i; i=e[i].n)
if (y!=f) a[y]=a[x]+((l[y]<=a[x]+1)&&(a[x]+1<=r[y])?1:-1),dfs3(y,x);
}
void work()
{
scanf("%d",&n),memset(a,-1,sizeof(a));
for (int i=1,u,v,tot=0; i<n; i++)
{
scanf("%d %d",&u,&v);
e[++tot]=(edge){u,v,h[u]},h[u]=tot;
e[++tot]=(edge){v,u,h[v]},h[v]=tot;
}
scanf("%d",&k);
for (int i=1,v,p; i<=k; i++) scanf("%d %d",&v,&p),a[v]=p;
for (int i=1; i<=n; i++) if (~a[i]) {col[i]=a[i]&1,dfs1(i,0); break;}
if (fail) puts("No"),exit(0);
dfs2(1,0);
if (fail) puts("No"),exit(0);
a[1]=l[1],dfs3(1,0);
puts("Yes");
for (int i=1; i<=n; i++) printf("%d\n",a[i]);
}
int main()
{
work();
return 0;
}
Submission Info
Submission Time |
|
Task |
E - Integers on a Tree |
User |
YMDragon |
Language |
C++14 (GCC 5.4.1) |
Score |
800 |
Code Size |
1312 Byte |
Status |
AC |
Exec Time |
55 ms |
Memory |
9856 KB |
Compile Error
./Main.cpp: In function ‘void work()’:
./Main.cpp:32:39: warning: ignoring return value of ‘int scanf(const char*, ...)’, declared with attribute warn_unused_result [-Wunused-result]
scanf("%d",&n),memset(a,-1,sizeof(a));
^
./Main.cpp:35:24: warning: ignoring return value of ‘int scanf(const char*, ...)’, declared with attribute warn_unused_result [-Wunused-result]
scanf("%d %d",&u,&v);
^
./Main.cpp:39:16: warning: ignoring return value of ‘int scanf(const char*, ...)’, declared with attribute warn_unused_result [-Wunused-result]
scanf("%d",&k);
^
./Main.cpp:40:59: warning: ignoring return value of ‘int scanf(const char*, ...)’, declared with attribute warn_unused_result [-Wunused-result]
for (int i=1,v,p; i<=k; i++) scanf("%d %d",&v,&p),a[v]=p;
^
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 |
40 ms |
7552 KB |
binary_02.txt |
AC |
24 ms |
6912 KB |
hand_01.txt |
AC |
2 ms |
640 KB |
hand_02.txt |
AC |
1 ms |
640 KB |
hand_03.txt |
AC |
1 ms |
640 KB |
kary_01.txt |
AC |
20 ms |
3712 KB |
kary_02.txt |
AC |
30 ms |
5248 KB |
kary_03.txt |
AC |
21 ms |
4480 KB |
line_01.txt |
AC |
26 ms |
8448 KB |
line_02.txt |
AC |
33 ms |
9856 KB |
line_03.txt |
AC |
33 ms |
9856 KB |
line_04.txt |
AC |
37 ms |
9856 KB |
line_05.txt |
AC |
24 ms |
9216 KB |
line_06.txt |
AC |
27 ms |
9216 KB |
random0_01.txt |
AC |
31 ms |
3712 KB |
random1_01.txt |
AC |
38 ms |
5248 KB |
random1_02.txt |
AC |
39 ms |
5248 KB |
random1_03.txt |
AC |
40 ms |
5248 KB |
random1_04.txt |
AC |
47 ms |
5248 KB |
random1_05.txt |
AC |
38 ms |
5248 KB |
random1_06.txt |
AC |
38 ms |
5248 KB |
random1_07.txt |
AC |
39 ms |
5248 KB |
random1_08.txt |
AC |
55 ms |
5248 KB |
random2_01.txt |
AC |
27 ms |
4608 KB |
random2_02.txt |
AC |
27 ms |
4608 KB |
random2_03.txt |
AC |
27 ms |
4608 KB |
random2_04.txt |
AC |
28 ms |
4608 KB |
random2_05.txt |
AC |
36 ms |
4608 KB |
random2_06.txt |
AC |
43 ms |
4608 KB |
random3_01.txt |
AC |
22 ms |
3840 KB |
random3_02.txt |
AC |
31 ms |
3712 KB |
random4_01.txt |
AC |
38 ms |
5248 KB |
random4_02.txt |
AC |
43 ms |
5248 KB |
random4_03.txt |
AC |
38 ms |
5248 KB |
sample_01.txt |
AC |
1 ms |
640 KB |
sample_02.txt |
AC |
1 ms |
640 KB |
sample_03.txt |
AC |
1 ms |
640 KB |
star_01.txt |
AC |
37 ms |
5248 KB |
star_02.txt |
AC |
28 ms |
4480 KB |